迭代器接口:
package zz.itheima.comparator;
public interface Iterator {
public boolean hasNext();
public int next();
}
package zz.itheima.comparator;
public interface Interable {
public Iterator iterator();
}
ArrList类:
package zz.itheima.comparator;
public class ArrList {
private int[] num;
private int index;
public ArrList() {
num = new int[10];
index = 0;
}
public void add(int o){
for (int i = 0; i < num.length; i++) {
if (num[i]==0) {
num[i] = o;
break;
}
}
}
public int get(int index){
return num[index];
}
public int size(){
return num.length;
}
public Iterator iterator(){
return new Iterator0();
}
class Iterator0 implements Iterator{
@Override
public boolean hasNext() {
if (index<num.length) {
return true;
}else {
return false;
}
}
@Override
public int next() {
return num[index++];
}
}
}
TestArrList :
package zz.itheima.comparator;
public class TestArrList {
public static void main(String[] args) {
ArrList al = new ArrList();
al.add(10);
al.add(18);
System.out.println(al.get(0));
for (int i = 0; i < al.size(); i++) {
System.out.println(al.get(i));
}
System.out.println("**************************");
Iterator it = al.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
运行结果:
10
10
18
0
0
0
0
0
0
0
0
**************************
10
18
0
0
0
0
0
0
0
0
beans类:
package zz.itheima.comparator;
public class Book implements Comparable<Book>{
private String name;
private int price;
public Book(String name, int price) {
super();
this.name = name;
this.price = price;
}
public Book() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + "]";
}
@Override
public int compareTo(Book o) {
int res = this.getName().compareTo(o.getName());
if (res==0) {
return this.getPrice()-o.getPrice();
}else {
return res;
}
}
}
package zz.itheima.comparator;
public class Stu implements Comparable<Stu> {
private String name;
private int age;
public Stu(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Stu() {
super();
}
@Override
public String toString() {
return "Stu [name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Stu o) {
int res=this.age-o.getAge();
if(res==0)
{
return this.name.compareTo(o.getName());
}
else {
return res;
}
}
}
TestStu类:
package zz.itheima.comparator;
import java.util.TreeSet;
public class TestStu {
public static void main(String[] args) {
TreeSet<Stu> hs = new TreeSet<Stu>();
hs.add(new Stu("张一", 80));
hs.add(new Stu("张三", 10));
hs.add(new Stu("李四", 16));
System.out.println(hs);
}
}
运行结果:
[Stu [name=张三, age=10], Stu [name=李四, age=16], Stu [name=张一, age=80]]
TestSort类:
package zz.itheima.comparator;
import java.util.Arrays;
public class TestSort {
public static void main(String[] args) {
String[] str = {"bcd","aca","abc"};
Arrays.sort(str);
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
System.out.println("**********************");
Book[] book = new Book[3];
book[0] = new Book("b", 2);
book[1]=new Book("c", 3);
book[2]=new Book("a", 1);
Arrays.sort(book);
for (int i = 0; i < book.length; i++) {
System.out.println(book[i]);
}
}
}
运行结果:
abc
aca
bcd
**********************
Book [name=a, price=1]
Book [name=b, price=2]
Book [name=c, price=3]
泛型:
package zz.itheima.comparator;
import java.util.ArrayList;
public class GenericType<T>{
private T o;
public void setO(T o){
this.o = o;
}
public T geto(){
return o;
}
GenericType<Book> fx = new GenericType<Book>();
ArrayList<Book> al = new ArrayList<Book>();
}
BookComparator1类:
package zz.itheima.comparator;
import java.util.Comparator;
public class BookComparator1 implements Comparator<Book> {
@Override
public int compare(Book o1, Book o2) {
int res = o1.getName().compareTo(o2.getName());
if (res==0) {
return o1.getPrice() - o2.getPrice();
}else {
return res;
}
}
}
BookComparator2类:
package zz.itheima.comparator;
import java.util.Comparator;
public class BookComparator2 implements Comparator<Book>{
@Override
public int compare(Book o1, Book o2) {
int res = o1.getPrice() - o2.getPrice();
if (res==0) {
return o1.getName().compareTo(o2.getName());
}else {
return res;
}
}
}
TestComparator类:
package zz.itheima.comparator;
import java.util.TreeSet;
public class TestComparator {
public static void main(String[] args) {
TreeSet<Book> ts=new TreeSet<Book>(new BookComparator2());
ts.add(new Book("abc", 10));
ts.add(new Book("aa", 1));
ts.add(new Book("aa", 10));
System.out.println(ts);
}
}
运行结果:
[Book [name=aa, price=1], Book [name=aa, price=10], Book [name=abc, price=10]]
TestBinarySerach类:
package zz.itheima.comparator;
import java.util.ArrayList;
import java.util.Collections;
public class TestBinarySerach {
public static void main(String[] args) {
ArrayList<Book> al2=new ArrayList<Book>();
al2.add(new Book("accc", 21));
al2.add(new Book("aaa", 10));
al2.add(new Book("abc", 11));
al2.add(new Book("ddd", 12));
al2.add(new Book("eee", 13));
Collections.sort(al2, new BookComparator2());
System.out.println(al2);
System.out.println("*********************");
int res = Collections.binarySearch(al2,new Book("accc", 21), new BookComparator2());
System.out.println(res);
}
}
运行结果:
[Book [name=aaa, price=10], Book [name=abc, price=11], Book [name=ddd, price=12], Book [name=eee, price=13], Book [name=accc, price=21]]
*********************
4
TestCollections类:
package zz.itheima.comparator;
import java.util.ArrayList;
import java.util.Collections;
public class TestCollections {
public static void main(String[] args) {
ArrayList<String> al1 = new ArrayList();
al1.add("aaa");al1.add("bbb");al1.add("ccc");
String key = "bbdfg";
int res1 = Collections.binarySearch(al1, key);
System.out.println(res1);
System.out.println("******************");
ArrayList<Book> al2=new ArrayList<Book>();
al2.add(new Book("accc", 12));
al2.add(new Book("aaa", 10));
al2.add(new Book("bbb", 11));
al2.add(new Book("ddd", 11));
al2.add(new Book("eee", 11));
System.out.println(al2);
Collections.sort(al2,new BookComparator2());
int res2=Collections.binarySearch(al2, new Book("accc", 12),new BookComparator2());
System.out.println(res2);
System.out.println("**************************");
ArrayList<String> al3=new ArrayList<String>();
al3.add("aaa");
al3.add("abc");
al3.add("cdf");
al3.add("add");
System.out.println(Collections.max(al3));
System.out.println("*********************");
ArrayList<Book> al4=new ArrayList<Book>();
al4.add(new Book("ccc", 10));
al4.add(new Book("aaa", 12));
al4.add(new Book("bbb", 11));
System.out.println(Collections.max(al4, new BookComparator2()));
System.out.println("*******************");
ArrayList<String> al5=new ArrayList<String>();
al5.add("aaa");
al5.add("abc");
al5.add("cdf");
al5.add("abc");
al5.add("add");
al5.add("abc");
System.out.println(al5);
Collections.reverse(al5);
System.out.println(al5);
Collections.sort(al5);
System.out.println(al5);
Collections.sort(al4, new BookComparator2());
System.out.println(al4);
Collections.shuffle(al5);
System.out.println(al5);
}
}
运行结果:
-3
******************
[Book [name=accc, price=12], Book [name=aaa, price=10], Book [name=bbb, price=11], Book [name=ddd, price=11], Book [name=eee, price=11]]
4
**************************
cdf
*********************
Book [name=aaa, price=12]
*******************
[aaa, abc, cdf, abc, add, abc]
[abc, add, abc, cdf, abc, aaa]
[aaa, abc, abc, abc, add, cdf]
[Book [name=ccc, price=10], Book [name=bbb, price=11], Book [name=aaa, price=12]]
[cdf, abc, aaa, abc, add, abc]