Java:简单数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7548381/
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
Java: Simple Arrays
提问by Michael
I'm making this array where the first number in the array should be 15 and the third as well. Then I need to print the array on the screen but I get an error when I do this, I've read that I got to write a loop when printing an array. How's that possible?
我正在制作这个数组,其中数组中的第一个数字应该是 15,第三个也是。然后我需要在屏幕上打印数组,但是执行此操作时出现错误,我读到打印数组时必须编写循环。这怎么可能?
This is my current code.
这是我目前的代码。
int[] i = {15,0,15,0,0};
System.out.println(i);
And what's the difference in using this method or using
使用这种方法或使用有什么区别
int [] i = new int [5];
Thanks in advance,
提前致谢,
Michael.
迈克尔。
回答by Oleg Pavliv
To print an array use Arrays.toString()
;
打印一个数组使用Arrays.toString()
;
import java.util.Arrays;
System.out.println(Arrays.toString(i));
// or print it in the loop
for(int e : i) {
System.out.print(e);
}
About differences between two methods:
关于两种方法的区别:
int [] i = new int [5]; // five evements are allocated
// the number of elements are determined by the initialization block
int[] i = {15,0,15,0,0};
回答by Cory Kendall
You could write a loop like this:
你可以写一个这样的循环:
for(int j=0; j < i.length; j++) {
System.out.println("Value at index " + j + ": " + i[j]");
}
回答by Bohemian
It's considered a "mistake" in java that there's no implementation for toString() - you get java.lang.Object implementation.
在 java 中没有实现 toString() 被认为是一个“错误”——你得到了 java.lang.Object 实现。
Instead, you must use the static method Arrays.toString(array).
相反,您必须使用静态方法Arrays.toString(array)。
Writing this int [] i = new int [5];
allocatesmemory for 5 elements, but they are all intitialized to zero (0
). You would have to write more code to assign values to the elements.
编写此代码会为 5 个元素int [] i = new int [5];
分配内存,但它们都被初始化为零 ( 0
)。您将不得不编写更多代码来为元素赋值。
回答by Johan Sj?berg
That code executes just fine, although it's probably not the string you expect as the default value for toString()
(which is what gets executed) is defined as:
该代码执行得很好,尽管它可能不是您期望的字符串,因为默认值toString()
(即执行的内容)定义为:
getClass().getName() + '@' + Integer.toHexString(hashCode())
To print the actual contents of the string you should employ the method suggested by e.g., @Oleg.
要打印字符串的实际内容,您应该使用例如@Oleg 建议的方法。
The statement int[] i = {15,0,15,0,0};
is just shorthand for the more verbose
该语句int[] i = {15,0,15,0,0};
只是更详细的简写
int [] i = new int [5];
i[0] = 15;
i[1] = 0;
i[2] = 15;
i[3] = 0;
i[4] = 0;
回答by Vineet G
Either you could print like this:
您可以像这样打印:
for (int index=0; index<i.length; index++)
System.out.println("Array's value at index " + index + "is: " + i[index] );
Or you can use toString() function.
或者您可以使用 toString() 函数。
回答by user3386157
The difference between the two is that
两者的区别在于
int[] i = {15,0,15,0,0};
System.out.println(i);
the elements are determined by a initialization block , while
元素由初始化块确定,而
int [] i = new int [5];
five elements are allocated in the int i
.
中分配了五个元素int i
。