• 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

Array of ArrayLists

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an array of arrayLists:
Focus on public ArrayList[] nodeInfo = new ArrayList[100];




Result:
[0, qw, 200, 20, 109, 647, 499]
[1, az, 350, 50, 133, 260, 259]
array of arraylist null
array of arraylist null


WHAT IS WRONG WITH MY CODE???
I WANT a array with individual elements storing an arraylist and which can be accessed within any other method.
please help!!!
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you do this:


What do you think nodeInfo[i] is giving you?

Hunter
 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think nodeInfo[i] will give me the nodeList saved at this ith index.
Am i wrong?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, so how can you access individual elements of a nodeList(i.e ArrayList)
 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But i need the individual elements of the Array i.e.every ArrayList stored in the Array as its elements and not the individual elements of the ArrayList.
Also, the elements of the Array are coming as null, so i guess, the individual elements of the ArrayList would also come as null, isnt it?
 
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rose Jac wrote:
Also, the elements of the Array are coming as null, so i guess, the individual elements of the ArrayList would also come as null, isnt it?



When the elements of Array are coming as null, that means there are no ArrayList objects, at that index.

Then why do you even think of individual elements of ArrayList.. ?
 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats what i said.
Any help on how i can actually do that?
Thanks!
 
Ranch Hand
Posts: 331
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rose Jac,

It would be better to change the code to not use an "array of ArrayList". An array of ArrayList ( or any collection within another collection ) makes the code difficult to read and maintain. Instead an ArrayList of POJOs would be more readable and maintainable.

It would look something like this :



This is called a "Data Transfer Object"


Now, you can retrieve from database like this

 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hI Vishwanath Krishnamurthi,

That sounds fairly simple though before implementing it, what i would like to know is : say, i have n number of records in the database, then would i be able to access all these records at once using DTO?

Thanks!
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't understand your question clearly. Typically you'd iterate through the recordset, create one DTO object corresponding to one row retrieved, and add that DTO to the ArrayList.
So If you have 100 rows retrieved by a query, then you'd have an ArrayList that contains 100 DTOs.

HTH,
Vishwa
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic