• Post Reply Bookmark Topic Watch Topic
  • New Topic

Divide Arraylist into several parts  RSS feed

 
Siana Ivanova
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an ArrayList of similar Strings.

Now I have to partition the lists into multiple lists with several parts.

Here is the sample data set

How can i make the size of sublists several?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siana Ivanova wrote:How can i make the size of sublists several?

Well, assuming you want to return them all at the same time, how about a List?

There's nothing to stop you storing a List as each element of a List, and yours would then be declared as
  List<List<String>> ...

HIH

Winston
 
Siana Ivanova
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the size of sublists is dynamic.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siana Ivanova wrote:I want the size of sublists is dynamic.

Lists are dynamic. That's one of the reasons to use them instead of arrays.

Winston

 
Siana Ivanova
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To know.
My question is not set correctly
I got the following list:

I want my method call Next I first printed two elements, the next call to method print three elements, the next call method again print two elements.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siana Ivanova wrote:I want my method call Next I first printed two elements, the next call to method print three elements, the next call method again print two elements.
You don't have enough elements in it to do so, you need 2, 3, 2 = 7 elements. I see you have only 6.

Anyway, problem probably not there. Do you have clear and written somewhere requirements so we could have a look at?
I just read your original question, which I did not understand, and the latest - these do not match. Please try clearly define your problem.
 
Siana Ivanova
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Sorry!
I have a class, that receives a list of items and divide it into list. Is defined size - a list of how many items are displayed at once.
The class has method next(), that returns next few elements of the list. Is defined size - a list of how many items are displayed at once.

I want this size to be dynamic.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siana Ivanova wrote:I have a class, that receives a list of items and divide it into list.

So, as I said before, you have a List of Lists - or rather, a List of sublists (which are themselves Lists, as you've already worked out).

And as I also said, you don't have to worry about size, because Java Lists are dynamic.

The only thing you need to do is change your 'lists' definition to
  private List<List<String>> lists;
and add a new sublist for each element.

Winston
 
Siana Ivanova
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!