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 › Define ArrayList by hand
Page Index Toggle Pages: 1
Define ArrayList by hand (Read 206 times)
Define ArrayList by hand
Mar 7th, 2009, 1:54am
 
We can define an array with the syntax

= {1, 21, 53, 54};

But I cannot figure out how to do that with an ArrayList. Is this possible? Can you show me how it is done?
Re: Define ArrayList by hand
Reply #1 - Mar 7th, 2009, 9:47am
 
Basically, no.

Now, there are tricks to workaround this: Double Brace Initialization and Building your own literals in Java - Lists, and Arrays. Tricks seen in Hidden Features of Java.

Code:
ArrayList als = new ArrayList()
{{
add("Foo");
add("Bar");
add("42");
}};

void setup()
{
for (int i = 0; i < als.size(); i++)
{
println((String) als.get(i));
}
}

Note that such init will actually create an anonymous class!
Re: Define ArrayList by hand
Reply #2 - Mar 7th, 2009, 4:09pm
 
Thanks very much Phil. Your answer is filled with enormous amounts of information.
Page Index Toggle Pages: 1