This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Code to get deta from ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Code to get deta from ArrayList" Watch "Code to get deta from ArrayList" New topic
Author

Code to get deta from ArrayList

Krishna Tota
Ranch Hand

Joined: Mar 22, 2008
Posts: 40
Hi,
I know that ArrayList can hold any type of Objects because both the arguments of the ArrayList add() and the return value of the get() are of type Object.

And we want to retrive the rlements we need to cast the eements in to particular type if we know.

Example:

ArrayList list=new ArrayList();
list.add(new Integer(5));
Integer i=(integer)list.get(0);


Now if we don't know the type then what is the code.

Please give the code in brief.

Thanks,
Krishna
Steve McLeod
Greenhorn

Joined: May 26, 2008
Posts: 11
Your question is a bit vague. Here's 2 answers depending on what you want:

Answer 1: Use an object of type Object

ArrayList list=new ArrayList();
list.add(new Integer(5));
Object obj=list.get(0);


Answer 2: Check the type before casting it

Example:

ArrayList list=new ArrayList();
list.add(new Integer(5));
Object obj=list.get(0);
if (obj instanceof Integer) {
Integer i=(Integer)obj;
}


<a href="http://www.solidsimplesafe.com/" target="_blank" rel="nofollow">http://www.solidsimplesafe.com/</a>
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
If you did not know the type, then you would do something like this:


ArrayList list=new ArrayList();
list.add(new Integer(5));
Object x = list.get(0);
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Krishna Tota:
Now if we don't know the type then what is the code.


If you don't know the type, what are you going to do with the object?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Prakash Subramanian
Ranch Hand

Joined: Feb 03, 2005
Posts: 32
Originally posted by Ilja Preuss:


If you don't know the type, what are you going to do with the object?


Bang
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code to get deta from ArrayList
 
Similar Threads
map is an ArrayList, compiler knows that Object is superclass of ArrayList, yet does nothing?
Inferring type in call to generic method: how does it work?
Generics and Legacy code
Generics using <? extends A>,<? super A> Problem
Compile fails (generics/collections)