Two ways to copy arrays in Java

Source: Internet
Author: User

There are two ways to copy an array in Java:

A: System.arraycopy (original array, start copy of the subscript, the copy contents of the array, start to store the subscript, the length of the copy is required);

This method requires creating an empty array that holds the copy content, and then copying the original array into the new array.

  

int [] arr = {1,2,3,4,5};      int New int [ten];   0 1 5); // 5 is the length to copy      System. out. println (arrays.tostring (copied));  

Two: arrays.copyof (original array, copy of the array length);

Can be used to expand and shrink, do not need to first create an array, but actually also created a new I array, the bottom is called system.arraycopy () method;

  

int[] copied = arrays.copyof (arr, 10); The length of the new array  System.out.println (arrays.tostring (copied));     copied = arrays.copyof (arr, 3);  System.out.println (arrays.tostring (copied));  

  

Two ways to copy arrays in Java

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.