java-数组工具类

java.util.Arrays
1.public static int binartSearch(int [] a, int key)
    1)使用二分搜索法来搜索指定的int型数组,以获得指定的值
    2)要求数组是有序的,否则结果不一定
    3)如果key在a数组中存在,就返回找到的第一个的下标,如果不存在返回负数
2.public static int[] copyOf(int[] original,int newLength)
    意思是:从original原数组中复制得到一个新数组,新数组的长度为newLength
3.public static int[] copyOfRange(int[] ariginal, int from, int to)
    意思是:1)从original原数组中把[from,to)复制出来,构成一个新数组
           2)要求from必须在original的合理下标范围内,to可以超过长度
4.public static boolean equals(int[] a,int[] a2)
    意思是:比较两个数组的长度和内容是否一致
5.public static void fill(int[] a, int val)
    意思是:把a数组的元素用val填充
6.public static void sort(int[] a)
    意思是:把数组a按照升序排列
7.public static String toString(int[] a)
    意思是:把a数组的元素遍历,拼接为一个字符串返回,比如[1,3,5]
和数组有关的常用方法:java.lang.System类中
# Object 是所有引用数据类型的根父类,那么根据多态,Object类型的变量,形参就可以接收任意的引用类型的对象,包括数组

public static native void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
# 第一个参数:src 原数组
  第二个参数:srcPos 从原数组的[srcPos]下标开始复制
  第三个参数:dest 目标数组对象
  第四个参数:destPos 目标数组从[destPos]开始存储
  第五个参数:length 表示从元数组复制几个元素
说明:如果src和dest是同一个数组的话,那么就会实现数组的元素的移动效果
  如果secPos > destPos, 往左移动,一般用于删除
  如果secPos < destPos, 往右移动,一般用于插入
import java.util.Arrays;

public class TestSystemArrayCopy {
    public static void main(String[] args) {
        String [] arr = {"hello", "world", "java", null, null};
        int total = 3;
        System.arraycopy(arr,1,arr,0,2);
//        System.arraycopy(arr,1,arr,2,2);
        System.out.println(Arrays.toString(arr));
        arr[2] = null;
    }
}
When nothing seems to help, I go look at a stonecutter hammering away at his rock, perhaps a hundred times without as much as a crack showing in it. Yet at the hundred and first blow it will split in two, and I know it was not that blow that did it, but all that had gone before. -- Jacob Riis
原文地址:https://www.cnblogs.com/xhwy-1234/p/12512286.html