This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error! Array Index Out Of Bounds Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error! Array Index Out Of Bounds Exception" Watch "Error! Array Index Out Of Bounds Exception" New topic
Author

Error! Array Index Out Of Bounds Exception

Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
Posts: 27
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
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 ]

Si altas son las torres, el valor es alto - Alberti
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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.


The Best way to predict your future is to create it - Every great individual common man
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Why not using a good old ArrayList
(whose initial storage size happens to be 10 items)


[My Blog]
All roads lead to JavaRanch
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hello,
In the below codes I increased the size of bag. So this codes can be beneficial for you.
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Ah, ignore valid
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Ah sorry for rudimentary knowledge.

i : the location whixh you locate your int.
value : the specified int
 
 
subject: Error! Array Index Out Of Bounds Exception
 
Similar Threads
Assigning array to existing array reference.
simple sorting algorithm
AOOB exception in my CODE-PLEASE HELP!
Using a multidimensional array
Problem with instanceof