We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Creating arraylists inside a loop
Page Index Toggle Pages: 1
Creating arraylists inside a loop (Read 280 times)
Creating arraylists inside a loop
Feb 26th, 2009, 8:33am
 
I have been searching all over for this, but can't seem to find a single place that directly addresses my problem, even though it seems to me to be something a lot of people would encounter. Nevertheless, this is the relevant part of my code:

http://pastebin.com/m240a26d7

Line 32 and line 38 are where my problems occur. Obviously protocolRules.add(new ArrayList<String>()); wont't work, but how can i add new arraylists to my outer arraylist, in a loop?

Any help is greatly appreciated.
Re: Creating arraylists inside a loop
Reply #1 - Feb 26th, 2009, 11:46am
 
First thing, you have remaining string == "stuff" comparisons. As you commented, this doesn't work: Strings in Java are objects, and you can must compare objects using equals(). Actually, you can use == but it compares references, not content.
Code:
String a = "foo";
String b = a;
println(a == b); // true
b = "foo";
println(a == b); // true: literal strings are interned in Java
b = "f" + "oo";
println(a == b); // true: Java compiler is smart enough to do concatenation itself...
b = "f";
b += "oo";
println(a == b); // false!

Idem when you get a string from an external source (user input, file...).
Mmm, looking again at the source, it can work because precisely you use the same literal strings.
Note that I would define these strings at class level as constants, to highlight the fact they are the same entity.

Next, your question.
protocolRules.add(new ArrayList<String>());
can work. If protocolRules is a Collection (eg. an ArrayList) of type ArrayList<String>...
Now, I am not sure why you add an empty array list there.

And if the code is in a .pde file, you must use untyped ArrayLists: Processing uses Java 1.6 but is currently stuck on 1.4 syntax. The 1.6 syntax is only usable within .java files in the sketch. (I should copy this sentence and paste it each time...)
Re: Creating arraylists inside a loop
Reply #2 - Feb 26th, 2009, 7:23pm
 
Thanks for the reply. It turnes out, like you said, that it actually works to make a new arraylist in a loop like that. I add empty ones because i fill them later on. And i don't need to name then for the purpose of my problem, because the contructor i am sending the lists to will know where each field he wants is in each of the arrylists.

What made my prog fail was something really basic, and i just assumed that the arraylist creating was what was wrong. It turned out i just forgot to initialize the outer arraylists ><.

Again, thanks for the input.


Best regards,

Magnus
Page Index Toggle Pages: 1