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.
Page Index Toggle Pages: 1
dynamic array (Read 2093 times)
dynamic array
Feb 15th, 2008, 2:09am
 
Hello dear community,

i'm working on a little project and i want to store data in an array.
While reading through the Processing book by Fry and Reas i only discovered arrays where you set the amount of data right at the beginning.
But I need a dynamic array so that i don't need to declare a fixed value on how much data I will store in the array.

almost like that.

int data [] = new int [];
.
.
.
for (int i = 0; i< test.length(); i++) {
data [i];
}

as this programm should be used for many different sizes of the variable "test" i need a dynamic array.

would you mine to explain to me how this type of array works?
many thanks to you!

Re: dynamic array
Reply #1 - Feb 15th, 2008, 3:43am
 
Ok, this is a fairly major and crucial point in programming.

1.) to make a new array holding integers (all spots initialized to 0) of a certain length, you can do:

int[] data = new int[5];

will make data be {0,0,0,0,0}.

You can also use a variable:
int n = 5;
int[] data = new int[n];

is the same as the above.

Also,
int[] data = new int[test.length()]

^ makes a new array of length test.length().

Now, if you truly need a "dynamic" array, where it can hold a varying amount of data, you could just make the array inordinately long (say, 1000 units) and then only use the first few. But, this is wasteful.

You need what is known as a Collection, and they are made for you in java.util. (Though, you could design your own.)

The simplest is "ArrayList".

ArrayList data = new ArrayList();
data.add(1);
data.add(2);

data is now {1,2}.

to get back your stored data,
data.get(n)

So, the above data arraylist would return 1 on .get(0) and 2 on .get(1).

Was I clear enough?
Re: dynamic array
Reply #2 - Feb 15th, 2008, 12:01pm
 
many thanks for your detailled help.
I tried to use the ArrayList as you explained to me.

ArrayList data = new ArrayList ();
for (int j = 0; j < test.length; j++) {
       data.add (j);
}

I tried this to store the value of j in my array. When using this I get an error naming: "Perhaps you wanted the overloaded version "boolean add (java.lang.Object$1);" insted?
When using 0,1 or 2 insted of j I get the same error.

Could you help me once again to solve the problem?

Many thanks to you!

PS. your explanation are great! Smiley
Re: dynamic array
Reply #3 - Feb 15th, 2008, 12:41pm
 
@ leanderlike
Unfortunately it seems that ArrayLists currently won't take ints.

Check out this thread covering the same topic. It may be a bit confusing with all the talk about Java versions, but towards the end, fry recommends to go with a standard int array and using expand() to change its length when needed.

Hope this helps.

@ taifunbrowser
That's a great little rundown on arrays.
Re: dynamic array
Reply #4 - Feb 15th, 2008, 2:22pm
 
Quote:
Unfortunately it seems that ArrayLists currently won't take ints.

But it will take Integers. Try this instead, if you want to use ArrayLists :
data.add(new Integer(j));


Re: dynamic array
Reply #5 - Feb 15th, 2008, 2:51pm
 
Quote:
But it will take Integers.

Thanks for the advice!

In case you're confused, what's the difference between int and Integer
And here's what the Java Reference has to say about Integer.
Page Index Toggle Pages: 1