zoukankan      html  css  js  c++  java
  • Java 实践

    /**
      *宠物就是一个标准,包含多类宠物
      *定义宠物标准接口Pet
      *定义Cat和Dog两个Pet接口的子类
      *使用链表结构动态存储宠物信息
      *定义商店类(工厂类),负责宠物的上架(链表添加)、下架(链表删除)、查找(模糊查找)
      *
      *所有数据类型均为 接口————Pet 
      *
      *
      */
    class Link {        //链表类(外部可调用)
    
             class Node {  //节点类,定义该内部类,只可服务于Link类
    
                       private Object data ;       //节点数据
    
                       private Node next ;           //引用关系(顺序)
    
                       public Node(Object data) {
    
                                this.data = data ;
    
                       }
    
     
    
                       public void addNode(Node newNode){
    
                                if ( this.next == null){        //下一个节点为空则设置下一个节点
    
                                         this.next = newNode ;
    
                                } else {      //若节点存在,则向后移
    
                                         this.next.addNode(newNode) ;
    
                                }
    
                       }
    
                      
    
                       public boolean containsNode(Object data) { 
    
                                //(如果比较的数据是对象,则定义一个对象比较的方法。)
    
                                if ( data.equals(this.data)) {     //对比data
    
                                         return true ;
    
                                } else {
    
                                         if ( this.next != null ) {       // 对比下一个data
    
                                                   return this.next.containsNode(data) ;
    
                                         } else {
    
                                                   return false ;
    
                                         }
    
                                }
    
                       }
    
     
    
                       public Object getNode(int index) { //索引查找
    
                                // 1 比较index和foot的值是否相等
    
                                // 2 将foot的内容自增 1
    
                                if ( Link.this.foot ++ == index) {         //对比当前节点的foot
    
                                         return this.data ;
    
                                } else {      //下一个节点
    
                                         return this.next.getNode(index) ;
    
                                }
    
                       }
    
     
    
                       public void setNode(int index , Object data) {
    
                                if ( Link.this.foot ++ == index ) {
    
                                         this.data = data ;     //内容修改(替换     )
    
                                } else {
    
                                         if ( this.next != null ) {
    
                                                   this.next.setNode(index,data) ;
    
                                         }
    
                                }
    
                       }
    
     
    
                       // 要传递上一个节点以要删除的数据
    
                       public void removeNode( Node previous , Object data ) {
    
                                if ( data.equals(this.data)){//数据满足删除条件
    
                                         previous.next = this.next ; //上一个节点的next指向当前的next(空出当前节点)
    
                                } else {      // 继续匹配查询删除
    
                                         if(this.next != null) {
    
                                                   this.next.removeNode(this , data) ;//this表示当前对象,将this给previous
    
                                                            // 因为previous接收的是上一个对象。
    
                                         }
    
                                }
    
                       }
    
     
    
                       public void toArrayNode() {
    
                                Link.this.retArray[Link.this.foot ++] = this.data ;
    
                                if ( this.next != null ) {
    
                                         this.next.toArrayNode() ;
    
                                }
    
                       }
    
                      
    
             }
    
     
    
             /* ========================================================================== */
    
     
    
             private Node root ;  //根节点
    
             private int count = 0 ; //保存元素的个数
    
             private int foot = 0 ; //保存链表的索引编号
    
             private Object [] retArray ; //对象数组
    
     
    
             public void add(Object data) { //数据添加
    
                       Node newNode = new Node(data) ;
    
                       if ( this.root == null ){//无根节点,保存根节点
    
                                this.root = newNode ;
    
                       } else {      //向Node调用
    
                                this.root.addNode(newNode) ;
    
                       }
    
                       this.count ++ ; //每一个保存后数据量自加 1
    
             }
    
     
    
             public int size() {       //判断返回链表数据数量
    
                       return this.count ;
    
             }
    
     
    
             public boolean isEmpty() {        //判断链表是否为空
    
                       return this.count == 0 ;   // 根据链表数量判断
    
             }
    
     
    
             public boolean contains(Object data) { //查询数据是否存在
    
                       if ( data == null || this.root == null ) {       //查找数据为空则false
    
                                return false ;
    
                       }
    
                       return this.root.containsNode(data) ;    //不为空则调用Node查找数据;
    
             }
    
     
    
             public Object get(int index) {   //设置索引目标 index
    
                       if ( index > this.count ) {
    
                                return null ;
    
                       }
    
                                this.foot = 0 ;   //索引归零,表示从左往右查找
    
                                return this.root.getNode(index) ;   //调用getNode()查找
    
             }
    
     
    
             public void set(int index , Object data) {//修改链表数据内容
    
                       if ( data == null || this.root == null ) {
    
                                return ;
    
                       }
    
                       this.foot = 0 ;   // 重置foot,便于查找内容
    
                       this.root.setNode(index,data) ;       //调用Node修改
    
             }
    
            
    
             /*
    
                       如果要删除的是根节点:root指向下一个节点即可
    
                       如果删除的不是根节点:删除的前一个节点,下一个节点指向删除的下一个节点
    
                       删除数据的形式:当前节点的上一个节点next = 当前节点的next
    
     
    
                       需要设置一个方法专门处理非根节点的删除(removeNode()方法)
    
             */
    
             public void remove(Object data) {
    
                       if ( this.contains(data)) {//contains() 判断数据是否存在
    
                                // 判断删除的数据是不是根节点数据
    
                                // root是Node类的对象,此处直接访问了Node对象的私有属性 data
    
                                if ( data.equals(this.root.data)) {
    
                                         this.root = this.root.next ;       //空出当前节点(改变根节点)
    
                                } else {
    
                                         this.root.next.removeNode(this.root , data) ;
    
                                }
    
                                this.count -- ;   //链表个数减一
    
                       }
    
             }
    
              
    
             //      首先开辟一个数组空间,空间 == count
    
     
    
             public Object [] toArray() { //将链表以对象数组形式返回
    
                       if (this.root == null ) {
    
                                return null ;
    
                       }
    
                       this.foot = 0 ; //下标控制
    
                       this.retArray = new Object [this.count] ; //开辟一个数组
    
                       this.root.toArrayNode() ; //Node处理
    
     
    
                       return retArray ;
             }
    }
    
    
    interface Pet { // 宠物标准接口Pet
        public String getName() ;//得到名字
        public int getAge() ;//得到年龄
    }
    
    class PetShop {  // 商店
        private Link pets = new Link() ;//保存的宠物信息 ** 利用链表存储结构来保存信息
        public void add(Pet pet) {//上架宠物信息pet        ** 调用链表add()来添加数据
            this.pets.add(pet) ;//向链表中保存数据pet
        }
        public void delete(Pet pet) { // 下架
            this.pets.remove(pet) ; //从链表中删除数据pet
        }
        public Link search(String keyWord) {
            Link result = new Link() ;
            // 将集合变为对象数组的形式返回,因为保存的是Object
            // 但是真正要查询的数据在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 {  // Cat子类 实现 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 (obj==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 toString() {
            return "Name:" + this.name + "Age:" + this.age ;
        }
    
        public String getName() {
            return this.name ;
        }
        public int getAge() {
            return this.age ;
        }
    }
    
    class Dog implements Pet {  // Dog子类 实现 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 (obj==null) {
                return false ;
            }
            if (!(obj instanceof Dog)) {
                return false ;
            }
            Dog c = (Dog) obj ;
            if ( this.name.equals(c.name) && this.age == c.age ) {
                return true ;
            }
            return false ;
        }
        public String toString() {
            return "Name:" + this.name + "Age:" + this.age ;
        }
        public String getName() {
            return this.name ;
        }
        public int getAge() {
            return this.age ;
        }
    }
    
    
    public class TestDemo {
        public static void main(String [] args) {
            PetShop shop = new PetShop() ;
            shop.add(new Cat("Ha",1));
            shop.add(new Cat("Ea",2));
            shop.add(new Cat("Lw",3));
            shop.add(new Cat("O",4));
            shop.add(new Dog("Wa",5));
            shop.add(new Dog("O",6));
            shop.add(new Dog("Rw",7));
            shop.add(new Dog("D",8));
    
            Link all = shop.search("a") ;//模糊查询
            Object obj [] = all.toArray() ;
            for ( int x = 0 ; x < obj.length ; x++ ) {
                System.out.println(obj[x]) ;
            }
        
    /**
      *宠物就是一个标准,包含多类宠物
      *定义宠物标准接口Pet
      *定义Cat和Dog两个Pet接口的子类
      *使用链表结构动态存储宠物信息(链表中存放的是对象地址)
      *定义商店类(工厂类),负责宠物的上架(链表添加)、下架(链表删除)、查找(模糊查找)
      *
      *所有数据类型均为 接口————Pet 
      *
      */
    class Link {        //链表类(外部可调用)
    
             class Node {  //节点类,定义该内部类,只可服务于Link类
    
                       private Object data ;       //节点数据
    
                       private Node next ;           //引用关系(顺序)
    
                       public Node(Object data) {
    
                                this.data = data ;
    
                       }
    
     
    
                       public void addNode(Node newNode){
    
                                if ( this.next == null){        //下一个节点为空则设置下一个节点
    
                                         this.next = newNode ;
    
                                } else {      //若节点存在,则向后移
    
                                         this.next.addNode(newNode) ;
    
                                }
    
                       }
    
                      
    
                       public boolean containsNode(Object data) { 
    
                                //(如果比较的数据是对象,则定义一个对象比较的方法。)
    
                                if ( data.equals(this.data)) {     //对比data
    
                                         return true ;
    
                                } else {
    
                                         if ( this.next != null ) {       // 对比下一个data
    
                                                   return this.next.containsNode(data) ;
    
                                         } else {
    
                                                   return false ;
    
                                         }
    
                                }
    
                       }
    
     
    
                       public Object getNode(int index) { //索引查找
    
                                // 1 比较index和foot的值是否相等
    
                                // 2 将foot的内容自增 1
    
                                if ( Link.this.foot ++ == index) {         //对比当前节点的foot
    
                                         return this.data ;
    
                                } else {      //下一个节点
    
                                         return this.next.getNode(index) ;
    
                                }
    
                       }
    
     
    
                       public void setNode(int index , Object data) {
    
                                if ( Link.this.foot ++ == index ) {
    
                                         this.data = data ;     //内容修改(替换     )
    
                                } else {
    
                                         if ( this.next != null ) {
    
                                                   this.next.setNode(index,data) ;
    
                                         }
    
                                }
    
                       }
    
     
    
                       // 要传递上一个节点以要删除的数据
    
                       public void removeNode( Node previous , Object data ) {
    
                                if ( data.equals(this.data)){//数据满足删除条件
    
                                         previous.next = this.next ; //上一个节点的next指向当前的next(空出当前节点)
    
                                } else {      // 继续匹配查询删除
    
                                         if(this.next != null) {
    
                                                   this.next.removeNode(this , data) ;//this表示当前对象,将this给previous
    
                                                            // 因为previous接收的是上一个对象。
    
                                         }
    
                                }
    
                       }
    
     
    
                       public void toArrayNode() {
    
                                Link.this.retArray[Link.this.foot ++] = this.data ;
    
                                if ( this.next != null ) {
    
                                         this.next.toArrayNode() ;
    
                                }
    
                       }
    
                      
    
             }
    
     
    
             /* ========================================================================== */
    
     
    
             private Node root ;  //根节点
    
             private int count = 0 ; //保存元素的个数
    
             private int foot = 0 ; //保存链表的索引编号
    
             private Object [] retArray ; //对象数组
    
     
    
             public void add(Object data) { //数据添加
    
                       Node newNode = new Node(data) ;
    
                       if ( this.root == null ){//无根节点,保存根节点
    
                                this.root = newNode ;
    
                       } else {      //向Node调用
    
                                this.root.addNode(newNode) ;
    
                       }
    
                       this.count ++ ; //每一个保存后数据量自加 1
    
             }
    
     
    
             public int size() {       //判断返回链表数据数量
    
                       return this.count ;
    
             }
    
     
    
             public boolean isEmpty() {        //判断链表是否为空
    
                       return this.count == 0 ;   // 根据链表数量判断
    
             }
    
     
    
             public boolean contains(Object data) { //查询数据是否存在
    
                       if ( data == null || this.root == null ) {       //查找数据为空则false
    
                                return false ;
    
                       }
    
                       return this.root.containsNode(data) ;    //不为空则调用Node查找数据;
    
             }
    
     
    
             public Object get(int index) {   //设置索引目标 index
    
                       if ( index > this.count ) {
    
                                return null ;
    
                       }
    
                                this.foot = 0 ;   //索引归零,表示从左往右查找
    
                                return this.root.getNode(index) ;   //调用getNode()查找
    
             }
    
     
    
             public void set(int index , Object data) {//修改链表数据内容
    
                       if ( data == null || this.root == null ) {
    
                                return ;
    
                       }
    
                       this.foot = 0 ;   // 重置foot,便于查找内容
    
                       this.root.setNode(index,data) ;       //调用Node修改
    
             }
    
            
    
             /*
    
                       如果要删除的是根节点:root指向下一个节点即可
    
                       如果删除的不是根节点:删除的前一个节点,下一个节点指向删除的下一个节点
    
                       删除数据的形式:当前节点的上一个节点next = 当前节点的next
    
     
    
                       需要设置一个方法专门处理非根节点的删除(removeNode()方法)
    
             */
    
             public void remove(Object data) {
    
                       if ( this.contains(data)) {//contains() 判断数据是否存在
    
                                // 判断删除的数据是不是根节点数据
    
                                // root是Node类的对象,此处直接访问了Node对象的私有属性 data
    
                                if ( data.equals(this.root.data)) {
    
                                         this.root = this.root.next ;       //空出当前节点(改变根节点)
    
                                } else {
    
                                         this.root.next.removeNode(this.root , data) ;
    
                                }
    
                                this.count -- ;   //链表个数减一
    
                       }
    
             }
    
              
    
             //      首先开辟一个数组空间,空间 == count
    
     
    
             public Object [] toArray() { //将链表以对象数组形式返回
    
                       if (this.root == null ) {
    
                                return null ;
    
                       }
    
                       this.foot = 0 ; //下标控制
    
                       this.retArray = new Object [this.count] ; //开辟一个数组
    
                       this.root.toArrayNode() ; //Node处理
    
     
    
                       return retArray ;
             }
    }
    
    interface Pet { // 宠物标准接口Pet
        public String getName() ;//得到名字
        public int getAge() ;//得到年龄
    }
    
    class PetShop {  // 商店 
        private Link pets = new Link() ;//保存的宠物信息 ** 利用链表存储结构来保存信息
        public void add(Pet pet) {//上架宠物信息pet        ** 调用链表add()来添加数据
            this.pets.add(pet) ;//向链表中保存数据pet
        }
        public void delete(Pet pet) { // 下架
            this.pets.remove(pet) ; //从链表中删除数据pet
        }
        public Link search(String keyWord) {
            Link result = new Link() ;
            // 将集合变为对象数组的形式返回,因为保存的是Object
            // 但是真正要查询的数据在Pet接口对象的getName() 方法中
            // 所以有必要向下转型
            Object obj [] = this.pets.toArray() ;
            // 核心思维:将查询条件与链表中的name做比较,若查到则将该链表的地址存入新的链表中
            for ( int x = 0 ; x < obj.length ; x ++ ) {
                Pet p = (Pet) obj[x] ; //向下转型
                if ( p.getName().contains(keyWord)) { //查询到的 ** char.contains(str)方法:如果当前char中包含str则返回true
                    result.add(p) ; // 开辟新的链表 result;并将查到的符合条件的链表地址存入链表result
                }
            }
            return result ; // 返回链表 result
        }
    }
    
    class Cat implements Pet {  // Cat子类 实现 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 (obj==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 toString() {
            return "Name:" + this.name + "Age:" + this.age + "Cat";
        }
    
        public String getName() {
            return this.name ;
        }
        public int getAge() {
            return this.age ;
        }
    }
    
    class Dog implements Pet {  // Dog子类 实现 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 (obj==null) {
                return false ;
            }
            if (!(obj instanceof Dog)) {
                return false ;
            }
            Dog c = (Dog) obj ;
            if ( this.name.equals(c.name) && this.age == c.age ) {
                return true ;
            }
            return false ;
        }
        public String toString() {
            return "Name:" + this.name + "Age:" + this.age + "Dog";
        }
        public String getName() {
            return this.name ;
        }
        public int getAge() {
            return this.age ;
        }
    }
    
    
    public class TestDemo {
        public static void main(String [] args) {
            PetShop shop = new PetShop() ;
            shop.add(new Cat("Ha",1));
            shop.add(new Cat("Ea",2));
            shop.add(new Cat("Lw",3));
            shop.add(new Cat("O",4));
            shop.add(new Dog("Wa",5));
            shop.add(new Dog("O",6));
            shop.add(new Dog("Rw",7));
            shop.add(new Dog("D",8));
    
            Link all = shop.search("a") ;//模糊查询 ** all接受shop.search()返回的链表对象
            Object obj [] = all.toArray() ; //返回对象数组 ** 链表当中存放的是子类的堆内存地址(对象地址)
            for ( int x = 0 ; x < obj.length ; x++ ) {
                System.out.println(obj[x].toString()) ; 
                // 由于Object类的toString()方法特性,只要子类中定义了toSting就会按照覆写的方法执行
            }
        }
    }
    View Code
  • 相关阅读:
    4-8 求二叉树高度 (20分)
    汉诺塔的递归和非递归实现
    5-18 银行业务队列简单模拟 (25分)
    ACM 刷题小技巧【转】
    5-21 求前缀表达式的值(25分)
    5-20 表达式转换 (25分)
    约瑟夫环----循环链表问题
    关于埃拉托色尼筛选法的整理(质数问题)
    编码---隐藏在计算机软硬件背后的语言
    内排序和外排序扫盲
  • 原文地址:https://www.cnblogs.com/wangyuyang1016/p/10800452.html
Copyright © 2011-2022 走看看