Four ways to copy arrays in the Java language

Source: Internet
Author: User

Which of the following types of array replication methods in the Java language is most efficient?

B.      efficiency: system.arraycopy > Clone > Arrays.copyof > For Loop


1,the use of system.arraycopy:

[Java]View PlainCopy
  1. Public static void Arraycopy (Object src,
  2. int Srcpos,
  3. Object dest,
  4. int Destpos,
  5. int Length)
Parameters: src -source array. srcPos -The starting position in the source array. dest -target array. destPos -The starting position in the target data. length -The number of array elements to copy

Application Examples:

[Java]View PlainCopy
  1. Public class main{
  2. public static void Main (string[] args) {
  3. int[] a1={1,2,3,4,5,6};
  4. int[] a2={,N,16};
  5. System.arraycopy (A1, 2, A2, 3, 2);
  6. System.out.print ("copy post result:");
  7. For (int i=0;i<a2.length;i++) {
  8. System.out.print (a2[i]+"");
  9. }
  10. }
  11. }
Operation Result:



2,the use of clone:

First the cloned class implements the Cloneable interface, then overrides the Clone () method in the class and calls Super.clone () in the Clone () method, so that the Super.clone () You can call the Clone () method of the Java.lang.Object class.

Application Examples:

[Java]View PlainCopy
  1. Cloned class to implement the Cloneable interface
  2. Class Cat implements Cloneable
  3. {
  4. private String name;
  5. private int age;
  6. Public Cat (String name,int. age)
  7. {
  8. This.name=name;
  9. This.age=age;
  10. }
  11. //Override Clone () method
  12. protected Object Clone ()throws clonenotsupportedexception{
  13. return super.clone ();
  14. }
  15. }
  16. Public class Clone {
  17. public static void Main (string[] args) throws clonenotsupportedexception {
  18. Cat cat1=New Cat ("Xiaohua",3);
  19. System.out.println (CAT1);
  20. //Call the Clone method
  21. Cat cat2= (CAT) Cat1.clone ();
  22. System.out.println (CAT2);
  23. }
  24. }
3. the difference between copying a reference and copying an object

Copy reference: Refers to the copy of an object's address, so the copy of the object copy of the address and the source object is the same, so that when the copy of a value, the value of the source object is changed;

Copy object: Is the entire copy of the source object, the object copy and the address of the source object is not the same, when changing a copy of a value, the source object value will not change;

[Java]View PlainCopy
  1. Cat cat1=New Cat ("Xiaohua",3); Source Object
  2. System.out.println ("source object Address" +cat1);
  3. //Call the Clone method to copy the object
  4. Cat cat2= (CAT) Cat1.clone ();
  5. Cat cat3= (CAT) cat1; //Copy reference
  6. System.out.println ("Copy object address:" +CAT2);
  7. System.out.println ("Copy reference Address:" +CAT3);
Output Result:


You can see that the copied reference object and the source object address are the same, the Copy object and the source object address are different

4,the use of arrays.copyof:

Arrays.copyof has 10 overloaded methods that copy the specified array, returning a copy of the original array. The JDK API can be viewed specifically

Four ways to copy an array in the Java language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.