• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fetching an ArrayList

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to Javaranch Nilofar Syed .We are happy to have you here
 
Nilofar Syed
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Correct
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't cast Obj to String[]. Have you ever tried to compile your code? Where did you take those results from?
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic