// array expand test
// https://processing.org/discourse/beta/num_1175740933.html
int[][] test ={{11, 12, 13}, {21, 22, 23}, {31, 32, 33}, {41, 42, 43}};
int[][] tmp ;
int cols1,rows1,cols2,rows2;
cols1=test[0].length;
println(" test[0].length == columns : "+cols1);
rows1=test.length;
println(" test.length == rows : "+rows1);
for (int i = 0; i<4; i++) {
for (int k = 0; k<3; k++) {
println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
}
}
// backup
tmp = new int[rows1][cols1];
println(" copy to tmp ");
tmp = test;
for (int i = 0; i<4; i++) {
for (int k = 0; k<3; k++) {
//println("row ["+i+"] "+" col ["+k+"] : "+tmp[i][k]);
}
}
// expand:
println(" make one longer ");
test = new int[rows1+1][cols1];
cols2=test[0].length;
println(" test[0].length == columns : "+cols2);
rows2=test.length;
println(" test.length == rows : "+rows2);
for (int i = 0; i<rows2; i++) {
for (int k = 0; k<cols2; k++) {
//println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
}
}
println(" copy back ");
for (int i = 0; i<rows1; i++) {
for (int k = 0; k<cols1; k++) {
test[i][k] = tmp[i][k];
//println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
}
}
println(" show all ");
for (int i = 0; i<rows2; i++) {
for (int k = 0; k<cols2; k++) {
println("row ["+i+"] "+" col ["+k+"] : "+test[i][k]);
}
}
that might be a good reason to make a class and a arrayList of it?
class Row {
int x,y;
Row(int _x, int _y ) {
x=_x;
y=_y;
}
}
ArrayList<Row> my_array = new ArrayList<Row>();
void setup(){
my_array.add( new Row(1,2) );
my_array.add( new Row(3,4) );
for ( int i = 0;i < my_array.size();i++) println(my_array.get(i).x , my_array.get(i).y );
}
but a much better reason for it is that you can make mixed record types with it,
just add string, float, boolean variables to the class…
Its a lot more typing but ArrayList is better for performance if you plan on doing this a lot. The disadvantage is you can’t use primitive data types only the class wrappers which are slower. The advantage of using ArrayList instead of append is it doesn’t have to create a new array every time you add something to it.
// initialization
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
// append to array list that is already in the 2d list
list.get(/* index */).add(/* value */);
// append a new arraylist to the 2d list
ArrayList<Integer> otherList = new ArrayList<Integer>();
otherList.add(5);
list.add(otherList); // you can either create an otherList object
list.add(new ArrayList<Integer>()); // or add an empty one
// add the contents of one 2d list to the end of another
ArrayList<ArrayList<Integer>> other2d = new ArrayList<ArrayList<Interger>>();
list.addAll(other2d);
I am doing this just in setup(), but need to refer back to variables from these arrays/ArrayLists in draw(). Would it still be faster to use ArrayList or just an array?
array list use array in a smart way, so on some scale it might be slower…
tell me when you notice it.
the thinking/using array .vs. arraylist
has to do with the available functions
you might get a idea here from the intList
using array and make some better code ( like put / use it as a own function )
might have same result, still it not easy beats this line: my_array.add( new Row(a,b) );
as i define the arrayList “my_array” global, you can use it in setup ( and draw )
if you move that line down into setup it will be NOT usable further.
now i not see further considerations for that but there can be.