Total amount of elements in two dimensional array

12,030

Solution 1

No there is no native Java functionality for this, as an array of arrays (of arrays (of arrays ...)) is not a language construct per se. Array is, but an array of arrays is just an array of Objects (which again may, but need not be arrays). The notation Object[][] just provides type-safety for multi-dimensional arrays.

There is no way you can calculate this without looping, but you can solve it using reflection and recursion for arrays of arbitrary dimensions.


A generic solution, where performance is not an issue, that counts all non-array elements for arbitrary dimensional arrays using recursion and java.lang.reflect.Array could look something like this:

public static int size(Object object) {
    if (!object.getClass().isArray()) {
        return 1;
    }

    int size = 0;
    for (int i = 0; i < Array.getLength(object); i++) {
        size += size(Array.get(object, i));
    }
    return size;
}

You can call this function with any object:

int[][] matrix = {
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 }
};


Object[] array = {
    "1",
    new String[]{
        "2", "3", "4"
    },
    new int[][] {
        { 5 },
        { 6, 7 },
        { 8, 9, 10 }
    }
};


String literal = "literal";

System.out.println(size(matrix));
System.out.println(size(array));
System.out.println(size(literal));

would then output

9
10
1

It's not a very elegant solution, but as polygenelubricants puts it:

It's going to be very repetitive (but even java.util.Arrays is very repetitive), but that's the way it is in Java with arrays.

Solution 2

Nope. There's not really a concept of a 2d array in Java (C# has it though) - what you have there is actually an array of arrays. And since each "internal" array can have a different length, it's hard to do without a loop.

Is the loop really an issue, or are you just looking for a more "built-in" solution?

Share:
12,030
aka
Author by

aka

Updated on October 25, 2022

Comments

  • aka
    aka over 1 year

    Recently we learned two dimensional arrays and solved task of calculating average of all elements in it. My code was like:

    int a[][] = {
      {1, 2, 3, 4, 5},
      {6, 4, 2, 7},
      {3, 6},
      {2, 6, 8},
    };
    
    int sum=0, amount=0;
    for (int[] row : a)
      for (int val : row) {
        sum += val; amount += 1;
      }
    
    return sum / (double) amount;
    

    The thing is that I don't like the way I calculated the amount of elements in array. I tried to use size(), it not worked, tried to use Array and Arrays classes but both can retrieve neither amount of rows nor amount of elements in some row, just as a .length property.

    Question: is there any way of retrieving the amount of elements from two or more dimensional matrices without using loops?

  • aka
    aka over 10 years
    Yes, that's a good one, to look through dimensions recursively, I will remember that, thanks. Also, I searched other ways for these two days and didn't found any, but understand why it is not worthy to implement such methods in the case of java.
  • Robert
    Robert over 4 years
    ...and what if the input array has only 1 array? Or 20?