Java Arrays 用法

目录

1. 将数组转成集合对象

2. 在数组中二分查找指定元素key

3. 将数组的一部分拷贝出来( newLength 这个是从被拷贝数组头元素开始算)

4. 将数组的一部分指定范围拷贝出来(有区别于上面)

5. 判断两个数组是否相等(内部元素是否完全相同)个人感觉有点鸡肋

6. 将数组所有元素用指定元素替换(相当于初始化操作)

7. 将数组的指定位置元素用指定元素替换(相当于部分初始化操作)

8. 将数组进行内部排序 个人为这个 api 疯狂打 call(虽然对数组排序不难,但是我还是不想写,嘻嘻,用 api 老爽了 !)

9. 将数组进行输出操作 (用于调试的时候,特别方便)


1. 将数组转成集合对象

API: List<T> asList();

举例?:

String[] words = new String[]{"I", "Love", "China", "!"};
List<String> numberList = Arrays.asList(words);

2. 在数组中二分查找指定元素key

API: int binarySearch(int[] a, int key);

举例?:打印结果为,查找的元素在数组中的角标位置

 int[] numbers = new int[]{1, 2, 3, 4, 5, 6};
 System.out.println(Arrays.binarySearch(numbers, 5));

注意点?:对于自定义对象类型(非数值、非字符、等),需要添加比较器,例如?:对于自定义person数组的二分查找

class PersonComparator implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        return o1.age - o2.age;
    }
}
class Person{
    String name;
    int age;
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
}
public class Main {
    public static void main(String[] args) {
        Person[] persons = new Person[]{new Person("张三", 10), new Person("李四", 12), new Person("王五", 16)};
        System.out.println(Arrays.binarySearch(persons, new Person("王五", 16), new PersonComparator()));
    }
}

3. 将数组的一部分拷贝出来( newLength 这个是从被拷贝数组头元素开始算)

API: T[] copyOf(T[], int newLength);

String[] words = new String[]{"I", "Love", "China", "!"};
String[] subWords = Arrays.copyOf(words, 2);

4. 将数组的一部分指定范围拷贝出来(有区别于上面)

API: T[] copyOfRange(T[], int from, int to);

String[] words = new String[]{"I", "Love", "China", "!"};
String[] subWords = Arrays.copyOfRange(words, 1, 3);

温馨提示:Java关于数组等等的操作,都是包含头不包含尾,例如?面的代码,1,3 得到的 subWords = {"Love", "China"}

5. 判断两个数组是否相等(内部元素是否完全相同)个人感觉有点鸡肋

API: boolean equals(T[], T[]);

String[] words = new String[]{"I", "Love", "China", "!"};
String[] another = new String[]{"I", "Love", "China", "!!!"};
System.out.println(Arrays.equals(words, another));

注意点:如果比较的是两个自定义对象数组,对于自定义的对象要复写自身的 equals 方法

6. 将数组所有元素用指定元素替换(相当于初始化操作)

API: void fill(T[], T);

String[] words = new String[]{"I", "Love", "China", "!"};
Arrays.fill(words, "Yes");
        

7. 将数组的指定位置元素用指定元素替换(相当于部分初始化操作)

API: void fill(T[], int fromIndex, int toIndex, T);

String[] words = new String[]{"I", "Love", "China", "!"};
Arrays.fill(words, 1, 2, "Yes");

8. 将数组进行内部排序 个人为这个 api 疯狂打 call(虽然对数组排序不难,但是我还是不想写,嘻嘻,用 api 老爽了 !)

API: void sort(T[]); 排序方式:默认是从小到大

API: void sort(T[], comparator);自定义对象数组,需要给出比较器才能进行排序操作,比较器写法参照第二点

API: void sort(T[], int fromIndex, int toIndex); 指定部分排序

int[] numbers = new int[]{1, 3, 2, 5, 4};
Arrays.sort(numbers);

注意点:如果对自定义的对象数组进行排序的时候,需要写比较器,具体的比较器写法参照上面第二点

9. 将数组进行输出操作 (用于调试的时候,特别方便)

API: String toString(T[]);

int[] numbers = new int[]{1, 3, 2, 5, 4};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));

注意点:如果是对自定义的对象数组调用 Arrays.toString() 方法时,数组中的元素自身要实现 toString()方法

 

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值