编程语言
首页 > 编程语言> > Java编程思想第四版(Holding Your Objects )----第11章自由练习(个人练习)

Java编程思想第四版(Holding Your Objects )----第11章自由练习(个人练习)

作者:互联网

自由练习、书中例子实例

package out.file;

/**
 * @program: ChapterEleven
 * @description:
 * @author: Tenco_vivid
 * @create: 2019-07-24 15:09
 **/
import com.elevenexercise30.demo.pets.*;
import out.file.pets.Person;
import net.mindview.util.Null;
import org.w3c.dom.ls.LSInput;

import javax.naming.NamingEnumeration;
import java.sql.Array;
import java.util.*;
import static net.mindview.util.Print.*;




class A{
    private static long count ;
    //哦,对了static只能初始化一次
    private final long id = count++;
    public long getId(){
        return id;
    }
}
class B extends A{}
class C extends A{}
class D extends A{}
class F extends A{}


public class ex1 {
    //--------------------为什么啊,为什么???????一定要这个位置-----------------------
    static Collection fill(Collection<String> co3) {
        co3.add("a");
        co3.add("b");
        co3.add("c");
        return co3;
    }
    static Map fill(Map<String,String> ma1) {
        ma1.put("a","A");
        ma1.put("b","B");
        ma1.put("c","C");
        return ma1;
    }
//------------------------------------------------
public static void display(Iterator<Pet> it){
    while (it.hasNext()){
        Pet pt = it.next();
        print(pt.id()+" "+pt);
    }
}
//------------------------------------------------
public static Map<Person,List<? extends Pet>> mp = new HashMap<Person,List<? extends Pet>>();
    static {
        mp.put(new Person("Dawn"),Arrays.asList(new Cymric(),new Mutt()));
    }
//------------------------------------------------
public static void printQ(Queue qu1){
    while (qu1.peek()!=null){
        print(qu1.remove());
    }
}




    //@SuppressWarnings("unchecked")
    public static void main(String[] args) {
        ArrayList<A> al1 = new ArrayList<>();
        for (int i=0; i<3; i++){
            al1.add(new A());
        }
        for (int i=0; i<al1.size(); i++){
            print(al1.get(i).getId());
        }
        for (A xa:al1) {
            print(xa.getId());
        }



        print("----------------------------------------------------------");
        ArrayList<A> al2 = new ArrayList<>();
            al2.add(new B());
            al2.add(new C());
            al2.add(new D());
            al2.add(new F());
        for (A xa: al2) {
            print(xa.getId());
        }
        print(al2);



        print("----------------------------------------------------------");
        Collection<Integer> co1 = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            co1.add(i);
        }
        for (Integer xin:co1) {
            print(xin);
        }



        print("----------------------------------------------------------");
        Collection<Integer> co2 = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
        Integer[] xin = {6,7,8,9,10};
        co2.addAll(Arrays.asList(xin));
        print(co2+"one.");
        Collections.addAll(co2,11,12,13,14,15);
        print(co2+"two.");
        Collections.addAll(co2,xin);
        print(co2+"three.");

        print("----------------------------------------------------------");
        class A1{}
        class B1 extends A1{}
        class C1 extends A1{}
        class D1 extends A1{}
        class E1 extends B1{}
        class F1 extends B1{}
        List<A1> list1 = new ArrayList<A1>(Arrays.asList(new B1(), new C1(), new D1()));
        print(list1);
        List<A1> list2 = new ArrayList<A1>();
        Collections.addAll(list2,new E1(),new F1());
        print(list2);
        List<A1> list3 = Arrays.<A1>asList(new E1(),new F1());
        print(list3);


        print("----------------------------------------------------------");
        print(fill(new ArrayList<>()));
        print(fill(new LinkedList<>()));
        print(fill(new HashSet<>()));
        print(fill(new TreeSet<>()));
        print(fill(new LinkedHashSet<>()));
        print(fill(new HashMap<>()));
        print(fill(new TreeMap<>()));
        print(fill(new LinkedHashMap<>()));


        print("----------------------------------------------------------");
        Random ra1 = new Random(19);
        List<Pet> pets = Pets.arrayList(3);
        print(pets);
        Hamster h = new Hamster();
        pets.add(h);
        print(pets);
        print(pets.contains(h));
        pets.remove(h);
        print(pets);
        //
        Pet pet1 = pets.get(1);
        print(pets.indexOf(pet1));
        Pet cy1 = new Cymric();
        print(pets.indexOf(cy1));
        pets.remove(cy1);
        pets.remove(pet1);
        print(pets);
        //
        pets.add(1,new Manx());
        print(pets);
        List<Pet> pet2 = pets.subList(0,2);
        print(pet2);
        print(pets.containsAll(pet2));
        Collections.sort(pet2);
        print(pet2);
        //
        print("go on !");
        print(pets.containsAll(pet2));
        Collections.shuffle(pet2);
        print(pet2);
        print(pets.containsAll(pet2));
        //
        List<Pet> pet3 = new ArrayList<Pet>(pets);
        pet2 = Arrays.asList(pets.get(1),pets.get(2));
        print(pet3);
        print(pet3.containsAll(pet2));
        print(pet2);
        //
        print("go on 2!");
        pet3 = new ArrayList<Pet>(pets);
        print(pet3);
        pet3.removeAll(pet2);
        print(pet3);
        pet3.add(1,new Mouse());
        print(pet3);
        pet3.addAll(pet2);
        print(pet3);
        //
        print(pets.isEmpty());
        pets.clear();
        print(pets.isEmpty());
        pets.addAll(Pets.arrayList(4));
        print(pets);
        Object ob = pets.toArray();
        print(ob);
        Pet[] pet4 = pets.toArray(new Pet[0]);
        print(pet4);
        print(pet4[0].id());


