• 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

Looping through a set of data

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above result is returned in a resultset. What i would like to achieve is store these rows in an object that is structured as shown below



Now as you can see in the data, some of the rows belong to the same element. For example, all those with seq=6 should be stored in the same object because they below to the same group i.e. 6.

I tried several combinations of approaches but i always seem to either miss the last one, duplicate them or exhaust the resultset.

Here are some examples of what i tried to do.

Example 1 - This example copies every name into every element.



Example 2 - In this example i try to save the child elements only if the seq number is the same but i still get duplicates.


I also tried an inner while loop with an inner rs.next() but that exhausts the resultset. Is there a better way to do this? Thansk in advanc.e
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use ctrl-c ctrl-v to post code, otherwise we might think the spelling errors are of any significance.

Why are you using a String for seq_no rather than an int?

Where do you update seq_no when it changes in the ResultSet? I can't see that.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sorry my mistake,


seq_no is updated in the while loop. This was just a quick example i wrote to give an idea as to what i was trying to achieve so there are likely some syntax error in it.

 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Sorry for not being clear enough. The above was just a quick example to try and show what i am trying to achieve.

What i am trying to achieve is to store the data in objects. The data itself has a one to many relationship. This means that many names will be related to the same sequence number. I would like to store the common names (i.e. those with the same seq no) in the same object in an arraylist.

Maybe this data structure makes more sense



What im hoping to achieve is the following

- The seq object will contain one seq row (either 1,2,3,4,5,6)
- Array list in the seq object should contain all the rows for each individual seq.
- As an example if seq[3].seq = 6, the seq.namePcs() should contain all NamePcs objects tha t have id's (30,31,32,33).

Here is what i am hoping to achieve as the end result (for the first 6 rows)



I hope that made it a bit clear.


Thanks
[ September 23, 2008: Message edited by: O. Ziggy ]
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Name class is badly designed if it has public fields and a field called almost the same as the class.

You are going to have to get the seq_no into an int, then when you loop, if (seq_no > seq.size()) you can add a new object to your larger list.
Then for each line you can try seq.get(seq_no - 1).addToList(new NamePcs(id, total));

And you ought not to use plain simple ArrayList; it ought to be ArrayList<NamePcs>.

I have assumed your seq_no starts at 1; you will need to change the logic if it doesn't start at 1 or if there are gaps in the sequence.

[edit]Change nameList to seq to match your posting.[/edit]
[ September 23, 2008: Message edited by: Campbell Ritchie ]
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry mate for not being clear enough

- The sequence number is not in any order
- None of the values need converting into ints as i just want to display them. I dont want to process any of the values.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a Map as intermediate storage for lookups:

Also note how I use constructors and methods to hide my fields.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for all your help i have now managed to get the result i want using the Hashmap.

The only problem i have now is that i've lost the sort order that was returned by the original query. Is there a way i can sort the key or the values?

When i've got all the values into a map, i populate the arraylist as shown below. Can i instead sort the arraylist?



Again thanks all for your help.

Edited by: ziggy on Sep 23, 2008 11:01 AM
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a version of Map which retains insertion order; I think it's LinkedHashMap. That might be suitable.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help. I managed to use the Comparable Interface to sort the map.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic