Arrays array operation tools

Arrays of several commonly used methods in the class

Arrays are Java-based operating tool array
when using java.util.Arrays need to import Import
1.1 array to a string of Arrays.toString (arrays)
arrays may be int [] arr; double [] arr; float [] arr; char [] arr; byte [] arr ; boolean [] arr; long [] arr; short [] arr; eight basic types of data array.
The contents of the array into a string, there may be output Ststem.out.println (Arrays.toString (specified array))

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        double[] arr2 = {1.0,2.0,3.0,4.0,5.0,6.0,6.6};
        boolean[] arr3 = {true,false};
        char[] arr4 = {'h','e','l','l','o'};
        long[] arr5 = {1000,2000,3000};
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        System.out.println(Arrays.toString(arr3));
        System.out.println(Arrays.toString(arr4));
        System.out.println(Arrays.toString(arr5));
        /**输出结果为:
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.6]
        [true, false]
        [h, e, l, l, o]
        [1000, 2000, 3000]*/
    }
}

1.2 Copy array Arrays.copyOf (int [] Original, int newLength)
int [] array to copy Original, int length newLength to be copied
, of course copy the array may be copied to other array types

        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        int[] arr2 = Arrays.copyOf(arr1,10);//可以选择复制的长度

Note:
(. 1) .newLength parameter can not be negative
(2) .original is not the null (empty)

		int[] arr2 = Arrays.copyOf(arr1,-1);//异常:NegativeArraySizeException  
		
		int[] arr3 = null;
      	int[] arr2 = Arrays.copyOf(arr3,10);//异常:NullPointerException

1.3 Sorting an array Arrays.sort ()
Arrays.sort (int [] arr) only when a parameter array, the entire contents of the array will be the default in ascending order. Of course also be sorted selectively, only a part of the array is sorted, Arrays.sort (int [] arr, int fromIndex, int toIndex), wherein fromIndex toIndex start and end indexes of the index to be sorted and
particularly in this section is a a closed left and right open interval - [fromIndex, toIndex), we can verify through code:

    public static void main(String[] args) {
        int[] arr1 = {2, 5, 1, 4, 3, 9, 6, 7, 8, 10};
        //formIndex:2
        //toIndex:8
        Arrays.sort(arr1, 2, 8);
        System.out.println(Arrays.toString(arr1));
   	}
   	//运行结果:[2, 5, 1, 3, 4, 6, 7, 9, 8, 10]
   	//         0, 1, 2, 3, 4, 5, 6, 7, 8, 9

1.4 binary search Arrays.binarySearch ()
binary search a number, provided that this orderly array, so the use Arrays.binarySearch will be sorted (Arrays.sort) The first array
to find a number in a specified array, if found return this index number, if not found returns the index position of the computer that should be located.

    public static void main(String[] args) {
        int[] arr1 = {2, 5, 1, 4, 3, 9, 6, 7, 8, 10};
        int ret = Arrays.binarySearch(arr1,9);
        /*
        同样的也可以选择查找范围
        fromIndex = 1;
        toIndex = 8;
        key = 9;
        int ret = Arrays.binarySearch(arr1,1,8,9);
        */
   	}

It fills 1.5 Arrays.fill ()
will be filled with all or a portion of the contents of the specified array

        int[] arr = new int[10];
        Arrays.fill(arr,99);
        System.out.println(Arrays.toString(arr));
        //[99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
        //fromIndex = 0;
        //toIndex = 4;
        //[0,4)
        Arrays.fill(arr,0,4,66);
        System.out.println(Arrays.toString(arr));
        //[66, 66, 66, 66, 99, 99, 99, 99, 99, 99]

The results again confirm the output toIndex is not eligible, the left and right opening and closing section
1.6 is determined whether the same array Arrays.equals (int [] arr1, int
[] arr2) rule:
if both arrays contain the same elements in the same order, they are equal. Returns true, false otherwise

        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        int[] arr2 = {2,1,5,3,4,8,6,7,9,10};
        System.out.println(Arrays.equals(arr1,arr2));//false
        int[] arr3 = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(Arrays.equals(arr1,arr3));//true
Published 20 original articles · won praise 9 · views 905

Guess you like

Origin blog.csdn.net/weixin_44915811/article/details/102713000