Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

array to arraylist

Posted on 2014-07-24
13
Medium Priority
?
232 Views
Last Modified: 2014-07-27
Hi,

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));
}	

}
}	

Open in new window


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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 668 total points
ID: 40218116
System.out.println(java.util.Arrays.toString(arrayList.get(i)));

Open in new window


(arrays don't override Object.toString)
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 668 total points
ID: 40218284
List<int[]> arrayList = new ArrayList<int[]>(); // declares an ArrayList of int arrays called arrayList

int[] srtArray = {1,2,3,100,201,730}; //declares and initialises an int array called srtArray

arrayList.add(srtArray) ; //adds the array srtArray to the arrayList

for (int i=0;i<arrayList.size();i++){
System.out.println(arrayList.get(i)); /* prints out the object reference for all objects in arrayList, of which there is only 1, namely srtArray. Hence your 'gobbledegook' object handle ref.*/
}
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 664 total points
ID: 40218289
Also I'm guessing you don't really want a List of arrays:
    List<int[]> arrayList = new ArrayList<int[]>();

but a list of Integers:
    List<Integer> arrayList = new ArrayList<Integer>();

In which case your code should be:

List<Integer> arrayList = new ArrayList<Integer>();
int[] srtArray = {1,2,3,100,201,730};
for (int j = 0 ; j < srtArray.length ; j++) {
   arrayList.add(srtArray[j]) ;
}

The good news is that if you have a List<Integer> then you don't need to do this:
for (int j=0;j<arrayList.size();j++){
System.out.println(arrayList.get(j));
}

to print them out, you can just do this:

System.out.println(arrayList) ;

Doug
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Author Comment

by:gudii9
ID: 40219207
The good news is that if you have a List<Integer> then you don't need to do this:
for (int j=0;j<arrayList.size();j++){
System.out.println(arrayList.get(j));
}

to print them out, you can just do this:

System.out.println(arrayList) ;

List<int> i am using is instead arraylist of primitive not wrapper type right?



for (int i=0;i<arrayList.size();i++){
System.out.println(arrayList.get(i)); /* prints out the object reference for all objects in arrayList, of which there is only 1, namely srtArray. Hence your 'gobbledegook' object handle ref.*/
}

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
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40219220
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 

Open in new window


srtArray being the object.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40219259
Does not it look the elements within the strArray list. please advise
No it doesn't, as i explained in my last comment. You have to use the method i posted
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40219685
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(strArray) ;   // 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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40220469
(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?
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40220509
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40220510
Correct
0
 
LVL 7

Author Comment

by:gudii9
ID: 40222109
I ran as below

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)));
}	

}
}	

Open in new window


I got output as
[1, 2, 3, 100, 201, 730]
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40222373
I got output as
 [1, 2, 3, 100, 201, 730]

. . .  still not what you were expecting, then?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40222473
:)
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question