        print("----------------------------Iterator-1-----------------------------");
        List<Pet> pets2 = Pets.arrayList(6);
        Iterator<Pet> it = pets2.iterator();
        //
        while (it.hasNext()){
            print(it.next());
        }
        //
        for (Pet pt:pets2) {
            print(pt);
        }
        //?????????why????what????为什么一定要这样赋值,是因为返回指定开头是么
        it = pets2.iterator();
        for (int i = 0; i < pets2.size(); i++) {
            it.next();
            //?"????
            it.remove();
        }
        print(pets2);


        print("----------------------------------------------------------");
        ArrayList<Pet> al3 = Pets.arrayList(5);
        LinkedList<Pet> ll3 = new LinkedList<>(al3);
        HashSet<Pet> hs3 = new HashSet<>(al3);
        TreeSet<Pet> tp3 = new TreeSet<>(al3);
        display(al3.iterator());
        display(ll3.iterator());
        display(hs3.iterator());
        display(tp3.iterator());


        print("----------------------------------------------------------");
        List<Pet> list = Pets.arrayList(5);
        ListIterator<Pet> it2 = list.listIterator();
        while (it2.hasNext()){
            print(it2.next()+" "+it2.nextIndex());
        }
        //
        while (it2.hasPrevious()){
            print(it2.previous().id());
        }
        it2 = list.listIterator();
        while (it2.hasNext()){
            it2.next();
            it2.set(Pets.randomPet());
        }
        print(list);



        print("----------------------------------------------------------");
        LinkedList<Pet> li2 = new LinkedList<Pet>(Pets.arrayList(5));
        print(li2);
        print(li2.getFirst());
        print(li2.element());
        print(li2.peek());
        print(li2.getFirst());
        //
        print(li2.remove());
        print(li2.removeFirst());
        print(li2.poll());
        print(li2);
        //
        li2.addFirst(new Rat());
        print(li2);
        li2.offer(Pets.randomPet());
        print(li2);
        li2.add(new Hamster());
        print(li2);
        print(li2.removeLast());



        print("-----------------------------南风-----------------------------");
        Stack<String> sk1 = new Stack<>();
        for (String str:"xu li ya.".split(" ")) {
            sk1.push(str);
            if (sk1.isEmpty()){
                print(sk1.pop());
            }
        }



        print("----------------------------------------------------------");
        Random ra2 = new Random(19);
        SortedSet<Integer> ss1 = new TreeSet<>();
        for (int i = 0; i < 100; i++) {
            ss1.add(ra2.nextInt(10));
        }
        print(ss1);



        print("----------------------------------------------------------");
        Set<String> ss2 = new HashSet<>();
        Collections.addAll(ss2,"a,b,c,d,e".split(" "));
        print(ss2);
        ss2.add("f");
        print(ss2);
        print(ss2.contains("f"));
        print(ss2.contains("g"));
        //
        Set<String> ss3 = new HashSet<>();
        Collections.addAll(ss3,"a,b,c".split(" "));
        print(ss2.containsAll(ss3));
        ss2.removeAll(ss3);
        print(ss2);
        ss3.removeAll(ss3);
        print(ss3);
        Set<String> ss4 = new HashSet<>();
        Set<String> ss5 = new HashSet<>();
        ss4.add("a");
        ss4.add("b");
        ss4.add("c");
        ss5.add("a");
        ss5.add("c");
        ss5.add("b");
        ss5.add("d");
        ss5.add("e");
        print(ss5.containsAll(ss4));


        print("----------------------------------------------------------");
        Map<Integer,Integer> mp1 = new HashMap<>();
        Random ra3 = new Random(19);
        for (int i = 0; i <1000 ; i++) {
            int k = ra3.nextInt(10);
            //靠,一定得是Integer
            Integer v = mp1.get(k);
            mp1.put(k,v == null ? 1 : (v+1));
        }
        print(mp1);




        print("----------------------------------------------------------");
        print(mp.keySet());
        print(mp.values());
        for (Person pe:mp.keySet()) {
            print(pe);
            for (Pet pe1:mp.get(pe)) {
                print(pe1);
            }
        }



        print("----------------------------------------------------------");
        Queue<Integer> qu2 = new LinkedList<>();
        Random ra4 = new Random(19);
        for (int i = 0; i < 100; i++) {
            qu2.offer(ra4.nextInt(i+10));
        }
        print(qu2);
        Queue<Character> qu3 = new LinkedList<>();
        for (Character ch:"nishigedabendanme".toCharArray()) {
            qu3.offer(ch);
            printQ(qu3);
        }



        print("----------------------------------------------------------");
        print("----------------------------------------------------------");
        print("----------------------------------------------------------");
        print("----------------------------------------------------------");
        print("----------------------------------------------------------");
        print("----------------------------------------------------------");

    }
}

 

标签:11,Java,li2,练习,add,pets,pet2,print,new
来源: https://blog.csdn.net/weixin_43378197/article/details/97424101