2018/12/18
在一个列表中存储以下元素:apple,grape,banana,pear
3.1 返回集合中的最大的和最小的元素
3.2 将集合进行排序,并将排序后的结果打印在控制台上
package homework1;
public class Fruit implements Comparable<Fruit> {
public String toString() {
return "Fruit [fruit=" + fruit + "]";
}
private String fruit;
public String getFruit() {
return fruit;
}
public void setFruit(String fruit) {
this.fruit = fruit;
}
public Fruit(String fruit) {
super();
this.fruit = fruit;
}
public Fruit() {
super();
}
public int compareTo(Fruit o) {
return this.fruit.compareTo(o.fruit);
}
}
package homework1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Test3 {
public static void main(String[] args) {
ArrayList<Fruit> list = new ArrayList<>();
Fruit f1 = new Fruit("apple");
Fruit f2 = new Fruit("grape");
Fruit f3 = new Fruit("banana");
Fruit f4 = new Fruit("pear");
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
Fruit max = Collections.max(list);
System.out.println(max);
Fruit min = Collections.min(list);
System.out.println(min);
Collections.sort(list);
System.out.println(list);
}
}