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 › How to fill an array
Page Index Toggle Pages: 1
How to fill an array? (Read 851 times)
How to fill an array?
Nov 6th, 2006, 4:41pm
 
Hello!

I want to fill an array with the resalts from an Web-Service call.  I miss an Method like array.add.
How can I do this?

Best regards Hans
Re: How to fill an array?
Reply #1 - Nov 6th, 2006, 5:46pm
 
The following pseudo code should help you:

To add a single new element to an existing array:
MyArray=(MyArrayType[])append(MyArray,MyNewElement);

To add an array onto the end of another:
MyArray=(MyArrayType[])concat(MyArray,MyOtherArray);

Re: How to fill an array?
Reply #2 - Nov 7th, 2006, 3:33pm
 
You might also look into using an ArrayList.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

Some example code:

Code:

// Declaring the arraylist
ArrayList plist = new ArrayList() ;

// Adding 100 Particle objects
for (int i = 0; i < 100; i++) {
plist.add(new Particle()) ;
}

// Iterating the arraylist
for (int i = 0; i < plist.size() ; i++) {
Particle p = (Particle) plist.get(i) ;
p.doStuff() ;
}
Re: How to fill an array?
Reply #3 - Nov 7th, 2006, 7:13pm
 
Hello JohnG and shiffman,

thank you for your responses.
I try this from the Reference:
String[] pListe = new String[200];// I define the size? I dont now yet however big my array lake at the end. Can I made this more flexible?

String pListe[]=append(pListe,board.amazon.getProductName());

I become the error message:
The variable "pListe" may be accessed here before having definitley assigned a value

What do I wrong?

Dear Shiftman, I don't now how to use Java in processing. Is this possible? I think that only the code from the reference work.
Please excuse my English...

Hans

Re: How to fill an array?
Reply #4 - Nov 7th, 2006, 7:59pm
 
This should work, however I've not used the switchboard stuff, so I'm just copying your code for that part.

Code:
String[] pListe;

void setup()
{
pListe=new String[0]; // starts at 0, append makes it bigger
pListe=(String[])append(pListe,board.amazon.getProductName());
}


Page Index Toggle Pages: 1