aspose file tools
The moose likes Java in General and the fly likes Fetching an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Fetching an ArrayList" Watch "Fetching an ArrayList" New topic
Author

Fetching an ArrayList

Nilofar Syed
Greenhorn

Joined: Oct 07, 2008
Posts: 6
If suppose I add an object in ArrayList :
Ex : If Obj is my object having variables an int, string and String[].

Obj ob = new Obj(1,"xyz", str[]);
List list = new ArrayList();
list.add(ob);

Now while fetching back the contents of arrayList I'm getting str[] as a second element , where it has to be part of first element.

String[] results;
results = (String[]) list.get(1);

results[1] --> 1,"xyz"
results[2] --> str[]

How do I avoid this? So that I get results[1] --> 1,"xyz",str[]
nandini lagunia
Ranch Hand

Joined: May 05, 2009
Posts: 57
ArrayList contains the reference of the object.
Now if you fetch the contents of the ArrayList you'd get object reference. There's no second or third element. Just invoke getter method on the reference and you'd get the value.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Nilofar Syed wrote:
Obj ob = new Obj(1,"xyz", str[]);
List list = new ArrayList();
list.add(ob);

Here you are adding Obj into list [only one element is there]

Nilofar Syed wrote:
String[] results;
results = (String[]) list.get(1);


you are trying to get 2nd element of list upon that your are casting Object to String !

good practice to be introduce generic here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to Javaranch Nilofar Syed .We are happy to have you here
Nilofar Syed
Greenhorn

Joined: Oct 07, 2008
Posts: 6
seetharaman venkatasamy wrote:
Nilofar Syed wrote:
Obj ob = new Obj(1,"xyz", str[]);
List list = new ArrayList();
list.add(ob);

Here you are adding Obj into list [only one element is there]

Nilofar Syed wrote:
String[] results;
results = (String[]) list.get(1);


you are trying to get 2nd element of list upon that your are casting Object to String !

good practice to be introduce generic here


Thanks a lot for reply....
Do you mean I'm trying to add only one element and trying to get second one? If Yes then that was a typing mistake...sorry
I just want to clarify that arraylist get the elements the way they were being added irrespective of casting...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Nilofar Syed wrote:I just want to clarify that arraylist get the elements the way they were being added


Correct
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 154
You can't cast Obj to String[]. Have you ever tried to compile your code? Where did you take those results from?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Fetching an ArrayList
 
Similar Threads
Iteration speed of Collections
MultiDimenaional Array sort
hello, can y tell me why??
ArrayList class and subList Method
ArrayList vs LinkedList?