Results 1 to 8 of 8
Thread: Copying elements in an array
- 11-04-2012, 09:33 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 16
- Rep Power
- 0
Copying elements in an array
How would i copy all the elements in
into the last slots ofJava Code:char arr1[] = {'x','y','z'};
**Failed to mention that I have to be able to do this all in one statement. Meaning looping is out of the question.Java Code:char arr2[] = new char[26];
Last edited by Blondedude092; 11-04-2012 at 09:49 PM. Reason: failed to mention how it's suppose to be done
-
Re: Copying elements in an array
Use a for loop and a very little bit of arithmetic to figure out where your for loop index should start and end. Hint: you'll need to use the length of both arrays for this. You'll also need to use a different value for your arr1 and your arr2 indices, again calculated with a small amount of arithmetic.
- 11-04-2012, 09:51 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 16
- Rep Power
- 0
Re: Copying elements in an array
Ah, Yea, I know that it can be done with a for loop and having that copy into that yet I'm told that it can be done all in one statement; without loops.
-
Re: Copying elements in an array
Ah yes, but of course. System.arraycopy(...) will do this for you in one fell swoop. Please have a look here: System.arraycopy API
- 11-04-2012, 10:01 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 16
- Rep Power
- 0
Re: Copying elements in an array
I'm not sure if this qualifies as a single statement...
also thisJava Code:char arr1[] = {'x','y','z'}; char arr2[] = new char[26]; for(int i = 1; i <4;i++)arr2[22+i] = arr1[i-1]
Java Code:for(int i = 0; i <3;i++)arr2[23+i] = arr1[i];
Last edited by Blondedude092; 11-04-2012 at 10:03 PM.
-
Re: Copying elements in an array
No. You can place all your statements on one line and they still won't be a single statement. Again use System.arraycopy(...) for your single statement.
- 11-04-2012, 10:22 PM #7
Member
- Join Date
- Sep 2012
- Posts
- 16
- Rep Power
- 0
Re: Copying elements in an array
I got it I think! Thanks!!Java Code:System.arraycopy(arr1, 0, arr2, 23, 3);
-
Re: Copying elements in an array
You've almost got it. If this were my code, I'd get rid of the "magic" numbers 23 and 3. I'd use the array lengths instead to calculate the method parameters. This makes for fewer bugs, especially if changes are made to the code in the future.
Similar Threads
-
URGENT help needed in copying array contents
By Terminus_Est in forum New To JavaReplies: 0Last Post: 03-27-2012, 06:16 AM -
Copying from large input stream to byte array
By at_mds in forum Advanced JavaReplies: 1Last Post: 11-16-2011, 04:44 PM -
Copying array values without creating just a reference.
By xael in forum New To JavaReplies: 1Last Post: 04-11-2011, 08:25 PM -
Trouble copying an array
By xXRedneckXx in forum New To JavaReplies: 10Last Post: 02-05-2011, 05:36 PM -
Copying ArrayList into an Array
By Manfizy in forum New To JavaReplies: 6Last Post: 07-16-2009, 07:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks