How to append to a 2 dimensional array?

Hello, I’m having trouble finding an answer to this problem.

  1. How would you append an array to an array.

One of the ways I have tried doing this:

int[][] array;

  for (int i=1; i<=20; i++) { 
      append(array,{0,0,0});
    }
 }
  1. How would you append to an array that was within that array?

How I’ve tried this:

int[][] array;

for (int i=1; i<=20; i++) { 
    for (int j=1; j<=20; i++) {
      append(array[i][j],0);
    }
 }

Thanks in advance.

1 Like

i play long ago, and sure there are better ways

array copy expand
// 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…

1 Like

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);
2 Likes

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?

If an array is faster, how would you code that?

sorry that is little bit hidden under the above

array copy expand

link you might have missed

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
https://processing.org/reference/IntList.html


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.

1 Like

If that’s so, it means you already know how many rows & columns your 2D array is gonna need. :flushed:

Apparently, according to your posted double loop example, that 2D array is 20 x 20. :nerd_face:

So you’re just gonna need this, and you’re done: :bulb:

static final int ROWS = 20, COLS = 20;
final int[][] grid = new int[ROWS][COLS];

void setup() {
  for (final int[] row : grid)  println(str(row));
  exit();
}
3 Likes