views:

52

answers:

3

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.

+1  A: 

Since they might not be equal lengths, you're going to have to account for dead spots on the matrix, perhaps utilizing the Null Object pattern. Initialize each element to a special case that can be handled uniformly, like any other element.

That's my advice on how to do it with arrays, by the way. Collections aren't hard though.

Mike
+1  A: 

Multidimensional arrays in java are not similarly sized - they're just a bunch of arrays arranged in an array, just like any other object would be. if you wanted them to all be the same size you'd have to find the max size and fill smaller arrays with some sort of empty or default value.

In any case, since you want to copy some N number of arrays, as long as you accept different lengths, why not use varargs:

public static int[][] toMulti(int[] ... args) {
 // you can't resize an array, so you have to size your output first:
 int[][] output = new int[args.length][];
 for (int i =0; i<args.length; i++)
 {
    output[i[=args[i].clone();

   //you could also do this copying 1 at a time, or with
   int[] arr =new int[args[i].length];
   System.arraycopy(args[i], 0, arr, 0, args[i].length);
   output[i]=arr;
 }

If you wanted to size them all to the same size System.arraycopy would be better, as you'd create the array at the max size, the remaining values would automatically be 0's (or nulls for object arrays).

Steve B.
+1  A: 
import java.util.Arrays;
class Arr
{
public static void main(String[] args)
{
 int[][] ar=toMulti(new int[]{1,2},new int[]{1,2,3},new int[]{5,6,7,8});
 System.out.println(Arrays.deepToString(ar));

/*OR You can directly declare 2d array like this if your arrays don't
come as user inputs*/
 int[][] arr={{1,2,3},{1,2},{3,4}};
  System.out.println(Arrays.deepToString(arr));
}    
   /* ... is known as variable argument or ellipsis.
      int[] ... denotes that you can give any number of arguments of the type int[]
      The function input that we get will be in 2d-array int[][].So just return it.*/
   public static int[][] toMulti(int[] ... args) {
    return args;

}
}
Emil
This code just return's the reference's in 2d array.But if you need it as a copy then you better follow Steve's code.The difference is that if you make any change in the 1d array it will reflect in the 2d array also and also vice-versa.But this will be much more faster and use less memory in case you are dealing with huge arrays.
Emil