Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Copy of array - difference

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Copy of array - difference

    What is the difference between these two ways of array copying and which way is better?

    Java Code:
             int[] array1 = new int[7];
             int[] array2;
             array2 = array1.clone();
    Java Code:
             int[] array1 = new int[7];
             int[] array2 = new int[array1.length];
             System.arraycopy(array1, 0, array2, 0, array1.length);

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

    Default Re: Copy of array - difference

    For one, System.arraycopy has been optimized for copying arrays and is significantly faster at copying items from one array to another. AFAIK, neither does a deep copy though.

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

    Default Re: Copy of array - difference

    Also have a look at the Arrays utility class; it has a bunch of copyOf( ... ) methods; I bet they are optimized for the job.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Copy of array - difference

    Can I say: Arrays class is specialized for dealing with reference values of many array types?

Similar Threads

  1. What is the difference?
    By sublixt in forum New To Java
    Replies: 2
    Last Post: 08-14-2011, 03:37 AM
  2. What is the difference between
    By arnab321 in forum New To Java
    Replies: 2
    Last Post: 01-19-2009, 04:49 AM
  3. what is the difference between jsp and jsf?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-31-2008, 05:55 PM
  4. Array initialization difference
    By sln69 in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2008, 02:38 AM
  5. Difference between ASP and JSP
    By barney in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 07:15 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
  •