Results 1 to 17 of 17

Thread: Array copy

  1. #1
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default Array copy

    Hello,

    I have a question regarding copy between arrays. For instance: I have array1 = {"1","2","3"}, array2 ={"4","5","6"}. I need to copy array1 and array2 in to array3 which is empty. array3 should be { "1","2","3","4","5","6"}.

    Did anyone know the logic of this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Take a piece of paper and write down the two source arrays and the target array.
    Mark the source of the first element to copy from and mark the first target element to receive that value.
    copy elements and move both marks until all the elements of the first array are copied
    Move to the second array and set a mark at its first element.
    copy elements and move marks until the second array is copied.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    #1
    List<String> both = new ArrayList<String>(array1.length + array2.length);
    Collections.addAll(both, array1);
    Collections.addAll(both, array2);
    String[] array3 = both.toArray(new String[0]);
    #2
    String[] array3 = new String[array1.length + array2.length];
    System.arraycopy(array1, 0, array3, 0, array1.length);
    System.arraycopy(array2, 0, array3, array1.length, array2.length);
    #3
    iterate over the first array and then over the second one :)
    #4
    your idea here.... :D

    /edit: sorry Norm ;/

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    @eRaaaaa What is the code you posted supposed to do?
    Can you add comments to it that explains to the OP what the code does and why?

  5. #5
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default

    Yes. Please add comments, that would help me to understand.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    #1
    List<String> both = new ArrayList<String>(array1.length + array2.length); //create a arraylist with initial capacity of lenght1 and length2
    Collections.addAll(both, array1); //add all strings from the first array to the list
    Collections.addAll(both, array2); //add all strings from the second array to the list
    String[] array3 = both.toArray(new String[0]); //"convert" the list back to an array
    (addAll is using the add method of the list, so it will added to the end)

    see the collections api doc: Collections (Java Platform SE 6)
    #2
    String[] array3 = new String[array1.length + array2.length]; //create a third array with the size of lenght1 and length2
    System.arraycopy(array1, 0, array3, 0, array1.length); //copy all elements from array1 to the first array1.length positions in array3
    System.arraycopy(array2, 0, array3, array1.length, array2.length); //copy all elements from array2 in array3, started at index array1.length
    the arguments are:

    Object src,
    int srcPos,
    Object dest,
    int destPos,
    int length

    after the first System.arraycopy call the third array looks like:
    [1,2,3,0,0,0]
    so at the second call you have to change the fourth argument (destination position) to array1.length to add the other to the end otherwise you would overwrite them again!


    see the system api doc: System (Java Platform SE 6)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Quote Originally Posted by lakshmibvaraprasad View Post
    Yes. Please add comments, that would help me to understand.
    Why not attempt at writing the code yourself. That is the best way to learn.

  8. #8
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default

    Thanks a lot for the information. I have a doubt at code#1 of line 4. Can u tell me what does String[0] mean?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Java Code:
    both.toArray(new String[0]);
    The variable both refers to an ArrayList and it is calling the toArray method. Why not go to the Java API and read about that method?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Can u tell me what does String[0] mean?
    The actual expression on that line is "new String[0]". This creates a String array of length zero. (See the examples of this in the Arrays page of Oracle's Tutorial).

    It might seem like a silly thing to create - a container so small that nothing can be put into it. To see why it there you need to have a read of the API docs for the toArray(T[]) method of List.

    Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

    In other words the zero length String array is used as an argument in the toArray() call so that that method will return a String array.

    -----

    I cannot reccommend highly enough these two sources - the Tutorial and the API documentation - as the starting place when you want to know what code does. Of course forums like this are also good when the available documentation and examples are unclear in some way.

  11. #11
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default

    Hello eRaaa,

    I have found an error while using code#2. I have used this code in 'while' loop. When the loop iterate for the 1st time, it copies array1 and array2 into array3. When the loop iterates for the 2nd time, then the values in the array3 of iteration 1 are replaced and then writes the values obtained in 2nd iteration. Can u help me in this???

    -thanks

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    When the loop iterates for the 2nd time, then the values in the array3 of iteration 1 are replaced
    If you assign a new value to a variable, the old value is replaced.
    For example:
    int i = 1;
    i = 2;
    i = 3;
    // Now i has the value of 3, the old values of 1 and 2 were replaced

    Can you explain what you want to happen in the loop and what values you want available in the loop and after the code exits the loop?

  13. #13
    Join Date
    Jun 2011
    Posts
    24
    Rep Power
    0

    Default

    i should have 1,2,3 values.(one by one)

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    To hold multiple values in one variable: 1 and 2 and 3
    you need a container of some kind.
    An array or arraylist for example

  15. #15
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    array1= {"1","2","3"} and array2={"4","5","6"}

    array3=array1+array2 = {"1","2","3","4","5","6"}

    code here .................

    int array1= {"1","2","3"}
    int array2={"4","5","6"}

    int array3[] = new int[array1.length+array2.length];

    int k=0;

    for(int i=0;i<array1.length;i++)
    {
    array3[k] = array1[i];
    k++;
    }


    for(int j=0;j<array2.length;j++)
    {
    array3[k] = array2[j];
    k++;
    }

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What is that code supposed to do?
    What does it do when you execute it?

    Please make print outs to show what it does and add explanations about what you want different.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Why not use the System.arraycopy( ... ) method?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Copy String to Array Urgent!
    By louboulos in forum New To Java
    Replies: 10
    Last Post: 03-13-2011, 10:08 PM
  2. Need help copy array into new array
    By Get_tanked in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 03:45 AM
  3. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  4. Replies: 1
    Last Post: 05-20-2010, 10:17 PM
  5. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 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
  •