java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

//如果<a href="http://www.cfei.net/archives/tag/%e9%93%be%e8%a1%a8" title="浏览关于“链表”的文章" target="_blank" class="tag_link">链表</a>的的数据类型是对象,则关于比较的方法全部改成compare()
//class Book{         
//    private String title;
//    private double price;
//    public Book(String title,double price){
//        this.title = title;
//        this.price = price;
//    }
//    public String toString(){
//        return &quot;图书名称: &quot; + this.title + &quot;,价格: &quot; + this.price;
//    }
//    public boolean compare(Book book){
//        if(this == book)
//            return true;
//        if(book == null)
//            return false;
//        if(this.title.equals(book.title)&amp;&amp;this.price == book.price)
//            return true;
//        return false;
//    }
//}

//定义实现Link类
class Link{
//以下为内部类Node
    //定义内部类Node为Link类服务
    private class Node{
        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(this.data.equals(data))
                return true;
            else{
                if(this.next != null)           //该节点后续不为空 可继续查询
                    return this.next.containsNode(data);
                else{                            //没有节点 没得查了
                    return false;
                }
            }
        }
        public Object getNode(int index){
            if(Link.this.foot++ == index)
                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{
                this.next.setNode(index,data);
            }
        }
        //要传递上一个节点以及数据
        public void removeNode(Node previous,Object data){
            if(this.data.equals(data)){       //当前结点就是要删除的节点
                previous.next = this.next;    //空出当前结点
            }
            else{                     //继续向后查询
                this.next.removeNode(this,data);
            }
        }
        public void toArrayNode(){
            Link.this.retArray[Link.this.foot++] = this.data;
            if(this.next != null)
                this.next.toArrayNode();
        }
        public void printNode(){
            if(this != null){
                System.out.println(this.data);
                if(this.next != null)
                    this.next.printNode();
                else
                    return ;
            }
            else
                return;
        }
    }
    //===============以上为内部类==================
    private Node root;                      //根节点
    private int count = 0;                  //保存元素的个数
    private int foot = 0;                   //元素脚标
    private Object[] retArray;
    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++;                       //每次保存后数据量+1
    }  
    public int size(){                      //返回数组长度
        return this.count;
    }
    public boolean isEmpty(){               //链表判空
        return this.count == 0;
    }
    public Object get(int index){          //按照下标查询
        if(index &gt;this.count)
            return null;                   //无数据返回
        this.foot = 0 ;                    //表示从前向后取
        return this.root.getNode(index);
    }
    //第一次调用为root
    //第二次调用为root.next
    public boolean contains(Object data){
        //没有要查询的数据,根节点也没有数据
        if(data == null || this.root == null){
            return false;
        }
        return this.root.containsNode(data);
    }
    public void set(int index,Object data){
        if(index&gt;this.count)
            return;
        this.foot = 0;
        this.root.setNode(index,data);
    }
    public void remove(Object data){
        if(this.contains(data)){         //判断是否存在
            //要删除的节点是否是根节点
            if(this.root.data.equals(data)){
                this.root = this.root.next;
            }  
            else{
                this.root.next.removeNode(this.root,data);
            }
            this.count--;
        }
    }
    public Object[] toArray(){
        if(this.root == null)
            return null;
        this.foot = 0;
        this.retArray = new Object [this.count];
        this.root.toArrayNode();
        return this.retArray;
    }
    public void printList(){          //输出链表所有的值
        this.root.printNode();
    }
}
//实现宠物商店
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 remove(Pet pet){    //下架
        this.pets.remove(pet);
    }
    public Link search(String keyWord){    //实现模糊查询 因为不知道结果有什么 返回一个链表
        Link result = new Link();    //保存结果
        //将集合变为对象数组返回
        Object obj [] = this.pets.toArray();
        for(int i = 0; i &lt; obj.length; i++){
            Pet p = (Pet) obj[i];
            if(p.getName().contains(keyWord)){         //使用contains实现   
                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 String getName(){
        return this.name;
    }
    public int getAge(){
        return this.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) &amp;&amp; this.age == c.age)
            return true;
        return false;
    }
    public String toString(){
        return &quot;猫的名字: &quot;+this.name + &quot; 猫的年龄: &quot; + this.age + &quot; 岁&quot;;
    }
}
class Dog implements Pet{
    private String name;
    private int age;
    public Dog(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public boolean equals(Object obj){
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if(!(obj instanceof Fish))
            return false;
        Dog c = (Dog) obj;
        if(this.name.equals(c.name) &amp;&amp; this.age == c.age)
            return true;
        return false;
    }
    public String toString(){
        return &quot;狗的名字: &quot;+this.name + &quot; 狗的年龄: &quot; + this.age + &quot; 岁&quot;;
    }
}
class Fish implements Pet{
    private String name;
    private int age;
    public Fish(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public boolean equals(Object obj){
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if(!(obj instanceof Fish))
            return false;
        Fish c = (Fish) obj;
        if(this.name.equals(c.name) &amp;&amp; this.age == c.age)
            return true;
        return false;
    }
    public String toString(){
        return &quot;鱼的名字: &quot;+this.name + &quot; 鱼的年龄: &quot; + this.age + &quot; 岁&quot;;
    }
}
public class LinkedList{
    public static void main(String args[]){
        PetShop shop = new PetShop();
        shop.add(new Cat(&quot;A猫&quot;,10));
        shop.add(new Cat(&quot;B猫&quot;,10));
        shop.add(new Dog(&quot;A狗&quot;,10));
        shop.add(new Dog(&quot;B狗&quot;,10));
        shop.add(new Fish(&quot;A鱼&quot;,10));
        shop.add(new Fish(&quot;B鱼&quot;,10));
        Link all = shop.search(&quot;狗&quot;);
        Object [] list = all.toArray();
        for(Object tmp : list)
            System.out.println(tmp);
    }

}

链表接口

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。