Java Learning Note 04--array
The method of array copying is to use the arraycopy () method provided by the system class with the following syntax:
System.arraycopy (object src, int srcpos, object dest, int destpos, int length);
System.arraycopy ( source Array, starting position in source array, target array, starting position in target data, number of array elements to copy);
[Java]View Plaincopy
- Public class T {
- public static void Main (String args[]) {
- int arr1[] = {1,2,3,4,5};
- int arr2[] = new int[5];
- System.arraycopy (arr1, 0, ARR2, 0, 5);
- For (int i = 0; i < arr2.length; i++) {
- System.out.println (Arr2[i]);
- }
- }
- }
Arrays class
Some basic operations of an array, such as sorting, searching, and comparing, are common. The array is provided in Java to 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, and returns the index of the value if the specified value is found, otherwise a negative number is returned
Fill () When you configure an array, the default value is given according to the data type. For example, an array of integers has an initial value of 0, and you can use the Arrays.fill () method to set all elements to the specified value
Equals () compares the values of the elements in the two array if they are all equal, and returns true if they are, otherwise false
[Java]View Plaincopy
- Import Java.util.Scanner;
- Import Java.util.Arrays;
- Public class T {
- public static void Main (string[] args) {
- Scanner Scanner = new Scanner (system.in);
- int[] arr = { 5, 3, 2, 7, 91};
- System.out.print ("before sorting:");
- For (int i = 0; i < arr.length; i++) {
- System.out.print (Arr[i] + "");
- }
- System.out.println ();
- Arrays.sort (arr);
- System.out.print ("after sorting:");
- For (int i = 0; i < arr.length; i++) {
- System.out.print (Arr[i] + "");
- }
- System.out.print ("\ n Please enter search value:");
- int key = Scanner.nextint ();
- int find =-1;
- if (find = Arrays.binarysearch (arr, key)) >-1) {
- System.out.println ("Find value in Index" + Find + "location");
- } Else
- System.out.println ("No specified value found");
- }
- }
Execution Result:
[Java]View Plaincopy
- before sorting: 93 5 3 55 57 7 2 73 41 91&NBSP;&NBSP;&NBSP;
- after sorting: 2 3 5 7 41 55 57 73 91 93
- Enter search value: 5
- found value in index 2 location
The following uses arrays to populate and compare arrays
[Java]View Plaincopy
- Import Java.util.Arrays;
- Public class T {
- public static void Main (string[] args) {
- int[] arr1 = new int[10];
- int[] arr2 = new int[10];
- int[] Arr3 = new int[10];
- Arrays.fill (ARR1, 5);
- Arrays.fill (ARR2, 5);
- Arrays.fill (ARR3, 10);
- System.out.print ("arr1:");
- For (int i = 0; i < arr1.length; i++) {
- System.out.print (Arr1[i] + "");
- }
- System.out.println ("\narr1 = arr2?" + arrays.equals (arr1, arr2));
- System.out.println ("arr1 = arr3?" + arrays.equals (arr1, ARR3));
- }
- }
Execution results
[Java]View Plaincopy
- ARR1: 5 5 5 5 5 5 5 5 5 5
- arr1 = arr2? true
- arr1 = ARR3? false
Java Learning Note 04--array