• Post Reply Bookmark Topic Watch Topic
  • New Topic

breaking an ArrayList into several arrays  RSS feed

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
greetings all,

So I have an ArrayList and I need a way to break it up into several fixed-size arrays

An example:

ArrayList<Foo> lisst contains 27 objects of type Foo

If my maximum array length is 10, I want to wind up with

Foo[10] foo_0 contains lisst(0) thru lisst(9)
Foo[10] foo_1 contains lisst(10) thru lisst(19)
Foo[7] foo_2 contains lisst(20) thru lisst(26)


Or better still would be to only declare two fixed arrays
Foo[10] foo_filled
Foo[ lisst.size() % 10] foo_remainder

where I populate foo_filled with lisst(0) thru lisst(9), use it, then repopulate it with lisst(10) thru lisst(19) and reuse it, then populate foo_remainder once with the remaining 7 elements.

Best solution would be to consume lisst in chunks of 10, process and discard each chunk, lastly consume and process the remaining chunks.

Any ideas?


Still-learning Stuart
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this seems to do the trick, any anyone find any flaws?



TIA,

Still-learning Steve
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use the ArrayList class' subList() method to get a view of the original list spanning the range(s) you are interested in and then create an array directly from that.

BTW why are you creating an array rather than just passing a List?
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. subList() , didn't know about that one. docs say it takes two arguments - wonder what'd happen if passed only one? Hopefully Java would imply "...to the last element inclusive"

Why, you ask? I'm coding to someone else's API method whose argument is an array.

Thanks for responding!

CASE CLOSED

Still-learning Steve
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. subList() , didn't know about that one. docs say it takes two arguments - wonder what'd happen if passed only one?

It won't compile, but I guess you've already found that out by now.

Hopefully Java would imply "...to the last element inclusive"

It would be extremely dangerous to have a computer language that attempted to imply stuff when you pass in the wrong number of arguments, how does it know you haven't just forgotten to type the missing argument. Some languages allow optional arguments but that is different as the code is documented to say what happens in those cases.

If the writers of the library thought it was reasonable to imply "to the end" then there would be a single argument method as there is for substring() in the String class. They didn't so I'm afraid you will have to supply both arguments.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!