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 & HelpIntegration › ArrayList + String
Page Index Toggle Pages: 1
ArrayList + String (Read 1506 times)
ArrayList + String
Jun 2nd, 2008, 5:37pm
 
Hi, I can't find what is wrong in what I do and I don't know enough java. Here is my problem:

I can't get a String to be equal to a element of a ArrayList.
here is shorten version of my code:


ArrayList[] questions = new ArrayList[1];

void setup(){
 
//Load my txt file
 String Lines[] = loadStrings("questions.txt");

 //Expand my ArrayList according to the txt file
 questions = (ArrayList[]) expand(questions, (Lines.length/3));

//Store my lines in the arrays of the ArrayList
 for (int i = 0; i < questions.length; i++) {
   questions[i] = new ArrayList();
   for ( int q=0; q<3; q++){
   questions[i].add (new String(Lines[(i*3)+q]));
/* I first tried questions[i].add (Lines[(i*3)+q]) */
   }
 }
}

void draw(){
/* I just put where is the problem */
String myQuestion = questions[0].get(1);
text( myQuestion, width/2, height-20);

}

ArrayList seems really usefull and I hope somone will hep or that I will figure it out by myself.
Thanks
Re: ArrayList + String
Reply #1 - Jun 2nd, 2008, 8:39pm
 
You have to cast the content of your ArrayList back to String cause JAVA didnt know the type of the Elements in an ArrayList:

Code:

String myQuestion = (String)questions[0].get(1);



Re: ArrayList + String
Reply #2 - Jun 2nd, 2008, 8:59pm
 
Oh Brilliant!!!
So beautifully simple. I try that at work tomorrow.

I though by looking at the doc on ArrayLists that I could declare the type of ArrayLists at the beginning but I must have miss interpreted it ( collections seems to be something else).

Thanks for this quick and clear answer.
Re: ArrayList + String
Reply #3 - Jun 3rd, 2008, 12:55am
 
You can specify the type of the objects in an ArrayList... in Java 1.5 only! And Processing doesn't support this syntax yet.
Re: ArrayList + String
Reply #4 - Jun 3rd, 2008, 11:49am
 
haa, ok. I think processing is moving to 1.5 with version 0138.
Thanks again, it works nicely ( of course ).
Re: ArrayList + String
Reply #5 - Jun 3rd, 2008, 1:54pm
 
Processing is moving to 1.5, but it still doesn't support the Generics syntax, which would allow you to make specific versions of ArrayLists.
Re: ArrayList + String
Reply #6 - Jun 3rd, 2008, 2:18pm
 
TM wrote on Jun 3rd, 2008, 11:49am:
haa, ok. I think processing is moving to 1.5 with version 0138.

from revisions.txt:

+ Switching to Java 1.5
 We are moving to Java 1.5 as a minimum requirement for running
 Processing, as well as sketches created with it. This is an
 attempt at self-preservation as we try to limit the number of
 platforms that we support. Note, however that we *still do not*
 support Java 1.5 language extensions (enum, enhanced for loop,
 generics, etc.) More info about that can be found here:
 http://dev.processing.org/bugs/show_bug.cgi?id=598

 If you find places in the code or reference that refer to Java
 1.4 support, please let us know via dev.processing.org/bugs.
Page Index Toggle Pages: 1