gudii9
asked on
array to arraylist
Hi,
I am working on below program
I got gibberish output like
[I@74497449
How do i get meaningful output.
Is this is not the way i add array to array list and then display?
Please advise
I am working on below program
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Numbers {
public static void main(String[] args) {
List<int[]> arrayList = new ArrayList<int[]>();
int[] srtArray = {1,2,3,100,201,730};
arrayList.add(srtArray) ;
for (int i=0;i<arrayList.size();i++){
System.out.println(arrayList.get(i));
}
}
}
I got gibberish output like
[I@74497449
How do i get meaningful output.
Is this is not the way i add array to array list and then display?
Please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
why only one 1 reference for all bojcects in arraylist?
because you have only put one object in there.
viz :
arrayList.add(srtArray) ; //adds the array srtArray to the arrayList
srtArray being the object.
Does not it look the elements within the strArray list. please adviseNo it doesn't, as i explained in my last comment. You have to use the method i posted
There's a big difference between this:
List<int[]> arrayList = new ArrayList<int[]>();
where each element in the list is an array of items (very unusual to want this)
and this:
List<Integer> arrayList = new ArrayList<Integer>();
where each element in the list is a single integer value (you almost certainly want this).
(You can'd do List<int> arrayList because Java doesn't allow it - so you do List<Integer> instead).
In either case, if you print out the elements of the list, it works by taking one element at a time and printing it.
If the element of the list is an "array of integers" then Java prints the gibberish output.
You can see the same gibberish if you just do this:
int[] srtArray = {1,2,3,100,201,730};
System.out.println(strArra y) ; // This is what is stored in the list and Java doesn't know how to print arrays
Almost certainly what you want is a List<Integer>. Give the code I posted a try and see if it makes sense to you. Pretty sure your original code is going down a dead end that will leave you very confused.
Doug
List<int[]> arrayList = new ArrayList<int[]>();
where each element in the list is an array of items (very unusual to want this)
and this:
List<Integer> arrayList = new ArrayList<Integer>();
where each element in the list is a single integer value (you almost certainly want this).
(You can'd do List<int> arrayList because Java doesn't allow it - so you do List<Integer> instead).
In either case, if you print out the elements of the list, it works by taking one element at a time and printing it.
If the element of the list is an "array of integers" then Java prints the gibberish output.
You can see the same gibberish if you just do this:
int[] srtArray = {1,2,3,100,201,730};
System.out.println(strArra
Almost certainly what you want is a List<Integer>. Give the code I posted a try and see if it makes sense to you. Pretty sure your original code is going down a dead end that will leave you very confused.
Doug
ASKER
(You can'd do List<int> arrayList because Java doesn't allow it - so you do List<Integer> instead).
i wonder why java does not allow. What is speciality of Integer compared to int. Is it is because int is not object type where as Integer is object type and list expects only object type but not primitive?
Is it is because int is not object type where as Integer is object type and list expects only object type but not primitive?
Yes that's exactly it. A list has to be a list of objects. It can't be a list of primitives.
Doug
Correct
ASKER
I ran as below
I got output as
[1, 2, 3, 100, 201, 730]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Numbers {
public static void main(String[] args) {
List<int[]> arrayList = new ArrayList<int[]>();
int[] srtArray = {1,2,3,100,201,730};
arrayList.add(srtArray) ;
for (int i=0;i<arrayList.size();i++){
//System.out.println(arrayList.get(i));
System.out.println(java.util.Arrays.toString(arrayList.get(i)));
}
}
}
I got output as
[1, 2, 3, 100, 201, 730]
I got output as
[1, 2, 3, 100, 201, 730]
. . . still not what you were expecting, then?
:)
ASKER
List<int> i am using is instead arraylist of primitive not wrapper type right?
Open in new window
why only one 1 reference for all bojcects in arraylist?
Does not it look the elements within the strArray list. please advise