Problem with .clear with IntList and FloatList

edited June 2015 in Programming Questions

Hello!

I've been trying to use IntList and FloatList for easily cleaning array's elements. Everything is ok. I declare them right in the start, before void setup(), for example:

IntList dirPos0 = new IntList();

But when I try to put dirPos(0).clear() inside void draw() (within or outside an if) I get the error "The function dirPos(int) does not exist." Why is that?

Answers

  • Detail: I'm using dirPos0.append(value) in a keypressed action after void draw(). When I add dirPos0.clear() after the dirPos0.append() it works. But I need to put dirPos.clear() inside an if located at void draw(). How this could be done?

  • edited June 2015

    Another detail: dirPos0.clear() only works inside the same keypressed action (only for test). If it comes after dirPos0.append(value), but outside the same keypressed action it doesn't work.

  • Are you sure you haven't declared the variable again separately in keypressed?

  • It's a simple syntax error. Your IntList is called dirPos0, not dirPos(0). dirPos0.clear() should work fine.

  • edited June 2015

    Yes... My syntax mistake. Now the problem is: is it possible to use a for loop to navigate through dirPos0, dirPos1 and dirPos2 to clear them all?

    This is the reason I've been trying to use dirPos(i).

    void clearIndex() {
    for (int i = 0; i < bank.size(); i++) {
      duracao0.clear();
      dirPos0.clear();
    }
    bank.clear();
    }
    

    I couldn't do it with dirPos(i).clear() or dirPos + i.clear()

  • @blindfish I got confused about the variable name when I was working with the for loop.

  • No, clear() removes the whole list as a whole. No need for the for loop. If you want to remove an individual element, use remove(i). But if you loop over the list and remove elements like that, you'll run into problems because when you remove an element, all following elements in the list shift a position, meaning the last elements no longer exist when the loop gets there, throwing an error. The solution is to loop over the list backwards:

    for(int i = bank.size()-1; i >= 0; i--)

  • Yes, @colouredmirrorball, I understand this. But dirPos0 and dirPos1 are two different IntLists (and I should have plenty in the future). I want to use the for loop for cleaning every IntList and FloatList I got.

  • It looks like this now:

     void clearIndex() {
     for (int i = 0; i < bank.size(); i++) {
        duracao0.clear();
        duracao1.clear();
        duracao2.clear();
        dirPos0.clear();
        dirPos1.clear();
        dirPos2.clear();
      }
      bank.clear();
      }
    
  • Oh, I get what you mean. Well, every time you have a lot of something, you should put it in an array or list of some sorts. Yes, you can have a list of lists (this is a 2D list). Then you can easily loop through it and clear them.

    ArrayList<IntList> dirPos = new ArrayList<IntList>();
    
    dirPos.add( new IntList() );
    dirPos.get(0).append(5);
    
  • Hum!!! I knew about the 2D lists, but have not thought about it until now. So I should use dirPos.get(0).clear() to get it done, right?

    Thank you a lot, @colouredmirrorball! For each answer!

  • Yup, that's how you do it!

    My pleasure... I'm studying for my exams and this is a great way to slack off and not feel bad about it because I'm helping others :P

  • Nice! This is really a great way to improve ourselves. Thanks again! :)

  • edited June 2015 Answer ✓

    Extra tip, if we know how many fixed entries we're gonna need, a regular array provides a more simplified syntax besides being faster: :-bd

    static final int DURATIONS = 3;
    final FloatList[] durations = new FloatList[DURATIONS];
    {
      for (int i = 0; i != DURATIONS; durations[i++] = new FloatList());
    }
    
    static final int POSITIONS = 5;
    final IntList[] positions = new IntList[POSITIONS];
    {
      for (int i = 0; i != POSITIONS; positions[i++] = new IntList());
    }
    
    void setup() {
      printArray(durations);
      println();
      printArray(positions);
      exit();
    }
    

    Of course, when the length varies along the way, we should use a List instead of an array as @colouredmirrorball told you:

    import java.util.List;
    
    static final int INITIAL_DURATIONS = 3;
    final List<FloatList> durations = new ArrayList<FloatList>();
    {
      for (int i = 0; i++ != INITIAL_DURATIONS; durations.add(new FloatList()));
    }
    
    static final int INITIAL_POSITIONS = 5;
    final List<IntList> positions = new ArrayList<IntList>();
    {
      for (int i = 0; i++ != INITIAL_POSITIONS; positions.add(new IntList()));
    }
    
    void setup() {
      println(durations);
      println();
      println(positions);
      println();
    
      durations.add(new FloatList());
      println(durations);
    
      exit();
    }
    
  • edited June 2015

    Very good, @GoToLoop! Actually, I know how many fixed entries I'm gonna need. I'll try to do it with a regular array. I already did it with ArrayList and it's ok, but i'm gonna convert it to regular arrays for being faster and more simplified.

    Now I have more one doubt at the following code (I declared everything else just as you did in the first example, only changing code inside setup):

        void setup() {
          printArray(durations);
          println();
          printArray(positions);
          println();
          positions[3].append(10);
          println(positions[3]);
          println();
          println(positions[3].get(0));
          positions[3].clear();
          println("RESET: " + positions[3]);
          println("TRY: " + positions[3].get(0));
          exit();
        }
    

    I got RESET: IntList size=0 [ ] and TRY: 10

    Why isn't it empty of value?

  • edited June 2015

    If you got "TRY: 10", much probably you're using an old Processing version.
    Not so long ago, we'd get ArrayIndexOutOfBoundsException. :|

    Take notice that neither clear() nor remove() actually erase anything.
    They merely modify the size(). The underlying array keeps its length! ;;)

  • I was trying it at Processing 2.2.1. I'll do some more tests and experiments about arrays.

    I'm still understanding this concept of clear() merely modify the size().

    Thank you all!

  • Perhaps seeing what happens inside clear() can make things clearer! O:-)

    https://github.com/processing/processing/blob/master/core/src/processing/data/IntList.java#L158

    public void clear() {
        count = 0;
    }
    
  • Yes! Indeed a very good option to check the source code. :)

Sign In or Register to comment.