基本上,我希望我的代码能够让用户输入 x 个整数(他们选择 x),然后他们会将每个输入的整数值存储在我的数组中。
出于某种原因,当我在输入所有值后打印出数组时:
我明白了:
代码:
Scanner input = new Scanner(System.in);
System.out.print("How many integers will you enter:");
int amount = input.nextInt();
int myArray[] = new int[amount];
for (int counter = 0; counter < amount; counter ++){
myArray[counter] = input.nextInt();
}
System.out.println(myArray);
控制台:
How many integers will you enter:2
4
2
[I@55f96302
最佳答案
你得到的字符串是数组的对象表示法,它是预期的[I@55f96302
where
[I
是类名[
一维数组I
整数数组
@
连接字符串55f96302
一些哈希码
要打印数组,请执行以下操作
System.out.println(Arrays.toString(myArray));
或者您可以简单地循环遍历数组的每个元素
关于java - 数组打印Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29520813/