• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections  RSS feed

 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I receive two collections - units and subunits.
Units contain one or more of subunits.
Each SubUnit has a unitId to which it belongs to.

I need to paint them like

Unit 1 Unit 2 Unit3

SubUnit1 SubUnit1 Subunit1
SubUnit2 SubUnit2
SubUnit3

How do I loop through the subunit collection and put them in appropriate collections of units and then do the paiting as above?

Any help is greatly appreciated.

Thanks,
Vasu
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some pseudo-code if that helps.
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I thought of this but I do not know how many units and subsunits are coming. I do not know how to name the collections dynamically and then populate them.

I tried naming the collections with the unit name in the for loop but I didn't quite get how to do it.

Thanks,
Vasu
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific and post some code? For instance, where are you getting input?

Also, from what you have said so far, I think you might find LinkedLists helpful. Here is a brief overview.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got a Unit class which encapsulates the Subunits?
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the UnitDTOs and SubUnitDTOs come from resultset from a database call.
Unit class doesn't encompass SubUnit class but they are diff classes. Something like a buidling and room.

Thanks very much for your time.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A building encompasses rooms, so the one definitely could be a field of the other class.
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is how do I create the collection objects on the fly as I do not know how many I get.
I need to fill them with a collection of subunits, each of the subunit going to the unit that it belongs to ( it has a field called unitId).

So if I lood through the subunits in a switch as suggested, how do I know where to send them ( which collection? What should be the name).
If I want to call the collections as unit1Collection or UnitsOne or something similar where do I give them the names. I tried to use a String object and append it the name of the unit in the loop but it is not compiling...

Like in the for loop of unitDTOs...

List<UnitDTO> new String("Unit").append(unitId) = new someList...


Thanks,
Vasu
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here's some more pseudo code. I am making the assumption that Units also have a unitId that corresponds to the subunits unitId (if they don't, then you need to find some way of make the subunits unitId correspond to the units position in the linked list). So below I use parallel linked lists (ll), sort of. The first ll is of units. The second ll is a ll of lls of subunits. The index of the each unit in the first ll corresponds to the ll of its subunits in the second ll.Hope this helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!