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 › Confused by processing's array implementation
Page Index Toggle Pages: 1
Confused by processing's array implementation (Read 805 times)
Confused by processing's array implementation
Sep 21st, 2008, 12:59am
 
Hi All,

I'm having quite some trouble using array's in processing, i come from a ActionScript / Python background, and processing's implementation of array's is confusing me. I gather that i have to declare the length of my array up front, plus the datatype of the information inside the array. For some reason that's unclear to me i am not able then to "fill" the array with data so to speak.

code:
String[] fileNames = new String[numElements];

 for (int i = 0; i<numElements; i++) {
   XMLElement chart = xml.getChild(i);  
   String from = chart.getStringAttribute("from");
   String to = chart.getStringAttribute("to");
   String fileName = from + "-" + to + "-" + weeklyArtistChartListFilename + ".xml";
   fileNames = append(fileNames,fileName);
   
 }
 
 print(fileNames[0]);

now, the print statements keeps returning "null" to me for the total amount of elements that are in the array. So, where did the filenames go that i appended?
Re: Confused by processing's array implementation
Reply #1 - Sep 21st, 2008, 1:41am
 
By appending you're adding another element to the end of the array. You need to assign the value to the array element.

Quote:
String[] fileNames = new String[numElements];

for (int i = 0; i<numElements; i++) {
  XMLElement chart = xml.getChild(i);    
  String from = chart.getStringAttribute("from");
  String to = chart.getStringAttribute("to");
  String fileName = from + "-" + to + "-" + weeklyArtistChartListFilename + ".xml";
  
  fileNames[i] = fileName; // changed this line

}

print(fileNames[0]);
Re: Confused by processing's array implementation
Reply #2 - Sep 21st, 2008, 10:15am
 
append() is nice if you don't know beforehand how many elements you need to put in the array, as it grows the array each time you need to add an element there.
It is nice for newbies which don't need to be submerged by concept of ArrayList to be converted to array later, for example.
But it is very costly, because on each append, a new array is created with one more element than the previous one, and the old array is copied there!

To make your code work with append, which adds always at the end of the array (ie. after the empty elements you created first!), you can use the following code instead:
Code:
int numElements = 10;
String[] fileNames = new String[0];

for (int i = 0; i<numElements; i++) {
String fileName = "Element " + i;
fileNames = append(fileNames,fileName);
}

print(fileNames[0]);
(simplified for testing)
Otherwise, if you know in advance the final size of the array, do as polymonkey shown.
Re: Confused by processing's array implementation
Reply #3 - Sep 22nd, 2008, 12:43am
 
Thanks for the feedback guys, after some more playing around I'm getting the hang of it. I suppose ArrayList is some kind of java-provided datatype?
Re: Confused by processing's array implementation
Reply #4 - Sep 23rd, 2008, 12:40am
 
Yes. See ArrayList for a reference (but forget the generics notation, not supported by Processing yet).
Page Index Toggle Pages: 1