views:

60

answers:

4

If I have a 2D array, arr[][], and let say I do this: arr[0]=10; What does this do? I only assigned 10 in the first dimension, but I didnt mention the second dimension. What happens in this case?

Edit: Ok what about this:

public static int[][] mystery(int[][] srcA, int[][] srcB) {

    2 int width= srcA.length, height = srcA[0].length;
    3 int[][] output = new int[width][height];
    4 for (int x = 0; x < width; x++) {
    5 if(x%2 ==0) output[x] = srcA[x];
    6 else output[x] = srcB[x];
    7 }
    8 return output;
    9 }
+1  A: 

That makes no sense and you will get a compiler error warning you for incompatible types. If you only do arr[whatever] you should only assign it to a one dimensional array, ie

int[][] arr;
int[] otherArr;
arr[0] = otherArr;

--edit--

Your code should work OK.

output[x] = srcA[x];

output[x] is of the type int[] and so is srcA[x]

willcodejavaforfood
A: 

You will get an incompatible type error. int[][] expects an int[] when you assign to the 1st dimension.

class test{
    public static void main(String[] args){
            int[][] myarr = new int[10][10];
            myarr[10] = 0;
    }
}

j.java:4: incompatible types
found   : int
required: int[]
    myarr[10] = 0;}
                ^
1 error

EDIT: The line output[x] = srcA[x]; compiles just fine because if you look at what srcA[x] returns it is an int[]. When you assign to output[x] it is looking for an object of type int[]. So everything is fine.

controlfreak123
Can you elaborate more on your edit. I dont quite understand whats going on
fprime
now look at the edit
controlfreak123
+1  A: 

Also keep in mind that your 2d array does not need to be square.
A variable of type int[][] means that this variable is an array of int[]s.

So lets say we declared a variable int[][] array = new int[3][].
Then this means that array[0], which is of type int[] can now be assigned to any other value of type int[].

Here is a short example program.

public class Example {
  public static void main(String args[]){
      int[][] a = new int[3][];
      a[0] = new int[]{1};
      a[1] = new int[]{1,2};
      a[2] = new int[]{1,2,3};
      display(a);
  }

  private static void display(int[][] array){
      for(int[] row : array){
          for(int value : row){
              System.out.print(value + " ");
          }
          System.out.println();
      }
  }
}

Output:
1
1 2
1 2 3

fthinker
A: 

Be careful about the length of srcB. Line 6 tries to access srcB[x], where x can be up to srcA.length - 1; However, you have no checks that srcB is sufficiently long, so this potentially vulnerable to ArrayIndexOutOfBoundsExceptions. Also keep in mind that a 2D array in Java is really a 1D array where each element is itself a 1D array. As such those elements can be of varying sizes and your 2D array need not be square. As such, you might have thought you guaranteed the size of each of output in line 3, but you only guaranteed the width; the height could vary depending upon the size of arrays assigned to each position.

Michael McGowan