Comparable接口
创建Person类
public class Person {
public String name;
public double height;
public int age;
public String address;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name,double height,int age,String address) {
// TODO Auto-generated constructor stub
this.address=address;
this.age=age;
this.height=height;
this.name=name;
}
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + ", age=" + age + ", address=" + address + "]";
}
}
创建Test01类,向Per添加值
public class Test01 {
public static void main(String[] args) {
ArrayList<Person> list=new ArrayList<>();
Person p1=new Person("张飞",190.0,22,"小石城301");
Person p2=new Person("李白",185.5,25,"小石城201");
Person p3=new Person("曹操",165.5,24,"小石城401");
list.add(p2);
list.add(p1);
list.add(p3);
System.out.println(list);
}
输出:
[Person [name=李白, height=185.5, age=25, address=小石城201],
Person [name=张飞, height=190.0, age=22, address=小石城301],
Person [name=曹操, height=165.5, age=24, address=小石城401]]
当list容器添加的元素对象是自己是属于自己写的类时,排序出现问题
原因 是Java不知道应该怎样为Student对象排序, 是应该按名字排序? 还是按age来排序?
编译时出错:
- Student没有实现Comparable接口.
Student类的对象默认是不可以比较的. 除非它实现了Comparable接口.
如果你想要类的对象支持比较(排序), 就必须实现Comparable接口.
Comparable接口的实现及用法
修改并重写person类,实现Comparable接口并重写compareTo方法.
public class Person implements Comparable<Person>{
public String name;
public double height;
public int age;
public String address;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name,double height,int age,String address) {
// TODO Auto-generated constructor stub
this.address=address;
this.age=age;
this.height=height;
this.name=name;
}
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + ", age=" + age + ", address=" + address + "]";
}
// 手动创建person类 排序规则
//对年龄进行排序
@Override
public int compareTo(Person s) {
// TODO Auto-generated method stub
//比较本类对象和传入参数的对象的属性
//本类对象的年龄:this.age
//传入类对象的年龄:s.age
return this.age-s.age;
}
- 重写的compareTo(Object o)方法内. 根据Person的age来比较的, 也就是说跟姓名无关了.
Test01类
public class Test01 {
public static void main(String[] args) {
ArrayList<Person> list=new ArrayList<>();
Person p1=new Person("张飞",190.0,22,"小石城301");
Person p2=new Person("李白",185.5,25,"小石城201");
Person p3=new Person("曹操",165.5,24,"小石城401");
list.add(p2);
list.add(p1);
list.add(p3);
Collections.sort(list);
System.out.println(list);
}
输出:
[Person [name=张飞, height=190.0, age=22, address=小石城301],
Person [name=曹操, height=165.5, age=24, address=小石城401],
Person [name=李白, height=185.5, age=25, address=小石城201]]
再编译执行, 就能见到List容器内的Person对象已经根据age来排序了.
Comparable接口简介.
Comparable 接口内部只有1个要重写的关键的方法.
就是
int compareTo(T o)
这个方法返回1个Int数值,
例如 i = x.compareTo(y)
如果i=0, 也表明对象x与y排位上是相等的
如果返回数值i>0 则意味者, x > y啦,
反之若i<0则 意味x < y
参考:https://www.jianshu.com/p/5cb902e4b5e4
Collections工具类
Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序等操作。
void reverse(List list):反转
void shuffle(List list),随机排序
void sort(List list),按自然排序的升序排序
ArrayList<Integer> nums=new ArrayList<>();
for (int i = 0; i < 10; i++) {
nums.add(i);
}
System.out.println(nums);
//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
//打乱顺序
Collections.shuffle(nums);
System.out.println(nums);
//[8, 6, 2, 0, 7, 4, 3, 5, 9, 1]
////反转集合中的数据
Collections.reverse(nums);
System.out.println(nums);
//[1, 9, 5, 3, 4, 7, 0, 2, 6, 8]
//将集合元素排序
Collections.sort(nums);
System.out.println(nums);
//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Arrays类
1、boolean equals(array1,array2):比较两个数组是否相等。
String[] str1 = {"1","2","3"};
String[] str2 = {"4","5","6"};
System.out.println(Arrays.equals(str1, str2));
输出:
false
2、List集合 转换成为数组
创建一个和集合中元素数量一样多的数组
List<String> list = new ArrayList<>();
list.add("1111");
list.add("2222");
String[] array = new String[list.size()];
list.toArray(array);
System.out.println(Arrays.toString(array));
输出:
[1111, 2222]