• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Multiple ArrayList

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to use multiple ArrayLists?
My test program has Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(ArrayList.java:324)
at ArrayListTest.main(ArrayListTest.java:9)
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you check the API for the constructor you are invoking at line 5? It says:

public ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.



So, basically, until you add values in your list, the size of the list is 0. Hence, statement inside for loop is not executed, and a.get(a.size() - 1) is actually a.get(-1), and that's not valid index so you get an exception.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Specifying the initial capacity in the constructor doesn't mean the list will have that many elements in it, it just specifies the maximum number of elements that can be added before the backing array must be resized.
Therefore, the ArrayList will be constructed with a backing array of length 5 but the array will be empty and the ArrayList size will be 0 and so the for loop won't add any elements and hence your attempt to get the last element (a.size()-1) fails as the ArrayList is empty.
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modify my test program so that it will store the integer from 0 to 4.
However, the output is
a1 [] size 5 []
I expect to see integer 1 and 4 but get "[]".
How to fix this program?

 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try creating an array list and printing it out. You find you get a pair of square brackets, with all the elements inside. Maybe you have got empty lists from those get() calls, and they simply print out the pair of square brackets. If you look for the array lists’s toString method, you find this, which tells you about the square brackets. It doesn’t say that an empty List prints as [], but you can probably validate that with
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic