![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
c1="1234"
c2="123456" those are both char arrays. I want to make them eqwals in elngth : c1="001234" c2="123456" Like that, inserting '0' in the first array......Now, i dont know wich array is shortest. I've tried it like this: int N=c1.length; int M=c2.length; while(N!=M){
if(N<M){
for(int j=N;j>=1;j--)
c1[j]=c1[j-1];
c1[0]='0';
N++;
}
if(N>M){
for(int k=M;k>=1;k--)
c2[k]=c2[k-1];
c2[0]='0';
M++;
}
}It gives IndexOutOfBounds at: c1[j]=c1[j-1]; or c2[k]=c2[k-1] I don't realy undersand y. Also i don't want to copy the shortest array into a new one and inserting the zeros there ... It's a simple problem .. but i just can't find an answere. HELP ![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2004
Posts: 16
Rep Power: 0
![]() |
The reason is that you are accessing the array index N or M, which doesn't exist. There are N and M elements in the arrays, indexed from 0 to N - 1 and M - 1. So there is no element at c1[N] and c2[M], the arrays end at c1[N - 1] and c2[M - 1].
You cannot just index out of the array and expect it to grow magically, the size is fixed at the arrays creation. You will have to create new arrays and copy the contents of the old ones into the new ones. |
|
|
|
|
|
#3 |
|
Professional Programmer
|
Ok, thanks for the info
![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
If you want it to grow magically, use a Vector. (Or an arraylist, i think, im not hip to a bunch of the new Java 5 weirdness)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|