wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Compare multiple linked list sizes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compare multiple linked list sizes" Watch "Compare multiple linked list sizes" New topic
Author

Compare multiple linked list sizes

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have a number of linked lists, currently 5, each modelling a checkout line. But in the future i may choose to ad more
I want to compare each linked list and return the linked list with the smallest size
What is the best/easiest way to do this?
Could i make a list of linked lists, or an array of linked lists and iterate through these comparing the sizes?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You could either define a class, say, CheckoutGroup or something like that, that includes all the checkout lanes, plus whatever other administrative stuff makes sense.

Or, if you're not doing anything with them as a group other than finding out which one has the shortest line, just define a method that takes List of Lists and finds the shortest one.

And yes, you can make an array of Lists, List of Lists, List of arrays, array of arrays of List of arrays of Lists, and so on. Lists and arrays are objects, and they can be stored in a List or array just like any other object. I would lean toward a List rather than an array, since it will auto-resize itself as needed.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And of course, rather than asking "Can I do this?" since you already have some idea about Lists and arrays and an idea of how to use them, you could have first tried it before posting.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I wanted to see if there was a preferred way to do it.


I was thinking something along the lines of this, although i was hoping .get(i).size() would retrieve the size of the LinkedList, instead it looks for a method size(). Is there a way to retrieve the linkedList size the way i have started above
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You've got two problems that I can see:

1) You're not using generics.

2) You're not adding your individual lists to the main list. Instead, you're adding each of their items.



You want a list of checkout lanes, and you want to look at the size of each element in that list--that is, the number of people in the checkout lanes. So you need to add the checkout lanes to that list. But you're not doing that. You're adding the people in the individual lanes into that bigger list, with no regard for the "This lane contains these people" structure. If you had used generics, this would not have been possible, as the compiler would let you add a Person to a list that's supposed to contain List of Person.

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have worked it out now thanks, it now adds a customer to the queue with the least customers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compare multiple linked list sizes
 
Similar Threads
Recursive File List - Help me problem solve please
Formulas needed for calculating statistic using hashing.
oldskool linked list
advantage Doublylink list over linklist
Find where the loop is in a doubly - linked list