Common methods in the Arrays class

The Arrays class contains various methods for manipulating arrays. The following are some of the more commonly used methods

Table of contents

Guide package

Use of common methods 

1.Arrays.toString()

2.Arrays.fill()

 3.Arrays.sort()

4. Arrays.equals()

5.Arrays.binarySearch()

6.Arrays.copyOf() 和Arrays.copyOfRange()


Guide package

The Arrays class is located in the java.util package, so if you want to use the methods in the Arrays class, you must first import the package

import java.util.Arrays;

Use of common methods 

1.Arrays.toString()

Returns a string representation of the contents of the specified array

public class Demo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        //将数组arr中内容转换为字符串
        String str = Arrays.toString(arr);
        System.out.println(str);
    }
}

 output result

2.Arrays.fill()

Used to replace the original element of the array

 

public class Demo {
    public static void main(String[] args) {
        int[] arr = new int[5];
        //将1赋值给数组中的每一个元素
        Arrays.fill(arr,1);
        System.out.println(Arrays.toString(arr));
    }
}

output result

 

 

public class Demo {
    public static void main(String[] args) {
        int[] arr = new int[5];
        //将1赋值给数组中指定元素
        //从1索引开始到3索引(不包括)处
        Arrays.fill(arr,1,3,1);
        //第一个参数是数组,第二个参数是起始索引,第三个参数是结束索引,
        //第四个参数是指定的值
        System.out.println(Arrays.toString(arr));
    }
}

output result

 

 

 3.Arrays.sort()

Used to sort the specified array in numerical order (ascending by default)

public class Demo {
    public static void main(String[] args) {
        int[] arr = {13,5,6,8,2};
        System.out.println("调用sort方法前:" + Arrays.toString(arr));
        //按照升序排序
        Arrays.sort(arr);
        System.out.println("调用sort方法后:" + Arrays.toString(arr));
    }
}

output result

 Note: When sorting strings, the ASCII code value of each character is compared, not the string length

public class Demo {
    public static void main(String[] args) {
        String[] strings = {"abc","ad","ac"};
        Arrays.sort(strings);
        System.out.println(Arrays.toString(strings));
    }
}

output result

4. Arrays.equals()

It is used to compare whether the contents of two arrays are equal. The return type is Boolean. If the contents of the two arrays are the same, return true, and if they are different, return false.

public class Demo {
    public static void main(String[] args) {
        char[] chars1 = {'a','b','c'};
        char[] chars2 = {'a','b','d'};
        boolean isSame = Arrays.equals(chars1,chars2);
        System.out.println(isSame);
    }
}

5.Arrays.binarySearch()

Used to find elements in the array, the return value is int

Note: The array must be sorted, otherwise an error will occur

public class Demo {
    public static void main(String[] args) {
        //数组一定是排好序的
        int[] arr = {2,4,10,20,33};
        //传入两个参数,前一个为要查找的数组,后一个为要查找的元素
        int a = Arrays.binarySearch(arr,2);//0
        //找到该元素,则返回该元素的索引
        //返回值>=0,则说明能找到该元素

        int b = Arrays.binarySearch(arr,3);//-2
        //未找到该元素,返回-x
        //3介于2和4之间,若要将3放入数组中,是该数组的第二个元素,则返回-2

        int c = Arrays.binarySearch(arr,0,3,20);//-4
        //在0索引到3(不包括)索引位置查找该元素
    }
}

6.Arrays.copyOf() 和Arrays.copyOfRange()

for copying arrays

Arrays.copyOf() 

public class Demo {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        //第一个参数为原数组,第二个参数是拷贝长度,返回值是一个新数组
        //默认从0索引位置开始拷贝
        int[] arr2 = Arrays.copyOf(arr1,3);
        System.out.println(Arrays.toString(arr2));
    }
}

output result

Arrays.copyOfRange(int[] original, int from, int to)

public class Demo {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        //第一个参数:原数组,第二个参数:开始拷贝索引,第三个参数:结束拷贝索引(不包括)
        int[] arr2 = Arrays.copyOfRange(arr1,1,3);
        System.out.println(Arrays.toString(arr2));
    }
}

 output result

 

Guess you like

Origin blog.csdn.net/2301_76161469/article/details/130071024