Arrays类中有很多实用与数组的static方法:
1.equals()用于比较两个数组是否相等(deepEquals()用于多维数组)
2.fill();用于快速在数组中填充数据
3.sort():对数组排序
4.hinarySearch():在已经拍好的数组中查找元素
5.toString():产生数组的String表示
6.hashCode()产生数组的散列码
7.asList()接受任意的序列或数组将其转变为LIst集合;
1.数组复制;
System.arraycopy()针对所以的类型做了重载,用于复制数组。同时原数组的数据不会丢失。
package com.package16;import java.util.Arrays;public class CopyArrayDemo {public static void main(String[] args) {int [] i1=new int[7];int [] i2=new int[10];Arrays.fill(i1,47);Arrays.fill(i2,99);System.out.println(Arrays.toString(i1));System.out.println(Arrays.toString(i2));System.out.println("将i1的数组赋值到i2中后:");System.out.println("被复制后的i1"+Arrays.toString(i1));System.out.println(Arrays.toString(i1));System.out.println("复制后的i2"+Arrays.toString(i2));int[] k=new int[5];Arrays.fill(k,11);System.out.println("复制前的k"+Arrays.toString(k));System.arraycopy(i1,0,k,0,k.length);System.out.println("复制后的k"+Arrays.toString(k));}}
System.arraycopy()有个参数:1.源数组:需要复制的数组,后跟开始位置2.目标数组:后跟开始位置3.赋值长度
2.数组的比较
Arrays提供了重载后的equals方法(),用来比较整个数组。数组相等的条件是元素个数比较相等,对于位置的元素也要相等。对于基本类型的比较,使用的是对应的包装类型中重写的equals方法
package com.package16;import java.util.Arrays;public class ComparingArrays {public static void main(String[] args) {int [] arrays1=new int[10];int [] arrays2=new int[10];Arrays.fill(arrays1,11);Arrays.fill(arrays2,11);System.out.println(Arrays.equals(arrays1, arrays2));arrays2[3]=13;//改变任意一个位置的元素System.out.println("修改arrays2中的一个元素后"+Arrays.equals(arrays1, arrays2));arrays2=new int[11];//改变数组长度后System.out.println("修改arrays2中的数组长度后:"+Arrays.equals(arrays1, arrays2));String[]str1=new String[4];Arrays.fill(str1,"hi");String[]str2={new String("hi"),new String("hi"),new String("hi"),new String("hi")};System.out.println("str1 和 str2比较"+Arrays.equals(str1, str2));}}
3.策略设计模式
在排序中经常遇到,可以报一种不变的排序算法抽出来,用来去比较各种对象
3.1 抽出排序算法
package com.package16;import java.util.Arrays;public class ComTypeDemo implements Comparable<ComTypeDemo>{int i;ComTypeDemo(int i){this.i=i;}@Overridepublic int compareTo(ComTypeDemo o) {return i-o.i;//从小到大排序}@Overridepublic String toString() {return "ComTypeDemo"+ i;}
3.2对不同的对象进行比较
public static void main(String[] args) {ComTypeDemo [] comTypeDemos={new ComTypeDemo(6),new ComTypeDemo(3),new ComTypeDemo(4),new ComTypeDemo(2)};System.out.println(Arrays.toString(comTypeDemos));Arrays.sort(comTypeDemos);System.out.println(Arrays.toString(comTypeDemos));}
4.数组的排序
4.1排序
①使将要排序的类实现comparable接口中的comparato方法,让这个类自身带有排序
public class ComTypeDemo implements Comparable<ComTypeDemo>{int i;ComTypeDemo(int i){this.i=i;}@Overridepublic int compareTo(ComTypeDemo o) {return i-o.i;}@Overridepublic String toString() {return "ComTypeDemo"+ i;}public static void main(String[] args) {ComTypeDemo [] comTypeDemos={new ComTypeDemo(6),new ComTypeDemo(3),new ComTypeDemo(4),new ComTypeDemo(2)};System.out.println(Arrays.toString(comTypeDemos));Arrays.sort(comTypeDemos);System.out.println(Arrays.toString(comTypeDemos));}}
注意:所有的包装类型都实现了comparable接口,所以他们自身都带排序
Integer []integers=new Integer[5];integers[0]=45;integers[1]=15;integers[2]=35;integers[3]=5;integers[4]=85;System.out.println(Arrays.toString(integers));Arrays.sort(integers);System.out.println(Arrays.toString(integers));Output:[45, 15, 35, 5, 85][5, 15, 35, 45, 85]
②在不修改代码的前提下,添加类实现comparator接口中的compara方法,从而对目标进行排序
创建排序类
public class ComparatorDemo implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {return o1.age-o2.age;}}
不修改student类对其进行排序
public class Student {private String name;public int age;Student(String name,int age){this.name=name;this.age=age;}@Overridepublic String toString() {return name+":"+age+" ";}public static void main(String[] args) {Student[] students = {new Student("AA", 12), new Student("BB", 13), newStudent("cc", 10)};System.out.println(Arrays.toString(students));Arrays.sort(students, new ComparatorDemo());System.out.println(Arrays.toString(students));Output:[AA:12 , BB:13 , cc:10 ][cc:10 , AA:12 , BB:13 ]
4.2在排序好的数组中查找
对于已经排序过的数组我们可以使用Arrays.binarySearch方法快速查找某个元素的索引,如果该数组没有进行排序,那么该发放便无任何作用
public static void main(String[] args) {Integer []integers=new Integer[5];integers[0]=45;integers[1]=15;integers[2]=35;integers[3]=5;integers[4]=85;System.out.println(Arrays.toString(integers));System.out.println("未排序"+Arrays.binarySearch(integers, 15));Arrays.sort(integers);System.out.println(Arrays.toString(integers));System.out.println("排序后"+Arrays.binarySearch(integers, 15));}OUtput:[45, 15, 35, 5, 85]未排序-1[5, 15, 35, 45, 85]排序后1
我们发现只有排序后查找的元素索引才正确。
