File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Looping through a set of data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looping through a set of data" Watch "Looping through a set of data" New topic
Author

Looping through a set of data

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430



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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Oct 02, 2005
Posts: 430

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

Joined: Oct 02, 2005
Posts: 430

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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Oct 02, 2005
Posts: 430

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Use a Map as intermediate storage for lookups:

Also note how I use constructors and methods to hide my fields.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
There is a version of Map which retains insertion order; I think it's LinkedHashMap. That might be suitable.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Thank you all for your help. I managed to use the Comparable Interface to sort the map.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You're welcome
 
 
subject: Looping through a set of data
 
Similar Threads
No ResultSet set was produced
how to improve my program code ?
Returning a 0 value error
Nested Record sets and Joins
How to retrieve the first record in a database table