• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to split a list into 3 lists

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic