I am trying to write a function in processing.js that takes as an input an array list with 4 lists of class int[]
each containing 4 elements. This is how I am representing a matrix, with each list in the array list representing a row. Index 0 of the Array list is the top row and index 3 of the array list it the bottom row. I want to write a function to transpose this array list, but I have run into a snag. here is my code so far (note: Z is a 4x4 arraylist filled with zeros):
ArrayList<int[]> Transpose(ArrayList<int[]> M){
ArrayList<int[]> A=Z;
for (int q=0; q<4; q++){
int[] atemp={};
for (int p=0; p<4; p++){
int[] mtemp=M.get(p);
int i=mtemp[q];
append(atemp,i);
}
A.add(atemp);
}
return A;
}
My problem seems to be happen when I try to append i
to atemp
. When I print out the atemp.length
right before it is added to A
, it prints a length of 0. I am not sure why atemp has remained empty, and I am wondering if anyone can help me understand what is happening and how to fix it.