• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array index out of bounds exception?  RSS feed

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an arraylist and i wanted to print just 3 of it, whenever i add 3 or more elements it doesn't give me an error but whenever i add less than 3 it gives me this error?



Can somebody explain to me what's happening? and why is it giving me an error?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your for loop is accessing the last 3 elements in the ArrayList. You are right in that the last element index of a list sized n, is index n - 1. So you know the index range for a list of size n is 0 ... n-1.

Think about the indexes you are trying to access for a list of size 2. Can you see what the problem with that is when you attempt to access the last 3 elements of a 2 element list? The ArrayIndexOutOfBoundsException will tell you the invalid index you are attempting to access, which may help you figure it out.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand.. Is there a way i could get the current size of the arraylist?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The .size() gets you the size of the array, but you knew that already because you have it in your code.

Perhaps explain again what you're hoping to achieve? For lists with 3 or more elements you want to print the last 3 elements. For lists with less than 3 elements you want to do what?
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd still want to print them but as stated.. it gives me error..
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you have only 2 elements in the array you want to print all 2 elements?

Try modifying the termination clause if the for loop to check for index 0 as well as size() -3. Something like this:

Disclaimer: I haven't actually run this code to verify it works or not. But it should give you an idea either way.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't work but thanks for trying to help
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't work how?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I did try it out myself this time.

The code you presented will print the last 2 (not 3) elements in the list. And it runs fine unless the array size is 0 or 1. In order to handle those scenarios you can modify the termination clause, as I already talked about.


The code is tested this time.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That kinda works! Thank you so much!
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"kinda works"? It totally works
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that's how you reply xD
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!