[英]Fill a list of arrays java
I wanted to fill a list of arrays in Java I tried this in order to practice a little bit:我想用 Java 填充一个数组列表我尝试了这个以练习一点:
ArrayList <int []> temp = new ArrayList <int []>();
for (int a = 1; a < 4 ; a++) {
int [] array = new int [8];
for (int i = 0; i < 8; i++) {
array [i] = i*a;
}
System.out.println(array);
temp.add(array);
}
System.out.println(temp);
This is the output I got这是我得到的输出
[[I@15db9742, [I@6d06d69c, [I@7852e922]
Please, why do i have this strange result ?请问,为什么我有这个奇怪的结果?
temp
is defined as ArrayList <int []>
, that is, a list of arrays. temp
定义为ArrayList <int []>
,即数组列表。 In Java, when you print something, eg. 在Java中,当您打印某些内容时,例如 by calling
System.out.println(temp)
, the object in question is converted to a string by an implicit call to toString()
. 通过调用
System.out.println(temp)
,可以通过隐式调用toString()
将有关对象转换为字符串。 ArrayList
overrides toString()
and prints each of its elements in turn. ArrayList
重写toString()
并依次打印其每个元素。 For each element toString()
is also called. 对于每个元素,还调用
toString()
。 However there is no toString()
defined for int[]
so you simply get the object reference, ie. 但是,没有为
int[]
定义toString()
,因此您只需获取对象引用即可。 the funny-looking string [I@6d06d69c
. 看起来很有趣的字符串
[I@6d06d69c
。 To print the value of temp
in a meaningful fashion, you need to convert each int[]
to something human-readable. 要以有意义的方式打印
temp
的值,您需要将每个int[]
转换为人类可读的内容。 The easiest way to do this is to make use of java.util.Arrays.toString()
. 最简单的方法是利用
java.util.Arrays.toString()
。
So you could try something like: 因此,您可以尝试执行以下操作:
for (int[] element : temp) {
System.out.println(Arrays.toString(element));
}
You are printing an ArrayList. 您正在打印一个ArrayList。 Try using a loop to print or save your results.
尝试使用循环来打印或保存结果。
For example: 例如:
for(int i=0; i<ArrayList.size(); i++){
System.out.println(ArrayList.get(i[i]));
}
You aren't supposed to directly print an arraylist. 您不应该直接打印一个数组列表。
When an array is printed its reference is shown, not its elements. 打印数组时,将显示其引用,而不显示其元素。 Try replacing the array with another
ArrayList
. 尝试将数组替换为另一个
ArrayList
。
What you are doing is that you are printing the Object class name representation then @ followed by Hashcode. 您正在做的是先打印对象类名称表示形式,然后打印@,然后打印Hashcode。
Your Output: 您的输出:
[I@15db9742, [I@6d06d69c, [I@7852e922]
[I @ 15db9742,[I @ 6d06d69c,[I @ 7852e922]
I
is the Object Class representation I
是对象类的表示形式
Numbers followed by @
are Hashcode. Numbers followed by @
是哈希码。
So, when you call this System.out.println(temp);
因此,当您调用此
System.out.println(temp);
, you are printing default representation of array
. 您正在打印
array
默认表示形式。 To get something meaningful and data from it you need to do like below: 要获得有意义的东西和数据,您需要执行以下操作:
System.out.println(java.util.Arrays.toString(temp));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.