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 › array length definition
Page Index Toggle Pages: 1
array length definition (Read 606 times)
array length definition
Dec 24th, 2007, 6:00pm
 
hi,

i want to define an empty array but processing ask me array length.
i want to populate it at runtime because i need the legth value, then at startup i need an empty array with length 0. how can i do?

processing reference only shows this


int[] numbers = new int[3];
numbers[0] = 90;
numbers[1] = 150;
numbers[2] = 30;
int a = numbers[0] + numbers[1]; // Sets variable a to 240
int b = numbers[1] + numbers[2]; // Sets variable b to 180

int[] numbers = { 90, 150, 30 };
int a = numbers[0] + numbers[1]; // Sets variable a to 240
int b = numbers[1] + numbers[2]; // Sets variable b to 180
Re: array length definition
Reply #1 - Dec 24th, 2007, 11:13pm
 
If you're readign some value that you want to use as the array length you can do:

Code:
int[] items=new int[myReadInNumber];


If you don't know how long it needs to be and want to just keep adding stuff to it:

Code:
int[] items=new int[0];

if(something)
{
items=append(items,newValue);
}
//...
items=append(items,273);

Re: array length definition
Reply #2 - Dec 25th, 2007, 10:53am
 
thanks a lot, i will use append function but i don't find it into documentation also it seem a bad way to do this.
why processing donsen't support array without dimension and why the doc is so poor?
Re: array length definition
Reply #3 - Dec 25th, 2007, 5:58pm
 
See the "Array Functions" section on http://processing.org/reference/index.html for all the documentation.

Processing is built on Java and in Java an array has to have a size. There are other things like ArrayLists in Java that don't need to have a size.
Re: array length definition
Reply #4 - Dec 26th, 2007, 10:42am
 
ok, if is the only way i do it thanks a lot :]
Page Index Toggle Pages: 1