Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the copy methods of Java array

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the copy mode of Java array, which has certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article. Let's take a look at it.

The difference between deep copy and shallow copy

Suppose that there are the original array An and the copied array B. if you change a value in A, the copy mode of the B array is called shallow copy, otherwise, the B array is not affected, it is called deep copy.

Briefly summarize the concepts of the two:

Deep copy: after copying, modify the original array without affecting the new array

Shallow copy: after copying, modify the original array, and the new array will be changed accordingly.

1. For loop to copy the numeric type

When the element stored in the array is a basic data type, what happens is a deep copy

/ / 1. For cyclic copy (copy numeric type)-Deep copy public static void main (String [] args) {int [] A = {1LGI 2Jing 3Zhi 4Jing 5}; int [] B = new int [A.length]; for (int I = 0; I < A. resume; iCoke +) {B [I] = A [I] } System.out.println ("A:" + Arrays.toString (A)); / / A: [1,2,3,4,5] System.out.println ("B:" + Arrays.toString (B)); / / B: [1,2,3,4,5] System.out.println ("= modified ="); A [0] = 100 System.out.println ("A:" + Arrays.toString (A)); / / A: [100,2,3,4,5] System.out.println ("B:" + Arrays.toString (B)); / / B: [1,2,3,4,5]} / / method for printing an array of objects public static void show (Num [] arrays) {for (int I = 0; I < arrays.length) System.out.print (arrays [I] .getVal () + ");} System.out.println ();} class Num {public int val = 0; public Num (int val) {this.val = val;} public int getVal () {return val;} public void setVal (int val) {this.val = val }} copy reference type

When the element stored in the array is a reference data type, what happens is a shallow copy

/ / 1. For circular copy (copy reference data type)-shallow copy public static void main (String [] args) {Num [] A = new Num [4]; A [0] = new Num (1); A [1] = new Num (2); A [2] = new Num (3); A [3] = new Num (4); Num [] B = new Num [4] For (int I = 0; I < A.SecretthsI +) {B [I] = A [I];} show (A); / / 1234 show (B); / / 1234 System.out.println ("= modified ="); A [0] .setVal (100); show (A); / 100234 show (B) / / 1002 3 4} 2. Copyof / copyOfRange copy numeric type

When the element stored in the array is a basic data type, what happens is a deep copy

Arrays.copy (original array, custom new array length)

Arrays.copyOfRange (primitive array, from,to)

Note that the range of copy interception is left closed and right open [from,to).

/ / 2. Copy / copyOfRange (copy numeric type)-Deep copy public static void main (String [] args) {int [] A = {1pje 2je 3Zhi 4jue 5}; int [] B = Arrays.copyOf (A Magi A. length); int [] C = Arrays.copyOfRange (A Magee 1m 3); System.out.println ("A:" + Arrays.toString (A)) / A: [1,2,3,4,5] System.out.println ("B:" + Arrays.toString (B)); / / B: [1,2,3,4,5] System.out.println ("C:" + Arrays.toString (C)); / / C: [2,3] System.out.println ("= modified ="); A [0] = 100 System.out.println ("A:" + Arrays.toString (A)); / / A: [100,2,3,4,5] System.out.println ("B:" + Arrays.toString (B)); / / B: [1,2,3,4,5] System.out.println ("C:" + Arrays.toString (C)); / / C: [2,3]} copy reference type

When the element stored in the array is an object of the class, what happens is a shallow copy

/ / 2. Copy / copyOfRange (copy reference type)-shallow copy public static void main (String [] args) {Num [] A = new Num [4]; A [0] = new Num (1); A [1] = new Num (2); A [2] = new Num (3); A [3] = new Num (4); Num [] B = Arrays.copyOf Show (A); / 1234 show (B); / 1234 System.out.println ("= modified ="); A [0] .setVal (100); show (A); / 100234 show (B); / 100234} class Num {public int val = 0; public Num (int val) {this.val = val } public int getVal () {return val;} public void setVal (int val) {this.val = val;}} 3. Arraycopy copy numeric type

When the element stored in the array is a basic data type, what happens is a deep copy

System.arraycopy (src, srcPos dest, destPos, length)

The parameters are expressed as follows:

Src: source array

SrcPos: the starting position of the source array to copy

Dest: target array

DestPos: the starting position of the destination array replication

Length: length of copy

So when both srcPos and destPos are 0, and length is the length of the source array, it means that the complete copy has come over; then the copy of the intercepted range is also taken as an example. In the following code, srcPos = 1, which means that 2 elements are intercepted from the position where the subscript of the An array is 1, and put it into the position of subscript 2 in the B array as the starting position, and then compare the output.

/ / 3. Arraycopy (copy numeric type)-Deep copy public static void main (String [] args) {int [] A = {1, 2, 3, 3, 4, 5}; int [] B = new int [A.length]; / / System.arraycopy (A, / System.arraycopy (A, System.out.println ("A:" + Arrays.toString (A)); / / A: [1,2,3,4,5] System.out.println ("B:" + Arrays.toString (B)); / / B: [0,0,2,3,0] System.out.println ("= modified ="); A [0] = 100 System.out.println ("A:" + Arrays.toString (A)); / / A: [100,2,3,4,5] System.out.println ("B:" + Arrays.toString (B)); / / B: [0,0,2,3,0]} copy reference type

When the element stored in the array is an object of the class, what happens is a shallow copy

/ / 3. Arraycopy (copy reference type)-shallow copy public static void main (String [] args) {Num [] A = new Num [4]; A [0] = new Num (1); A [1] = new Num (2); A [2] = new Num (3); A [3] = new Num (4); Num [] B = new Num [4] System.arraycopy; show (A); / 1234 show (B); / 1234 System.out.println ("= revised ="); A [0] .setVal (100); show (A); / 100234 show (B); / 100234} class Num {public int val = 0 Public Num (int val) {this.val = val;} public int getVal () {return val;} public void setVal (int val) {this.val = val;}} 4. Clone copy numeric type

When the element stored in the array is a basic data type, what happens is a deep copy

/ / 4. Clone (copy numeric type)-Deep copy public static void main (String [] args) {int [] A = {1, 2, 3, 5); int [] B = A.clone (); System.out.println ("A:" + Arrays.toString (A)); / / A: [1, 2, 3, 4, 5] System.out.println ("B:" + Arrays.toString (B)) / B: [1,2,3,4,5] System.out.println ("= modified ="); A [0] = 100; System.out.println ("A:" + Arrays.toString (A)); / / A: [100,2,3,4,5] System.out.println ("B:" + Arrays.toString (B)) / / B: [1,2,3,4,5]} copy reference type

When the element stored in the array is an object of the class, what happens is a shallow copy

/ / 4. Clone (copy reference type)-shallow copy public static void main (String [] args) {Num [] A = new Num [4]; A [0] = new Num (1); A [1] = new Num (2); A [2] = new Num (3); A [3] = new Num (4); Num [] B = A.clone () Show (A); / 1234 show (B); / 1234 System.out.println ("= modified ="); A [0] .setVal (100); show (A); / 100234 show (B); / 100234} 5. Summary copy method numeric type reference type recommended to use for circular deep copy shallow copy copyof deep copy √ arraycopy deep copy √ clone deep copy shallow copy

Because the bottom layer of arraycopy is written by C++, it is fast and more likely to use this method.

Note: all the reference data types in this article take the object of the class as an example, using an array of objects, and we also know that reference types include classes, interfaces, strings, and so on. However, it is important to note that the string is a new variable, so if you copy an array of strings, even if they are of reference type, a string array object is created every time, so modifying the original array will not affect the new array, that is, deep copy.

Thank you for reading this article carefully. I hope the article "what are the copy methods of Java array" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report