The method for array replication is to use the methods provided by the system class, with the arraycopy() following syntax:
System.arraycopy (object src, int srcpos, object dest, int destpos, int length);
System.arraycopy ( 源数组,源数组中的起始位置,目标数组,目标数据中的起始位置,要复制的数组元素的数量 );
PublicClassT{PublicStaticvoidMain(StringArgs[]){IntArr1[]={1,2,3,4,5};IntArr2[]=NewInt[5];System.Arraycopy(Arr1, 0, arr2, 0, 5); for (int i = 0; I < arr2. Length; I+ +) { System. Out. println(arr2[i]); } } }< /c21>
Arrays class
Some basic operations of an array, such as sorting, searching, and comparing, are common. The array is provided in Java and the class can assist you with these operations,
Array is a class located in the Java.util package, and he provides several methods that can be used directly.
-
sort()Helps you sort the specified array by using the Quick Sort method
-
binarySearch()Allows you to search a sorted array for $ two, returns the index of the value if the specified value is found, or returns a negative
-
fill()When you configure an array, the default values are given according to the data type. For example, an array of integers has an initial value of 0,
You can use the Arrays.fill () method to set all elements to the specified value
equals()Compares the values of elements in two arrays if they are all equal, or returns False if they are true
ImportJava.util.Scanner;ImportJava.util.Arrays;PublicClassT{PublicStaticvoidMain(String[]Args){ScannerScanner=NewScanner(System.Inch);Int[]Arr={93,5,3,55,57,7,2,73,41,91};System.Out.Print("Before sorting:");For(IntI=0;I<Arr.Length;I++){System.Out.Print(Arr[I]+" ");}System.Out.println();Arrays.Sort(Arr);System.Out.Print("After sorting:");For(IntI=0;I<Arr.Length;I++){System.Out.Print(Arr[I]+" ");}System.Out.Print("\ n Please enter a search value:");IntKey=Scanner.Nextint();IntFind=-1;If((Find=arrays. (arrkey> -1) { System.. ( "find values in index" + find + "position" } else system. Out. ( "specified value not found" } } /span>
Execution Result:
排序前: 93 5 3 55 57 7 2 73 41 91 排序后: 2 3 5 7 41 55 57 73 91 93 请输入搜索值:5 找到值于索引 2 位置
The following uses arrays to populate and compare arrays
ImportJava.util.Arrays;PublicClassT{PublicStaticvoidMain(String[]Args){Int[]Arr1=NewInt[10];Int[]Arr2=NewInt[10];Int[]Arr3=NewInt[10];Arrays.Fill(Arr1,5);Arrays.Fill(Arr2,5);Arrays.Fill(Arr3,10);System.Out.Print("ARR1:");For(IntI=0;I<Arr1.Length;I++){System.Out.Print(Arr1[I]+" "} system.. ( "\NARR1 = arr2?") + arrays. Equals (arr1arr2system.. ( "arr1 = Arr3?") + arrays. Equals (arr1arr3} } /span>
Execution results
arr1: 5 5 5 5 5 5 5 5 5 5 arr1 = arr2 ? true arr1 = arr3 ? false
Copyright NOTICE: This article uses the BY-NC-SA agreement to authorize, reproduced the Wheat Field Technical blog article please indicate the source
Original address: Http://itmyhome.com/2015/03/java-study-notes-arrays
Java Learning Note 04--array