Printing all elements of an array in one println statement in Java [duplicate]

孤街醉人 提交于 2021-02-16 23:56:37

问题


public class Test {

    public static void main(String[] args) {

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

        System.out.println(a);


    }
}

I expected to take a compile or run-time error.I took an output.It's "[I@1ba4806".What's the reason of it in Java?


回答1:


That's the default implementation of toString() in Object you're seeing. You can use Arrays.toString for a readable result (make sure to import java.util.Arrays):

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



回答2:


It trys to print an Array.

If you want to get a readable result use

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



回答3:


expected to take a compile or run-time error.I took an output.It's "[I@1ba4806".What's the reason of it in Java?

Because currently the toString method from Object class is getting invoked, it looks like this

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Thats why you see [I@1ba4806

You can print array content using Arrays.toString which is overloaded method in Arrays class to print the array.

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

For int[] parameters the method implementation looks like

public static String toString(int[] a) {
    if (a == null)
        return "null";
    int iMax = a.length - 1;
    if (iMax == -1)
        return "[]";

    StringBuilder b = new StringBuilder();
    b.append('[');
    for (int i = 0; ; i++) {
        b.append(a[i]);
        if (i == iMax)
            return b.append(']').toString();
        b.append(", ");
    }
}



回答4:


I took an output.It's "[I@1ba4806".What's the reason of it in Java?

out static field in System class is instance of PrintStream and there is no println(int[]) method in PrintStream class which would iterate for you over all elements in passed array and print them, but there is println(Object o) which tries to convert passed object o into as String which will be then printed.

To do that this method uses String.valueOf(o) method which in case of o being null will return "null" String, or in case where o is not null (like in your example) it will return result of toString() method invoked from passed object. So System.out.println(a); prints result of a.toString().

Now why does array have would have toString() method? Well it was inherited from Object class (because all arrays extend Object). Also arrays do not override this method and they leave it in a way it was implemented in Object class, so its code looks like

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

which as you see returns TypeName@hexadecimalHash. One dimensional array of integers is represented as [I where number of [ determines number of dimensions, and I represents int type.


If you would like to print content of array, you would either have to iterate over all elements and print them

for (int i = 0; int < array.lenght; i++){
    System.out.println(array[i]);
}

or

for (int element : array){
    System.out.println(element);
}

OR you can use Arrays.toString(int[]) method, which will do this for you and will generate string in form [1, 2, 3, ....] so all you would need to do is print it like

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


来源:https://stackoverflow.com/questions/27681491/printing-all-elements-of-an-array-in-one-println-statement-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!