javaSE(11)(集合迭代器与比较器)

迭代器接口:
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 {
    //此类就当成ArrayList
    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) {//数组的默认值为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();
        // TODO Auto-generated constructor stub
    }
    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;//名字不相等话就直接返回int型数字,正数是大于,负数是小于
        }
    }

}
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();
        // TODO Auto-generated constructor stub
    }
    @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.getName().compareTo(o.getName());
// if (res==0) {//名字相等的情况下比较年龄
// return this.getAge() - o.getAge();
// }else {
// return res;
// }
        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) {
        // 回顾一下Arrays.sort()方法的作用,并思考一下如果数组存储的是自定义类型的数据,会怎么排序?
        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(有参构造)中存储几个图书对象,并输出该TreeSet进行测试 如果书名一样,价格不同,会怎样?*/


        /*java.util.Comparator接口 int compare(T o1,T o2) 自定义一个类实现该接口并重写该方法实现排序算法 TreeSet类有一个public TreeSet(Comparator<T> comp)构造方法可以指定排序算法类*/
        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) {
// public static <T> int binarySearch(List list, T key)
        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("******************");
// public static <T> int binarySearch(List list, T key, Comparator c)
        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("**************************");
// public static <T > T max(Collection<T> coll)
        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("*********************");

// public static <T> T max(Collection<T> coll, Comparator<T> comp)
        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("*******************");

// public static <T> T min(Collection<T> coll)
// public static <T> T min(Collection<T> coll, Comparator<T> comp)
// public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
        ArrayList<String> al5=new ArrayList<String>();
        al5.add("aaa");
        al5.add("abc");
        al5.add("cdf");
        al5.add("abc");
        al5.add("add");
        al5.add("abc");

        //Collections.replaceAll(al5, "abc", "hello");
        System.out.println(al5);

// public static void reverse(List<T> list)
        Collections.reverse(al5);
        System.out.println(al5);

// public static <T> void sort(List<T> list)
        Collections.sort(al5);
        System.out.println(al5);

// public static <T> void sort(List<T> list, Comparator<T> c)
        Collections.sort(al4, new BookComparator2());
        System.out.println(al4);

// public static void shuffle(List<?> list)
        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]



















你可能感兴趣的:(java,迭代器,iterator,se)