We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
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.
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).
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:
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.
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! :)
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
Of course, when the length varies along the way, we should use a List instead of an array as @colouredmirrorball told you:
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):
I got RESET: IntList size=0 [ ] and TRY: 10
Why isn't it empty of value?
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
Yes! Indeed a very good option to check the source code. :)