Assignment and copying of Arrays

Source: Internet
Author: User

In a program, you need to copy an array or a part of an array. In this case, the assign value statement (=) may be used, for example:

int []list1=new int[3];int []list2=new int[3];list1=list2;

However, this statement does not assign the value of list2 to list1. Instead, it only transfers the reference of list2 to list1. Through the preceding statement, it only points list1 and list2 to the same array. The array originally referenced by list1 cannot be referenced any more. If it becomes garbage, it will be recycled by JVM. Therefore, in Java, you can use the value assignment (=) statement to copy basic data type variables, rather than copying referenced data type variables. Assigning an array variable to another array variable is actually copying an array reference to another array so that the two variables point to the same memory address. In Java, there are three methods to copy an array: 1. Use loop statements to copy them one by one. 2. Use the static method arraycopy in the system class. 3. Use the clone method to copy the array.

Next, let's talk about the use of passing arrays in methods.

In Java, real parameters are passed to methods by passing values. The value of the passed frozen data type variable is very different from that of the passed array: for basic data type parameters, the value of the real parameter is passed. For parameters of the array type, the parameter value is an array reference, so this reference is passed to the method. To put it bluntly, the real parameter is assigned to the form parameter. Note that the value assignment (=) is not a copy.

See the following code,

Public class test {public static void main (string ARGs []) {int x = 1; int [] Y = new int [10]; m (x, y ); // PASS Parameters by method. System. out. println ("X is" + x); system. out. println ("Y [0] is" + Y [0]);} public static void M (INT nmber, int [] numbers) {number = 100; numbers [0] = 123; // The reference address of array y is assigned to the array numbers, that is, they all point to the same memory address, where the value of numbers [0] is changed, that is, the value of Y [0] is changed .}}

I believe that through this test, we can clearly see the differences between the basic data type and the input parameter of the array type.

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.