I am trying to write code that will turn N arrays into a multidimensional array with N rows. I currently have a code that can turn 2 arrays into a multidimensional array with 2 rows. However, I am unsure how to modify it in order to make this code take N arrays.
Additionally my code currently can only take arrays of the same size. However, it needs to be able to take arrays of different lengths. This would entail that the rows in my multidimensional array would not always be equal length. I have been told that this means a list might be more suitable than an array. However I am unfamiliar with lists.
Here is my code as it currently stands:
public class test5 {
int [][] final23;
public int [][] sum(int [] x, int[] y)
{
final23= new int[2][x.length];
for (int i = 0; i < Math.min(x.length, y.length); i++)
{
final23 [0][i] = x[i];
final23 [1][i] = y[i];
}
return final23;
}
public void print()
{
for (int i = 0; i < final23.length; i++)
{
for (int j = 0; j<final23[0].length; j++)
{
System.out.print(final23[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args)
{
int l[] = {7,3,3,4};
int k[] = {4,6,3};
test5 X = new test5();
X.sum(k,l);
X.print();
}
}
Thanks in advance. Sorry i am new to java and just learning.