Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 12th, 2004, 4:30 AM   #1
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is offline   Reply With Quote
Old Dec 12th, 2004, 5:05 AM   #2
andersRson
Newbie
 
Join Date: Nov 2004
Posts: 16
Rep Power: 0 andersRson is on a distinguished road
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.
andersRson is offline   Reply With Quote
Old Dec 12th, 2004, 5:25 AM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Ok, thanks for the info
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Dec 12th, 2004, 2:39 PM   #4
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
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)
ZenMasterJG is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC