赞
踩
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class day28 {
public static void main(String[] args) {
// 泛型使用类的形式
List<Integer> list = new ArrayList<Integer>();
list.add(8);
list.add(5);
list.add(9);
list.add(1);
for (int i:list){
System.out.print(i+" ");
}
System.out.println();
System.out.println("***********************");
// list排序
Collections.sort(list);
for (int i:list){
System.out.print(i+" ");
}
System.out.println();
System.out.println("***********************");
}
}
// 对存在list字符串进行排序
List<String> list = new ArrayList<String>();
list.add("orange");
list.add("red");
list.add("blue");
list.add("yellow");
System.out.println("Before sorting:");
for (String s : list) {
System.out.print(s + " ");
}
// 按照字母顺序排序
Collections.sort(list);
System.out.println("After sorting:");
for (String s:list){
System.out.print(s+" ");
}
package day2;
public class Cat {
private String name;
private int month;
private String species;
//构造方法
public Cat(String name, int month, String species) {
this.name = name;
this.month = month;
this.species = species;
}
//getter与setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", month=" + month +
", species='" + species + '\'' +
'}';
}
}
package day2;
import java.util.Comparator;
public class NameCompatator implements Comparator<Cat> {
@Override
public int compare(Cat o1, Cat o2) {
/**
* 按照名字升序
*/
String name1=o1.getName();
String name2=o2.getName();
// 对字符串进行比较,~ 倒序
return name1.compareTo(name2);
}
}
package day2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class day28 {
public static void main(String[] args) {
/**
* 例题:对宠物猫分别按照名字升序,年龄降序排列
*/
Cat huahua = new Cat("huahua",6,"波斯");
Cat fanfan = new Cat("fanfan",2,"波斯");
Cat maomao = new Cat("maomao",4,"波斯");
List<Cat> catList=new ArrayList<Cat>();
catList.add(huahua);
catList.add(fanfan);
catList.add(maomao);
System.out.println("Before sorting:");
for (Cat cat:catList){
System.out.println(cat);
}
// 名字升序
Collections.sort(catList,new NameCompatator());
System.out.println("After sorting:");
for (Cat cat:catList){
System.out.println(cat);
}
}
}
package day2;
import java.util.Comparator;
public class AgeComparator implements Comparator<Cat> {
@Override
public int compare(Cat o1, Cat o2) {
// 按照年龄降序
int age=o1.getMonth();
int age2=o2.getMonth();
return age2-age;
}
}
// 按照年龄进行降序
Collections.sort(catList,new AgeComparator());
System.out.println("Age:After sorting:");
for (Cat cat:catList){
System.out.println(cat);
}
package day2;
public class Goods implements Comparable<Goods>{
private String id;
private String name;
private double price;
public Goods(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
/**
* 商品价格降序
* @param o
* @return new Double(price2-price1).intValue()
*/
@Override
public int compareTo(Goods o) {
// 取出商品价格
double price1=this.getPrice();
double price2=o.getPrice();
return new Double(price2-price1).intValue();
}
}
Goods g1=new Goods("s01","手机",2000);
Goods g2=new Goods("s02","冰箱",5000);
Goods g3=new Goods("s03","电视剧",3000);
List<Goods> goodsList=new ArrayList<Goods>();
goodsList.add(g1);
goodsList.add(g2);
goodsList.add(g3);
System.out.println("Before sorting:");
for (Goods goods:goodsList){
System.out.println(goods);
}
Collections.sort(goodsList);
System.out.println("After sorting:");
for (Goods goods:goodsList){
System.out.println(goods);
}
Before sorting:
Goods{id=s01, name='手机', price=2000.0}
Goods{id=s02, name='冰箱', price=5000.0}
Goods{id=s03, name='电视剧', price=3000.0}
After sorting:
Goods{id=s02, name='冰箱', price=5000.0}
Goods{id=s03, name='电视剧', price=3000.0}
Goods{id=s01, name='手机', price=2000.0}
int[] arrs
Array.sort(arrs);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。