• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to split a list into 3 lists

 
Steph Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me out in this?

What is your problem ? Where are you stuck ?
 
Steph Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic