Results 1 to 8 of 8
  1. #1
    Blondedude092 is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Copying elements in an array

    How would i copy all the elements in
    Java Code:
    char arr1[] = {'x','y','z'};
    into the last slots of
    Java Code:
    char arr2[] = new char[26];
    **Failed to mention that I have to be able to do this all in one statement. Meaning looping is out of the question.
    Last edited by Blondedude092; 11-04-2012 at 09:49 PM. Reason: failed to mention how it's suppose to be done

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Blondedude092 is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  5. #5
    Blondedude092 is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Copying elements in an array

    I'm not sure if this qualifies as a single statement...
    Java Code:
    char arr1[] = {'x','y','z'};
            char arr2[] = new char[26];
    
            for(int i = 1; i <4;i++)arr2[22+i] = arr1[i-1]
    also this
    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    Blondedude092 is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Copying elements in an array

    Java Code:
    System.arraycopy(arr1, 0, arr2, 23, 3);
    I got it I think! Thanks!!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. URGENT help needed in copying array contents
    By Terminus_Est in forum New To Java
    Replies: 0
    Last Post: 03-27-2012, 06:16 AM
  2. Copying from large input stream to byte array
    By at_mds in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2011, 04:44 PM
  3. Replies: 1
    Last Post: 04-11-2011, 08:25 PM
  4. Trouble copying an array
    By xXRedneckXx in forum New To Java
    Replies: 10
    Last Post: 02-05-2011, 05:36 PM
  5. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 07:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •