views:

53

answers:

5

I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:

public class MyClass {

 public static void main(String args[])
    {
  int[][] test; 
  test = new int[5][10];

  int row = test.length;
  int col = test[0].length;

  System.out.println(row);
  System.out.println(col);
    }
}

This prints out 5, 10 as expected.

Now take a look at this line:

  int col = test[0].length;

Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:

test = new int[0][10];

Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?

+2  A: 

Consider

public static void main(String[] args) {

    int[][] foo = new int[][] {
        new int[] { 1, 2, 3 },
        new int[] { 1, 2, 3, 4},
    };

    System.out.println(foo.length); //2
    System.out.println(foo[0].length); //3
    System.out.println(foo[1].length); //4
}

Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.

SB
Unrelated question about your answer. What is the technique/method called where you put "{...};" after the object definition. As a new developer I keep seeing this more and more.
it's a way to initialize an array with values. http://www.janeg.ca/scjp/lang/arrays.html
SB
Well, I understand that much :). I just thought there might be a specific name for the technique.
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
SB
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
ILMTitan
+1  A: 

Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:

if (row > 0) col = test[0].length;
RD
A: 

You would need to iterate over the array using:

int row = test.length;
int length = 0;
for (int i = 0; i < row; i++)
{
    length += test[i].length;
}

Now, length is the count of the elements in the entire array.

Evan Mulawski
I think you misunderstood the question. OP not looking for total number of elements in the array. Rather, length of 2nd dimension assuming rectangular array.
kaliatech
I apologize for misunderstanding. The term "length" in the question is ambiguous.
Evan Mulawski
+1  A: 

There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.

You could easy create your own class to abstract the functionality you need.

If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.

kaliatech
+1  A: 

A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.

import java.util.Arrays;

public class Main {
  public static void main(String args[]) {

    int[][] test; 
    test = new int[5][];//'2D array'
    for (int i=0;i<test.length;i++)
      test[i] = new int[i];

    System.out.println(Arrays.deepToString(test));

    Object[] test2; 
    test2 = new Object[5];//array of objects
    for (int i=0;i<test2.length;i++)
      test2[i] = new int[i];//array is a object too

    System.out.println(Arrays.deepToString(test2));
  }
}

Outputs

[[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]

The arrays test and test2 are (more or less) the same.

Ishtar