Several methods of array copying in Java language

Source: Internet
Author: User

The For loop replicates one by one: The For loop is flexible, but the code is not concise enough.
System.arraycopy:System.arraycopy () source code. You can see that it is the native method: The native keyword describes its method of modification as an original ecological method, and the corresponding implementation of the method is not in the current file, but in a file implemented in other languages such as C and C + +. The native method can be likened to a Java program's interface to a C program. ( optimal )
public static native void Arraycopy (Object src,  int  Srcpos,           
System.copyof:copyOf is not the system method, but the arrays method, the following is the source code, you can see the Arraycopy method is essentially called. , then its efficiency must be inferior to Arraycopy's
123456 public static int[] copyOf(int[] original, int newLength) {   int[] copy = new int[newLength];   System.arraycopy(original, 0, copy, 0,   Math.min(original.length, newLength));   return copy;}
With the Clone method: Clone, the object returned is "" and requires a cast. The general use of clone efficiency is the worst

Several methods of array copying in Java language

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.