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 › easier way to apend
Page Index Toggle Pages: 1
easier way to apend? (Read 973 times)
easier way to apend?
Jan 5th, 2010, 4:20pm
 
i got

int[] gcu;

And i want to add values to that, do i have to create a counter to append the array?

gcu[counter] = append....

Code:
  void cleanUpGrid(){//alles met 6 buren de g[#] waarde oplsaan in array, vervolgens al die waarden op false zetten
for(int i=0; i<g.length; i++){
// println(g[i]); //
x = i % w;
y = (i / w) % h;
z = i / (w * h);
//int p = (x + y * w) + z * w * h;//
if(checkPos(x-1, y, z) == true){
if(checkPos(x+1, y, z) == true){
if(checkPos(x, y-1, z) == true){
if(checkPos(x, y+1, z) == true){
if(checkPos(x, y, z-1) == true){
if(checkPos(x, y, z+1) == true){
//println("g["+i+"]"+" kan weg");
gcu = append(gcu, i);
}
}
}
}
}
}
}
}
Re: easier way to apend?
Reply #1 - Jan 5th, 2010, 4:23pm
 
Also when i try it with a counter it says

cannot convert from Object to int

what am i doing wrong?

Code:
  void cleanUpGrid(){//alles met 6 buren de g[#] waarde oplsaan in array, vervolgens al die waarden op false zetten
int c = 0;//count
for(int i=0; i<g.length; i++){
// println(g[i]); //
x = i % w;
y = (i / w) % h;
z = i / (w * h);
//int p = (x + y * w) + z * w * h;//
if(checkPos(x-1, y, z) == true){
if(checkPos(x+1, y, z) == true){
if(checkPos(x, y-1, z) == true){
if(checkPos(x, y+1, z) == true){
if(checkPos(x, y, z-1) == true){
if(checkPos(x, y, z+1) == true){
//println("g["+i+"]"+" kan weg");
gcu[c] = append(gcu[c], i);
c++;
}
}
}
}
}
}
}
}
Re: easier way to apend?
Reply #2 - Jan 5th, 2010, 11:11pm
 
Append() returns an Object. That means that you have to cast the object into an array that fits.
http://processing.org/reference/append_.html
Code:
intArray = (int[])append(...); 



The line "gcu[counter] = append(...);" translates to "integer at counter = new Object" which doesn't match up.
You would only use an index if you had sub-arrays.
Code:
int[] intArray; //array of integers
int[][] intSubs; //array of arrays
...
//Note the casting to an integer array
intSubs[0] = (int[])append(...); //adds to the first sub array
intArray = (int[])append(...); //just adds to the array.

Hope that makes sense.

Maybe an ArrayList would be easier to follow when adding or removing items. Still have to cast to the correct type though.
http://processing.org/reference/ArrayList.html
Re: easier way to apend?
Reply #3 - Jan 6th, 2010, 4:10am
 
pff confusing...
Could u give an example of how to store the 'i' value in my cleanUpGrid function?
Re: easier way to apend?
Reply #4 - Jan 7th, 2010, 10:53pm
 
I take back part of my post; you would only have to cast if the array is of objects (instead of primitives such as 'int').

As for adding i to the array, this assigns gcu to a new array that includes both the original data from gcu and i:

"gcu = append(gcu, i);"

Will it do what you need? Can't say without more code to test the function. Wink
Re: easier way to apend?
Reply #5 - Jan 8th, 2010, 2:09am
 
clankill3r wrote on Jan 5th, 2010, 4:20pm:
i got

int[] gcu;

And i want to add values to that, do i have to create a counter to append the array

gcu[counter] = append....


Do you mean append in terms of making the array longer, or to insert the value into a certain position in your array If it is the latter, you can just assign the value to the array straight up. I.e. gcu[c] = 'value'; The only requirement here being that c is inside the arrays length, of which you have to decide at first. I.e. int[] gcu = new int[100];

See also:
http://processing.org/reference/arrayaccess.html

Sorry if this was not what you were looking for   Huh
Re: easier way to apend?
Reply #6 - Jan 9th, 2010, 8:25am
 
thx all, this is wat i was looking for:


int[] gcu = new int[0];//grid clean up


gcu = append(gcu, i);

Page Index Toggle Pages: 1