java操作数组的工具类-Arrays

static int binarySearch(type[] a, type key) 使用二分搜索法来搜索key元素在数组中的索引;若a数组不包括key,返回负数。(该方法必须已按升序排列后调用)。

static int binarySearch(type[] a, int fromIndex, int toIndex, type key) 使用二分搜索法来搜索key元素在数组中从fromIndex到toIndex的索引;若a数组不包括key,返回负数。(该方法必须已按升序排列后调用)。

static boolean[] copyOf(type[] original, int newLength) 复制指定的数组见下面备注

static byte[] copyOfRange(type[] original, int from, int to) 将数组的指定范围复制到一个新数组。

static boolean equals(type[] a, type[] a2) 如果两个数组长度相等和元素一一相等,则返回 true

static void fill(type[] a, type val) 将a数组所有元素都赋为val。

static void fill(type[] a, int fromIndex, int toIndex, type val) 将a数组从formIndex 到tiondex索引之间的元素都赋为val。 

static void sort(type[] a) //sort(int[] arr)对指定的数组按数字升序进行排序。

static void sort(type[] a, int fromIndex, int toIndex) 对指定数组的从formIndex 到tiondex索引之间的元素按数字升序进行排序。

static String toString(type[] a) 返回指定数组内容的字符串表示形式。多个数组元素之间用英文逗号或空格隔开。

我的总结:使用数组工具类可以节省时间,提高效率,注意常查阅api

Eg:

package reviewDemo;

import java.util.Arrays;

//使用Arrays类

public class Demo4 {

    public static void main(String[] args) {

        int[] age = new int[] { 12, 26, 3, 60, 55, 6, 48, 4, 98 };

        System.out.println(Arrays.toString(age));//直接打印出数组的方法

       

        int []age2 = {1,2,3,4,5,6,98,65,23};

        int i = Arrays.binarySearch(age2, 98);

        System.out.println(i);

    }

}

猜你喜欢

转载自www.cnblogs.com/fanweisheng/p/11131080.html