1. 程式人生 > >MLDN 李興華老師

MLDN 李興華老師

根據視訊整理

class Link {
    class Node {
        private Node next;// 下一個節點
        private Object data;// 資料

        public Node(Object data) {
            this.data = data;
        }

        // 1.新增節點
        public void addNode(Node newNode) {
            if (this.next == null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }

        // 2.資料查詢
        public boolean containsNode(Object data) {
            if (data.equals(this.data)) {
                return true;
            } else {
                if (this.next != null) {
                    return this.next.containsNode(data);
                } else {
                    return false;
                }

            }

        }

        // 3.修改資料
        public void setNode(int index, Object data) {
            if (Link.this.foot++ == index) {
                this.data = data;
            }
            this.next.setNode(index, data);
        }

        // 4.獲取資料
        public Object getData(int index) {
            if (Link.this.foot++ == index) {
                return this.data;
            }
            return this.next.getData(index);
        }

        // 5.刪除資料
        public void removeNode(Node previous, Object data) {
            if (data.equals(this.data)) {
                previous.next = this.next;
            } else {
                this.next.removeNode(this, data);
            }
        }

        // 6.物件陣列
        public void toArrayNode() {
            Link.this.retArray[Link.this.foot++] = this.data;
            if (this.next != null) {
                this.next.toArrayNode();
            }
        }

    }

    // ===================以上是內部類=========================
    private Node root;// 根節點
    private int count;// 計數器
    private int foot;// 腳標
    private Object[] retArray;// 物件陣列
    // 1.新增資料

    public void add(Object data) {
        if (data == null) {
            return;
        }
        Node newNode = new Node(data);// 資料裝包
        if (this.root == null) {
            this.root = newNode;
        } else {
            this.root.addNode(newNode);
        }
        this.count++;
    }

    // 2.連結串列長度
    public int size() {
        return this.count;
    }

    // 3.連結串列是否為空
    public boolean isEmpty() {
        return this.count == 0;
    }

    // 4.連結串列查詢
    public boolean contains(Object data) {
        if (data == null || this.root == null) {
            return false;
        }
        return this.root.containsNode(data);
    }

    // 5.修改資料
    public void set(int index, Object data) {
        if (index > this.count) {
            return;
        }
        this.foot = 0;
        this.root.setNode(index, data);
    }

    // 6.獲取資料
    public Object get(int index) {
        if (index > this.count) {
            return null;
        }
        this.foot = 0;
        return this.root.getData(index);
    }

    // 7.刪除資料
    public void remove(Object data) {
        if (this.contains(data)) {
            if (data.equals(this.root.data)) {
                this.root = this.root.next;
            } else {
                this.root.next.removeNode(this.root, data);
            }
        }
        this.count--;
    }

    // 8.物件陣列
    public Object[] toArray() {
        if (this.root == null) {
            return null;
        }
        this.retArray = new Object[this.count];
        this.root.toArrayNode();
        return this.retArray;
    }

}

interface Pet{//定義寵物介面
    public String getName();//定義方法 取得名字資訊
    public int getAge();//定義方法 取得年齡資訊
}
class petShop {//定義寵物商店
    private Link pets = new Link();//連結串列儲存寵物資訊

    public void add(Pet pet) {
        this.pets.add(pet);//向連結串列儲存資料
    }

    public void delete(Pet pet) {
        this.pets.remove(pet);//從連結串列中刪除寵物資訊
    }

    public Link search(String keyWord) {
        Link result = new Link();//儲存結果
        //將結果合變為物件陣列的形式返回,因為集合儲存的是Objective
        //但是真正要查詢的資料在pet介面物件的getName()方法的返回值
        Object obj[] = this.pets.toArray();
        for (int x = 0; x < obj.length; x++) {
            Pet p = (Pet) obj[x];//向下轉型
            if (p.getName().contains(keyWord)) {//查詢到結果
                result.add(p);//儲存結果
            }
        }
        return result;

    }
}
class Cat implements Pet{
    private String name;
    private int age;
    public Cat(String name,int age){
        this.name = name;
        this.age = age;
    }
    public boolean equals(Object obj){
        if (this == obj){
            return true;
        }
        if (this == null){
            return false;
        }
        if (!(obj instanceof Cat)){
            return false;
        }
        Cat c = (Cat) obj;
        if(this.name.equals(c.name)&&this.age == c.age){
            return true;
        }
        return false;
    }
    public String getName(){//覆寫
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public String toString(){//覆寫Objective類中的toString
        return "貓的名字:"+this.name+"年齡:"+this.age;
    }
}
class Dog implements Pet{
    private String name;
    private int age;
    public  Dog(String name,int age){
        this.name = name;
        this.age = age;
    }
    public boolean equals(Object obj){
        if (this == obj){
            return true;
        }
        if (this == null){
            return false;
        }
        if (!(obj instanceof Dog)){
            return false;
        }
        Dog d = (Dog) obj;
        if(this.name.equals(d.name)&&this.age == d.age){
            return true;
        }
        return false;
    }
    public String getName(){//覆寫
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public String toString(){//覆寫Objective類中的toString
        return "狗的名字:"+this.name+"年齡:"+this.age;
    }

}
public class Main {
    public static void main(String[] args) {
        petShop shop = new petShop();
        shop.add(new Cat("一號貓", 1));
        shop.add(new Cat("二號貓", 2));
        shop.add(new Cat("三號貓", 3));
        shop.add(new Cat("四號貓", 4));
        shop.add(new Dog("一號狗", 1));
        shop.add(new Dog("二號狗", 1));
        shop.add(new Dog("三號狗", 1));
        shop.add(new Dog("四號狗", 1));
        Link all = shop.search("");//輸入查詢的關鍵字
        Object obj[] = all.toArray();
        for (int x = 0; x < obj.length; x++) {
            System.out.println(obj[x]);
        }
    }
}