Java-14.4 Java array functions (1)
In this section, we will discuss the practical functions of arrays provided by Java.
1. Copy
Let's compare System. arraycopy and.
package com.ray.ch14;import java.util.Arrays;public class Test {public static void main(String[] args) {int[] a = new int[10000000];Arrays.fill(a, 1);int[] b = new int[10000000];long startTime = System.currentTimeMillis();System.arraycopy(a, 0, b, 0, a.length);long endTime = System.currentTimeMillis();System.out.println(endTime - startTime);System.out.println("-------------");b = new int[10000000];startTime = System.currentTimeMillis();for (int i = 0; i < a.length; i++) {b[i] = a[i];}endTime = System.currentTimeMillis();System.out.println(endTime - startTime);}}
Output:
6
-------------
10
From the output data, we can see that for is slower than System. arraycopy. Of course, this kind of slowness is negligible. The above is just an experiment.
Note:
(1) Using System. arraycopy, the two arrays must be of the same type.
(2) In the copy of an array, if it is of the basic type, copy the value. If it is an object, copy the reference (Shortest copy)
2. array comparison
Two conditions must be met:
(1) the corresponding elements must be consistent
(2) The number of elements must be consistent.
Package com. ray. ch14; import java. util. arrays; public class Test {public static void main (String [] args) {int [] a = new int [5]; Arrays. fill (a, 1); int [] B = new int [5]; Arrays. fill (B, 1); int [] c = new int [5]; Arrays. fill (c, 2); int [] d = new int [6]; Arrays. fill (d, 1); System. out. println (Arrays. equals (a, B); System. out. println (Arrays. equals (a, c); // The element is inconsistent with the System. out. println (Arrays. equals (a, d); // inconsistent number of elements }}
Output:
True
False
False
Here are some examples:
package com.ray.ch14;import java.util.Arrays;public class Test {public static void main(String[] args) {int[] a = new int[] { 1, 2, 3, 4, 5 };int[] b = new int[] { 5, 4, 3, 2, 1 };System.out.println(Arrays.equals(a, b));}}
Output:
False
package com.ray.ch14;import java.util.Arrays;public class Test {public static void main(String[] args) {int[] a = new int[] { 1, 2, 3, 4, 5 };int[] b = new int[] { 1, 2, 3, 4, 5, 6 };System.out.println(Arrays.equals(a, b));}}
Output:
False
The above example is of the basic type, and the following is an object:
package com.ray.ch14;import java.util.Date;import java.util.Arrays;public class Test {public static void main(String[] args) {MyClass[] myClasses1 = new MyClass[2];MyClass myClass1 = new MyClass();MyClass myClass2 = new MyClass();MyClass myClass3 = new MyClass();MyClass myClass4 = new MyClass();myClass1.setDate(new Date());myClass2.setDate(new Date());myClass3.setDate(new Date());myClass4.setDate(new Date());myClasses1[0] = myClass1;myClasses1[1] = myClass2;MyClass[] myClasses2 = new MyClass[2];myClasses2[0] = myClass1;myClasses2[1] = myClass2;System.out.println(Arrays.equals(myClasses1, myClasses2));myClasses2[0] = myClass3;myClasses2[1] = myClass4;System.out.println(Arrays.equals(myClasses1, myClasses2));}}class MyClass {private Date date;public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}}
Output:
True
False
Summary: This chapter mainly discusses the practical functions of arrays provided by Java.
This chapter is here. Thank you.