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 › Arrays on the fly
Page Index Toggle Pages: 1
Arrays on the fly? (Read 926 times)
Arrays on the fly?
Mar 3rd, 2008, 2:16pm
 
Dear Foks,

since i am coming from actionscript, i wondered how to create Arrays on the fly?

For example, if i want to have x Arrays, where i don't know the number before

in Actopnscript:

for(int i = 0; i < some.length; i++){

    ["NameArr_"+i] = ["NameArr_"+i] .push(Array[i]);

}

that way x Arrays will be created and i don't need to initialize them before.

how can i accomplish this in processing?

Thanks a Lot!


Re: Arrays on the fly?
Reply #1 - Mar 3rd, 2008, 3:52pm
 
try the method append, which you can find in the reference.

int[] x = new int[0];
int someLength= 5;

for(int i = 0; i < someLength; i++){
x= append(x,int(random(10)));
}
Re: Arrays on the fly?
Reply #2 - Mar 3rd, 2008, 4:13pm
 
Hi,
thanks.
maybe i was not clear.

i would like to know, if it it possible to create a unlimited amount of singel arrays without initializing them at the head of the program?

for example, if i load a string from a database, then cut it in pieces and distribute them, depending on their name in different arrays. since i don't know how many names there are, i don't know how many arrays there will be.

So i would like to create Arrays which include the name of the loaded String and a number:

for(int number = 0; number < somelength; number++){
String "dataname_" + number = new String[0];
}

within the draw method

Thanks!
Re: Arrays on the fly?
Reply #3 - Mar 3rd, 2008, 7:27pm
 
mh ok. as i know you cant create variables with variable names like String "anything"= new String[0].
So I think you would go best using a abstract type like a Hashmap.

 HashMap as= new HashMap();
 String[] x= new String[0];
 as.put(new String("test"), x);
 String[] y= (String[]) as.get("test");

-
ramin
Re: Arrays on the fly?
Reply #4 - Mar 3rd, 2008, 11:28pm
 
sorry, but i don't really get this.

What i am looking for is a possibility to create a unlimited number of Arrays or Variables.

I am loading data, where i am not sure, if it is one string or 1000, i would like to have, depending on the number, for every string one variable, that i can compare them etc

Since i don't want to put 1000 times
String var0001;
.
.
.
at the begining of the program, i would like to do something like this
for(int i = 0; i < number;i++){
 String stringname + i;
}

isn't it possible to have something in this kind?

it looks that using HashMap also needs initializing
String []x
String []y

???
Re: Arrays on the fly?
Reply #5 - Mar 3rd, 2008, 11:38pm
 
HashMap only needs creating. You don't tell it how big it needs to be, it grows as you put things in.

e.g.
Code:

HashMap myMap=new HashMap();

String[] lines=loadStrings("thing.txt");
for(int i=0;i<lines.length;i++)
{
String name;
int[] values;
//some code to divide your input into a string containing the name, and an array of the values..
myMap.put(name,values);
}

// some time later...

int[] values=(int[])myMap.get("Something"); //get all the values associated with "Something"
Re: Arrays on the fly?
Reply #6 - Mar 4th, 2008, 1:36am
 
allright, thats pretty useful.
but what the problem seems to be, is that maybe some names are double and only theire position is different.
It kind of seems to be still limited.

Does someone know other posibilities or how to expand that?

And again i would like to ask, if it is absolutly impossible to create Variables on the fly? I mean, there must be a possibility to create a certain amount of variables without tiping the exact name manually?


Thanks a LOT!
Re: Arrays on the fly?
Reply #7 - Mar 4th, 2008, 12:40pm
 
greg_rasputin wrote on Mar 4th, 2008, 1:36am:
allright, thats pretty useful.
but what the problem seems to be, is that maybe some names are double and only theire position is different.


you can add the index onto the end of the name easily enough

HashMap map = new HashMap();
for (int i = 0 ; i < some.length ; i++) {
 map.put("dataname_" + i, anything);
}

the value part of a hash ('anything' in the above example) can be any Object, even another hash.
Re: Arrays on the fly?
Reply #8 - Mar 4th, 2008, 12:46pm
 
That's what arrays are for... you can set the size of an array within the program based on a received value.

So you could do something like:
Code:
class item
{
String name;
int[] vals;
item(String s, int[] v)
{
name=s;
vals=v;
}
}

item[] items; //currently has no set size...

void setup()
{
//stuff
String[] lines=loadStrings("data.txt");
items=new item[lines.length]; //items is now the size it needs to be...
for(int i=0;i<lines.length;i++)
{
String name;
//get the name somehow
int[] vals;
//get the values somehow, again vals can be set to the right length, or you can use append(...)
items[i]=new item(name,vals);
}

//this code allows you to have the same name with different values without replacing anything.
}

void draw()
{
//example code to find a specific value/values for duplicates

for(int i=0;i<items.length;i++)
{
if(items[i].name.equals("Thing"))
{
for(int j=0;j<items[i].vals.length;j++)
{
//prints all values of a name seperately.
println(items[i].name+" has a value "+items[i].vals[j]);
}
}
}
}
Re: Arrays on the fly?
Reply #9 - Nov 20th, 2008, 2:20am
 
hey greg_rasputin,

did you solve the problem or at least got an anwere?
Ive got the same probleme where i need to create a random amount of arrays and i dont want to creat them by hand.

Any idea?
Re: Arrays on the fly?
Reply #10 - Nov 20th, 2008, 3:39pm
 
Use ArrayList. They are extensible at will and you can put anything in them, including ArrayLists...
Page Index Toggle Pages: 1