每天练习的java基础题目

每天练习的java基础题目

1、 链表操作:
增加数据
取得链表长度
判断空链表
内容查询
根据索引取得数据
修改链表数据
删除链表数据
对象数组转换

2、 工厂设计模式 | 代理设计模式

3、 equals方法

代码:
Link.java

// 链表操作
public class Link {
    private class Node {
        private Object data;
        private Node next;
        public Node(Object data) {
            this.data = data;
        }
        // addNode
        public void addNode(Node newNode) {
            if (this.next == null)
                this.next = newNode;
            else 
                this.next.addNode(newNode);         

        }

        // containsNode
        public boolean containsNode(Object data) {
            if (data.equals(this.data)) 
                return true;
            if (this.next != null)
                return this.next.containsNode(data);
            return false;
        }

        // getNode
        public Object getNode(int index) {
            if (Link.this.foot ++ == index)
                return this.data;
            return this.next.getNode(index);
        }

        // setNode
        public void setNode(int index, Object data) {
            if (Link.this.foot ++ == index)
                this.data = data;
            else
                this.next.setNode(index, data);
        }

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

        // toArrayNode
        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 
            this.root.addNode(newNode);
        this.count ++;
    }

    // 取得链表长度
    public int size() {
        return this.count;
    }

    // 判断空链表
    public boolean isEmpty() {
        return this.count == 0;
    }

    // 内容查询
    public boolean contains(Object data) {
        if (this.root == null || data == null)
            return false;

        return this.root.containsNode(data);

    }

    // 根据索引取得数据
    public Object get(int index) {
        if (this.root == null || index > this.count - 1 || index < 0)
            return null;
        this.foot = 0;
        return this.root.getNode(index);
    }

    // 修改链表数据
    public void set(int index, Object data) {
        if (this.root == null || index > this.count - 1 || index < 0)
            return;
        this.foot = 0;
        this.root.setNode(index, data);
    }

    // 删除链表数据
    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 --;
        }
    }

    // 对象数组转换
    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 static void main(String[] args) {
        Link link = new Link();
        link.add("111");
        link.add("222");
        link.add("333");

        //System.out.println("size = " + link.size());
        //System.out.println("isEmpty = " + link.isEmpty());
        //System.out.println("contains = " + link.contains("111"));
        //System.out.println("contains = " + link.contains("444"));
        //System.out.println("get = " + link.get(0));
        //System.out.println("get = " + link.get(4));
        //link.set(0, "fuck");
        //System.out.println("get = " + link.get(0));

        //link.remove("111");
        //System.out.println("get = " + link.get(0));

        Object[] array = link.toArray();
        for (int x = 0 ; x < array.length ; x ++) {
            if (array[x] instanceof String) 
                System.out.println("x = " + x + "; value = " + array[x]);
        }   
    }

}

Factory.java

// 工厂设计模式
public class Factory {
    public static Fruit getInstance(String className) {
        if ("apple".equals(className))
            return new Apple();
        if ("orange".equals(className))
            return new Orange();
        return null;
    }

    public static void main(String[] args) {
        Fruit fruit = Factory.getInstance("apple");
        fruit.eat();

        fruit = Factory.getInstance("orange");
        fruit.eat();
    }
}

interface Fruit {
    public void eat();
}
class Apple implements Fruit {
    public void eat() {
        System.out.println("apple ... eat");
    }
}

class Orange implements Fruit {
    public void eat() {
        System.out.println("orange ... eat");
    }
}

Proxy.java

// 代理设计模式
public class Proxy implements Subject {
    private Subject subject;
    public Proxy(Subject subject) {
        this.subject = subject;
    }
    private void prepare() {
        System.out.println("Proxy ... prepare");
    }
    public void make() {
        this.prepare();
        this.subject.make();
        this.destroy();
    }
    private void destroy() {
        System.out.println("Proxy ... destroy");
    }
    public static void main(String[] args) {
        Subject subject = new Proxy(new RealSubject());
        subject.make();
    }
}

interface Subject {
    public void make();
}

class RealSubject implements Subject {
    public void make() {
        System.out.println("RealSubject ... make");
    }
}

Student.java 的equals

// equals方法
public class Student {
    private String name;
    private int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object object) {
        if (this == object)
            return true;
        if (object == null)
            return false;
        if (!(object instanceof Student))
            return false;
        Student s = (Student) object;
        if (this.name.equals(s.name) && this.age == s.age)
            return true;
        return false;
    }

    public static void main(String[] args) {
        Student s1 = new Student("zhangsan", 10);
        Student s2 = new Student("lisi", 10);
        System.out.println(s1.equals(s2));
    }
}

猜你喜欢

转载自blog.csdn.net/miracle_8/article/details/80943383