• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundsException:0  RSS feed

 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched online for this error and found out it's because the array is smaller than the index but I am not sure how I can fix this error....



I tried

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that DEFAULT_ZERO is just the value 0?

If that's the case, then you are creating an array with room for 0 elements in line 3:

In line 7 you're trying to set the element at position i, but if the size of the array is 0 then it does not have room for any elements.

Arrays have a fixed size once they are created. If you need a collection that can hold a number of items that you don't know beforehand, then don't use an array - use a java.util.List instead.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe your Car class should have a method to tell whether the power is within a rangeIn Java8 you can do that with a Stream, which can be done in one statement. Something like this:-I have probably got the syntax wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!