java
public static void sortMaoPao(int[] ints, Comparator comparator) {
int temp;
for (int i = 0; i < ints.length -1; i++) {
for (int j = 0; j < ints.length-i-1; j++) {
if(comparator.compare(ints[j],ints[j+1]) > 0) {
temp = ints[j];
ints[j] = ints[j+1];
ints[j+1] = temp;
}
}
}
}
java
// 核心代码是
Arrays.sort(books, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Book book1 = (Book) o1;
Book book2 = (Book) o2;
// 价格从大到小
return book2.getPrice()-book1.getPrice()>0?1:-1;
// 书名从小到大
// return book1.getName().length() - book2.getName().length();
}
});
java
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
if(next instanceof Book) {
Book book = (Book) next;
System.out.println(book);
}
}
java
/**
ArrayList 本质操作的是 elementData
transient 表示的是短暂的瞬时的表示该属性不会被序列化
**/
transient Object[] elementData