Android 2.3.3
This is my code…
String[] expression = {""}; //globally declared as empty
somewhere in the code below, I am trying to assign a string to it.
expression[0] = "Hi";
I keep getting the following error...
12-08 22:12:19.063: E/AndroidRuntime(405): java.lang.ArrayIndexOutOfBoundsException
Can someone help me with this..
Can we access the index 0, directly as i am doing?
Actual Code :::
static int x = 0; // global declaration
String[] assembledArray = {""}; // global declaration
assembleArray(strSubString, expression.charAt(i)); //Passing string to the method
//Method Implementation
private void assembleArray(String strSubString, char charAt) {
// TODO Auto-generated method stub
assembledArray[x] = strSubString;
assembledArray[x+1] = String.valueOf(charAt);
x = x+2;
}
The problem is not in
assembledArray[x]; it’s inassembledArray[x+1].At the first iteration,
x+1 = 1, so you cannot access that part of the array. I would suggest using a dynamic array, aka anArrayList:This way, Java takes care of the resizing, and you don’t need to keep track of
x.