I wanted to fill a list of arrays in Java I tried this in order to practice a little bit:
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. In Java, when you print something, eg. by calling System.out.println(temp)
, the object in question is converted to a string by an implicit call to toString()
. ArrayList
overrides toString()
and prints each of its elements in turn. For each element toString()
is also called. However there is no toString()
defined for int[]
so you simply get the object reference, ie. the funny-looking string [I@6d06d69c
. To print the value of temp
in a meaningful fashion, you need to convert each int[]
to something human-readable. The easiest way to do this is to make use of 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. 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
.
What you are doing is that you are printing the Object class name representation then @ followed by Hashcode.
Your Output:
[I@15db9742, [I@6d06d69c, [I@7852e922]
I
is the Object Class representation
Numbers followed by @
are Hashcode.
So, when you call this System.out.println(temp);
, you are printing default representation of array
. To get something meaningful and data from it you need to do like below:
System.out.println(java.util.Arrays.toString(temp));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.