Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error! Array Index Out Of Bounds Exception

 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dani Atrei
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int x [] = new int [9];

--There are 10 elements in this array (0-9) right.


That's wrong, there's 9 elements, from 0 - 8

You don't need to expand the array, you can just use an ArrayList which will do it for you. With an arraylist, you can just add stuff to it, it will dynamically increase in size or decrease.

Dan
[ February 04, 2006: Message edited by: Dani Atrei ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above reply is fine otherwise you can create an array of that particular size and copy it by System.arrayCopy() which will copy the earlier array into new one.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not using a good old ArrayList
(whose initial storage size happens to be 10 items)
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In the below codes I increased the size of bag. So this codes can be beneficial for you.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, ignore valid
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah sorry for rudimentary knowledge.

i : the location whixh you locate your int.
value : the specified int
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic