Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array question

Status
Not open for further replies.

Mike2020

Programmer
Mar 18, 2002
55
0
0
US

Hi all,

I try to expand my array by doing this.

for(int x=0; x < account.length;x++)
{ //System.out.println("in For loop,AccountNumber:"+ account[x].getAccountNumber());
int anumber = account[x].getAccountNumber();
if(anumber == Anums)
{ Match = "true";
aindex = x;
break;}
}
if(Match == "true")
{
int testnum = 500;
int testbalance = account[aindex].getBalance();
int finalbalance = testbalance + testnum;
account[aindex].setBalance(finalbalance);
//System.out.println("Account NO: " + account[aindex].getAccountNumber() + "-- Balance:" + account[aindex].getBalance());
}
else
{
int Len = account.length;
int newLen = Len + 1;
Account[] newArray = new Account[newLen];
System.arraycopy(account, 0, newArray, 0,Len);
account = newArray;
//account = Arraysize.arrayExpand(account);
int iindex = account.length;
int ibal = 0;
System.out.println("AccountLength:"+ iindex);
account[iindex] = new Account(Anums, ibal);
// String aM = "false";
// vMutex.addElement(aM);

}

I encounter this error.
java.lang.ArrayIndexOutOfBoundsExpection: 7 at ATM.run(ATM.java: 157)
The line 157 is: account[iindex] = new Account(Anums, ibal);

Do you know where I do wrong?

Thanks....

Mike

 
ArrayIndexOutOfBounds exception means that you have specified an index that is larger or equal to the length of the array, since arrays are 0-indexed. Replace the line with
Code:
account[iindex[red]-1[/red]] = new Account(Anums, ibal);
since iindex is defined to be the length.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 

Hi chessbot:

Hey, thank you so much, you are correct,
I just forget about that. :)

Thank you so much...

Mike
 
No problem... Can you help on my post? (thread269-942628)

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top