• 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

arraylist - error

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here are some of my codes.
i try to add an item to my arraylist from a GUI class.

i cannot add a second item after i have added one item.



i get : java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
arraylist should adjust the size automatically?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message likely also suggests a line of code where the error originates. Which line of code is it?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, I don't see a main method in the posted code. So, apparently there's more to this application that could possibly be the source of the problem.

Are you sure the problem originates from the posted code and not from something else?
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a List will automatically "grow" when new objects are added. However, you can still get an IndexOutOfBoundsException if you try to do a list.get(i) and i is a value greater then or equal to list.size(). So, if your list has one object in it and you did list.get(1), you'd get an IndexOutOfBoundsException because list.get(1) assumes there are at least 2 objects in the list.

It's hard to tell exactly what your code is doing since it's in a different language, but this part of the code looks like the culprit:

I believe the second conditional should be rewritten like this:

Try that out, and let us know if you're still having trouble.
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Blake,
you were right! you saw the problem ^^ ...

but it was me that forgot a silly thing.
i should use on both:

list.size()>i

thats why i defined a variable (int i = 0).

and i found out that to use "while" on the first, will totally give me no problems ^^-..



thanks for the replies
 
reply
    Bookmark Topic Watch Topic
  • New Topic