aspose file tools*
The moose likes Beginning Java and the fly likes How to split a list into 3 lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to split a list into 3 lists" Watch "How to split a list into 3 lists" New topic
Author

How to split a list into 3 lists

Steph Sam
Greenhorn

Joined: Dec 09, 2007
Posts: 15
Hi,
I have a list of objects. I need to equally split the objects and create 3 lists. Say for example in my first list i have 8 objects. i have to equally split the objects as 3,3,2. The number of objects will be random. so i need some kind of code to crate the list dynamically. Can anyone help me out in this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
We don't simply hand out code like that; please tell us what you have so far, or write the algorithm down and let's see if we can turn that into code.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Can anyone help me out in this?

What is your problem ? Where are you stuck ?


[My Blog]
All roads lead to JavaRanch
Steph Sam
Greenhorn

Joined: Dec 09, 2007
Posts: 15
Thanks for your immediate responses. My requirement is this. I have a page where i'll display the list of applications to the user in 3 columns. I have the application object arraylist. how will i equally split the objects across the 3 columns.

If i have 9 applications, i can divide it by 3 and display them as 3 per each column. What will i have to do if my list has 8 objects.

This is my code:
int size = filteredAppLst.size();

int column1=0;
int column2=0;
int val = size%3;
int checkValue = 0;
if(val == 0){
checkValue = size/3;
}else{
checkValue = size%3;
}
for (int i = 0; i < filteredAppLst.size(); i++) {
Application app = (Application) filteredAppLst.get(i);
String name = app.getName().toLowerCase();
if(column1 != checkValue){
column1Lst.add(app);
column1++;
}else if(column2 != checkValue){
column2Lst.add(app);
column2++;
}else{
column3Lst.add(app);
}
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
That looks promising, but would have been easier to read with the CODE button. You don't need all those checks when you do the division by 3; you can do count / 3 three times, then if (count % 3 == 1) column 1 is incremented, etc.
Steph Sam
Greenhorn

Joined: Dec 09, 2007
Posts: 15
how will count/3 three times will make my lists to hold equal number of objects? can you please provide a sample code snippet?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

how will count/3 three times will make my lists to hold equal number of objects? can you please provide a sample code snippet?


I think you need to trust your code a bit more than that -- being willing to just dump what you did for sample code from a stranger isn't good.

Some of your lists will hold (count/3) items. Some of your lists will hold (count/3 + 1) items. Now the trick is to figure out how many holds which.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
Henry Wong wrote: . . . Now the trick is to figure out how many holds which.

Henry
I thought I had already said how to do that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to split a list into 3 lists