• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fill an arraylist with an array using for loop  RSS feed

 
Laura White
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the title says I need to fill an arraylist with an array using a for loop.

The code below works...almost. I'm using length-1 in my for loops otherwise I get a java.lang.IndexOutOfBoundsException: Index: 0, Size: 0



So I can't fill my arraylist properly (without the .length-1) without getting this error:


 
Laura White
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this working! Sorry there was nothing wrong with my arraylist, it was something else entirely in my program.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear you figured it out.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you have found out, you cannot get that sort of Exception from your for loops. If you pass a 0‑length array, your loop will never start because its continuation condition will never become true. That is the behaviour you want. Your loops will of course give you an out by one error because your Lists will have a maximum size one less than the arrays and you will omit the last element, if any, of your arrays. If you are writing a for loop, always start like this:-
for (int i = 0; i < something.size(); i++) ...
That is the basic form of a for loop and you shou‍ld start there, even if you need to change some of the parts later. I would prefer to use a for‑each loop where you don't need to do any counting; the loop can do it for you:-That is pronounced, “For each XZY x in myXYZarray, ...” Actually that isn't true any more. I wouldn't use a loop to populate an array at all, if I can help it. Not when I can write:-Note odd indentation convention: the dots align vertically. What you are doing is using the stream() method to handle the array. There are overloadings of stream() which can handle any array type except boolean[], byte[], short[], char[], and float[]. Now you get a Stream<XYZ> which can iterate the entire array and handle each element. In a simple case like this, you only need the collect() method, which takes a Collector as its parameter. Now, it would appear difficult to write a Collector, but you don't have to, because the Collectors class has a static method which produces such a Collector ready‑made. You will find that the type returned from that Collector has the runtime type java.util.ArrayList, which is just what you want. You shou‍ld find code similar to my example if you read all those links.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using a loop to fill the ArrayList, or even instead of  streaming, why not just use the ArrayList constructor that takes a Collection as its single argument?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to convert the array to a Collection first. You would have to do something like this:-Unfortunately, I can't seem to find an ArrayList constructor taking an array as a parameter. If you use JavaFX there is an ObservableList with an addAll method but I don't think that helps here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!