在 Java 中的一个 println 语句中打印数组的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27681491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing all elements of an array in one println statement in Java
提问by Furkan
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?
我预计会出现编译或运行时错误。我得到了一个输出。它是“ [I@1ba4806
”。它在 Java 中的原因是什么?
回答by August
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
):
这就是你看到的toString()
in的默认实现Object
。您可以使用Arrays.toString
可读的结果(确保导入java.util.Arrays
):
System.out.println(Arrays.toString(a));
回答by Emanuel S
It trys to print an Array.
它尝试打印一个数组。
If you want to get a readable result use
如果您想获得可读的结果,请使用
System.out.println(Arrays.toString(a));
回答by sol4me
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?
预计会出现编译或运行时错误。我得到了一个输出。它是“[I@1ba4806”。它在 Java 中的原因是什么?
Because currently the toStringmethod from Object class is getting invoked, it looks like this
因为当前正在调用 Object 类的toString方法,它看起来像这样
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Thats why you see [I@1ba4806
这就是为什么你看到 [I@1ba4806
You can print array content using Arrays.toString
which is overloaded method in Arraysclass to print the array.
您可以使用Arrays类中的Arrays.toString
重载方法打印数组内容以打印数组。
System.out.println(Arrays.toString(a));
For int[]
parameters the method implementation looks like
对于int[]
参数,方法实现看起来像
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(", ");
}
}
回答by Pshemo
I took an output.It's "
[I@1ba4806
".What's the reason of it in Java?
我拿了一个输出。它是“
[I@1ba4806
”。它在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 convertpassed object o
into as String which will be then printed.
out
System
类中的静态字段是实例,PrintStream
并且类中没有println(int[])
方法PrintStream
可以遍历传递数组中的所有元素并打印它们,但是有一个方法println(Object o)
会尝试将传递的对象o
转换为字符串,然后将其打印出来。
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()
.
为此,此方法使用String.valueOf(o)
方法,如果为o
null,将返回“null”字符串,或者在o
不为 null(如您的示例中)的情况下,它将返回toString()
从传递的对象调用的方法的结果。所以System.out.println(a);
打印结果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
现在为什么数组有会有toString()
方法呢?那么它是从Object
类继承的(因为所有数组都扩展了对象)。数组也不会覆盖这个方法,它们以Object
类中实现的方式保留它,所以它的代码看起来像
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.
如您所见,返回TypeName@hexadecimalHash
. 一维整数数组表示为[I
其中数量[
决定维数,并I
表示int
类型。
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
或者您可以使用Arrays.toString(int[])
方法,它会为您执行此操作并以表格形式生成字符串,[1, 2, 3, ....]
因此您需要做的就是像这样打印
System.out.println(Arrays.toString(a));