How to display a multidimensional array without using a loop?

5

When trying to execute the following snippet:

int[] vetor = {1, 2, 3, 4, 5, 6};

System.out.println(Arrays.toString(vetor));

The array is normally displayed as [1, 2, 3, 4, 5, 6] but if I try with a two-dimensional array, as follows:

int[][] matriz = {{1, 2, 3, 4, 5, 6}, {8, 10, 12, 14}};

System.out.println(Arrays.toString(matriz));

The result is [[I@7852e922, [I@4e25154f] and the array values are not displayed.

Is there a way to display a direct two-dimensional array in System.out.println without having to loop or overwrite methods, as occurred in code with the simple array?

    
asked by anonymous 14.01.2017 / 14:28

3 answers

5

Using codes both forEach and lambda , as well as the deepToString method of Arrays use foreach and for respectively internally in their codes, with no other way of displaying array (simple or multidimensional) without the loop happening. Concluding that without a loop , you are not able to display in a text the data contained in a array , and the classes codes can encapsulate this to be transparent to the developer, hiding (omitting) the reality of the code.

Codes:

  

forEach with lambda

@Override
public void forEach(Consumer<? super E> action) {
    Objects.requireNonNull(action);
    for (E e : a) {
        action.accept(e);
    }
}
  

deepToString

public static String deepToString(Object[] a) {
    if (a == null)
        return "null";

    int bufLen = 20 * a.length;
    if (a.length != 0 && bufLen <= 0)
        bufLen = Integer.MAX_VALUE;
    StringBuilder buf = new StringBuilder(bufLen);
    deepToString(a, buf, new HashSet<Object[]>());
    return buf.toString();
}

private static void deepToString(Object[] a, StringBuilder buf,
                                 Set<Object[]> dejaVu) {
    if (a == null) {
        buf.append("null");
        return;
    }
    int iMax = a.length - 1;
    if (iMax == -1) {
        buf.append("[]");
        return;
    }

    dejaVu.add(a);
    buf.append('[');
    for (int i = 0; ; i++) {

        Object element = a[i];
        if (element == null) {
            buf.append("null");
        } else {
            Class<?> eClass = element.getClass();

            if (eClass.isArray()) {
                if (eClass == byte[].class)
                    buf.append(toString((byte[]) element));
                else if (eClass == short[].class)
                    buf.append(toString((short[]) element));
                else if (eClass == int[].class)
                    buf.append(toString((int[]) element));
                else if (eClass == long[].class)
                    buf.append(toString((long[]) element));
                else if (eClass == char[].class)
                    buf.append(toString((char[]) element));
                else if (eClass == float[].class)
                    buf.append(toString((float[]) element));
                else if (eClass == double[].class)
                    buf.append(toString((double[]) element));
                else if (eClass == boolean[].class)
                    buf.append(toString((boolean[]) element));
                else { // element is an array of object references
                    if (dejaVu.contains(element))
                        buf.append("[...]");
                    else
                        deepToString((Object[])element, buf, dejaVu);
                }
            } else {  // element is non-null and not an array
                buf.append(element.toString());
            }
        }
        if (i == iMax)
            break;
        buf.append(", ");
    }
    buf.append(']');
    dejaVu.remove(a);
}

The example I step would be Java 8, with lambda :

int[][] matriz = {{1, 2, 3, 4, 5, 6}, {8, 10, 12, 14}};
Arrays.asList(matriz).forEach((i) -> { System.out.println(Arrays.toString(i)); });

Example Online

Reference:

14.01.2017 / 14:45
9

Just use the deepToString(Object[] a) method;

int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(Arrays.deepToString(array));

Output:

  

[1, 2, 3], [4, 5, 6], [7,8,9]]

    
14.01.2017 / 14:48
1

You should refer to the first array array, and the position you want, and so on.

System.out.println(matriz[0][0]);//saida 1  
System.out.println(matriz[0][1]); //saida 2  
System.out.println(matriz[0][2]); //saida 3 
    
15.01.2017 / 21:39