标签:java turn play 基础 提交 bubuko lap 拷贝 hid
–不存储数据,而是在数据容器上,实现高效操作
• 排序
• 搜索
–Arrays类
–Collection类
–排序:对数组排序, sort/parallelSort。
–查找:从数组中查找一个元素, binarySearch。
–批量拷贝:从源数组批量复制元素到目标数组, copyOf
–批量赋值:对数组进行批量赋值, fill。
–等价性比较:判定两个数组内容是否相同, equals。
import java.util.Arrays; import java.util.Random; public class ArraysTest { public static void main(String[] args) { //testSort(); //SearchSort(); //testCopy(); //testFill(); testEquality(); } public static void testSort() { Random rm=new Random(); int[] a=new int[10]; System.out.println("=======测试排序======"); for(int i=0;i<a.length;i++) { a[i] = rm.nextInt(); } System.out.println("===排序前==="); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); System.out.println("===排序后==="); Arrays.sort(a); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); } public static void SearchSort() { Random rm=new Random(); int[] a=new int[10]; System.out.println("=======测试查找======"); for(int i=0;i<a.length;i++) { a[i] = rm.nextInt(); } a[8] = 1000; Arrays.sort(a); System.out.println("===查找前==="); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); //二分查找需要先进性排序 System.out.println("1000位置:"+Arrays.binarySearch(a, 1000)); } public static void testCopy() { Random rm=new Random(); int[] a=new int[10]; System.out.println("=======测试拷贝======"); for(int i=0;i<a.length;i++) { a[i] = rm.nextInt(); } int[] b=Arrays.copyOf(a, 6); //拷贝前6个元素到b中 System.out.println("===拷贝前a==="); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); System.out.println("===拷贝后b==="); for(int i=0;i<b.length;i++) { System.out.print(b[i] + ","); } System.out.println(); } public static void testFill() { int[] a = new int[10]; Arrays.fill(a, 100); Arrays.fill(a, 2, 8, 200); System.out.println("===========测试批量赋值============"); System.out.print("数组赋值后:"); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); } public static void testEquality() { int[] a = new int[10]; Arrays.fill(a, 100); int[] b = new int[10]; Arrays.fill(b, 100); System.out.println(Arrays.equals(a, b)); //数据一样时为true b[9] = 200; System.out.println(Arrays.equals(a, b)); //不一致为false } }
public static void testSort() { Random rm=new Random(); int[] a=new int[10]; System.out.println("=======测试排序======"); for(int i=0;i<a.length;i++) { a[i] = rm.nextInt(); } System.out.println("===排序前==="); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); System.out.println("===排序后==="); Arrays.sort(a); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); }
public static void SearchSort() { Random rm=new Random(); int[] a=new int[10]; System.out.println("=======测试查找======"); for(int i=0;i<a.length;i++) { a[i] = rm.nextInt(); } a[8] = 1000; Arrays.sort(a); System.out.println("===查找前==="); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); //二分查找需要先进行排序 System.out.println("1000位置:"+Arrays.binarySearch(a, 1000)); }
public static void testCopy() { Random rm=new Random(); int[] a=new int[10]; System.out.println("=======测试拷贝======"); for(int i=0;i<a.length;i++) { a[i] = rm.nextInt(); } int[] b=Arrays.copyOf(a, 6); //拷贝前6个元素到b中 System.out.println("===拷贝前a==="); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); System.out.println("===拷贝后b==="); for(int i=0;i<b.length;i++) { System.out.print(b[i] + ","); } System.out.println(); }
public static void testFill() { int[] a = new int[10]; Arrays.fill(a, 100); Arrays.fill(a, 2, 8, 200); System.out.println("===========测试批量赋值============"); System.out.print("数组赋值后:"); for(int i=0;i<a.length;i++) { System.out.print(a[i] + ","); } System.out.println(); }
public static void testEquality() { int[] a = new int[10]; Arrays.fill(a, 100); int[] b = new int[10]; Arrays.fill(b, 100); System.out.println(Arrays.equals(a, b)); //数据一样时为true b[9] = 200; System.out.println(Arrays.equals(a, b)); //不一致为false }
处理对象是 Collection及其子类 –排序:对List进行排序,sort。 –搜索:从List中搜索元素,binarySearch –批量赋值:对List批量赋值,fill。 –最大、最小:查找集合中最大/小值,max,min –反序:将List 反序排列,reverse
import java.util.ArrayList; import java.util.Collections; public class CollectionsTest { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(12); list.add(2); list.add(19); // 排序 Collections.sort(list); // 检索 System.out.println("元素所在的索引值是:" + Collections.binarySearch(list, 12)); //最大最小 System.out.println("最大值:" + Collections.max(list)); System.out.println("最小值:" + Collections.min(list)); Collections.reverse(list); //翻转不需要用到排序 Collections.fill(list, 100); //全部赋值为100 } }
–compareTo方法 > 返回1,==返回0,<返回-1 –Arrays和Collections在进行对象sort时,自动调用该方法
import java.util.Arrays; class Dog implements Comparable<Dog>{ int age; String name; public Dog(String n,int a) { name=n; age=a; } public int getAge() { return age; } public String getName() { return name; } public int compareTo(Dog another) { int flag; flag=name.compareTo(another.getName()); if(flag==0) { return age-another.getAge(); } return flag; } } public class CompareToTest { public static void main(String[] args) { Dog[] da=new Dog[3]; da[0]=new Dog("Tom",13); da[1]=new Dog("Som",27); da[2]=new Dog("Som",16); Arrays.sort(da); for(Dog d : da) { System.out.println(d.getName()+":"+d.getAge()); } } }
Som:16 Som:27 Tom:13
public static void main(String[] args) { LinkedList<Dog> dl = new LinkedList<Dog>(); dl.add(new Dog("Tom",13)); dl.add(new Dog("Som",27)); dl.add(new Dog("Som",16)); Collections.sort(dl); for(Dog d : dl) { System.out.println(d.getName()+":"+d.getAge()); } }
例如:获取的是class,无法修改源码
–compare方法 > 返回1,==返回0,<返回-1 –Comparator比较器将作为参数提交给工具类的sort方法
import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; class Dog{ //不可修改,不能添加compareTo,不能继承接口 int age; String name; public Dog(String n,int a) { name=n; age=a; } public int getAge() { return age; } public String getName() { return name; } } class MyComparator implements Comparator<Dog>{ //需要我们自己实现一个比较器 public int compare(Dog one,Dog another) { int flag; flag = one.getName().compareTo(another.getName()); if(flag==0) { return one.getAge()-another.getAge(); } return flag; } } public class CompareToTest { public static void main(String[] args) { LinkedList<Dog> dl = new LinkedList<Dog>(); dl.add(new Dog("Tom",13)); dl.add(new Dog("Som",27)); dl.add(new Dog("Som",16)); Collections.sort(dl,new MyComparator()); for(Dog d : dl) { System.out.println(d.getName()+":"+d.getAge()); } } }
Som:16 Som:27 Tom:13
JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)
标签:java turn play 基础 提交 bubuko lap 拷贝 hid
原文地址:https://www.cnblogs.com/ssyfj/p/10220635.html