Java数据结构-顺序表

Student.java

package com.linearlist;

public class Student {
    public String id;
    public int age;

    public String getSId(){
        return this.id;
    }
    public int getAge(){
        return this.age;
    }
}

Strategy .java

package com.linearlist;

public interface Strategy {
    public boolean equal(Object obj1, Object obj2);
    /**
     * 比较两个数据元素的大小
     * 如果 obj1 < obj2 返回 -1
     * 如果 obj1 = obj2 返回 0
     * 如果 obj1 > obj2 返回 1
     */
    public int compare(Object obj1, Object obj2);
}

StudentStrategy .java

package com.linearlist;

public class StudentStrategy implements Strategy{
    public boolean equal(Object obj1, Object obj2){
        if(obj1 instanceof Student && obj2 instanceof Student){
            Student s1 = (Student)obj1;
            Student s2 = (Student)obj2;
            return s1.getSId().equals(s2.getSId());
        }
        else
            return false;
    }
    public int compare(Object obj1, Object obj2){
        if(obj1 instanceof Student && obj2 instanceof Student){
            Student s1 = (Student)obj1;
            Student s2 = (Student)obj2;
            return s1.getSId().compareTo(s2.getSId());
        }
        else
            return obj1.toString().compareTo(obj2.toString());
    }
}

List.java

package com.linearlist;

public interface List {
    public int getSize();
    public boolean isEmpty();
    public boolean contains(Object e);
    public int indexOf(Object e);
    public void insert(int i, Object e) throws OutOfBoundaryException;
    public boolean insertBefore(Object obj, Object e);
    public boolean insertAfter(Object obj, Object e);
    public Object remove(int i) throws OutOfBoundaryException;
    public boolean remove(Object e);
    public Object replace(int i, Object e) throws OutOfBoundaryException;
    public Object get(int i) throws OutOfBoundaryException;
}

ListArray .java

package com.linearlist;

public class ListArray implements List{
    private final int LEN = 8;
    private Strategy strategy;
    private int size;
    private Object[] elements;

    public ListArray(){
        // DefaultStrategy
    }
    public ListArray(Strategy strategy){
        this.strategy = strategy;
        this.size = 0;
        this.elements = new Object[LEN];
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public boolean contains(Object e){
        for(int i=0;i<size;i++)
            if(strategy.equal(e, elements[i]))
                return true;
        return false;
    }

    public int indexOf(Object e){
        for(int i=0;i<size;i++)
            if(strategy.equal(e, elements[i]))
                return i;
        return -1;

    }

    public void insert(int i,Object e) throws OutOfBoundaryException{
        if(i<0||i>size){
            throw new OutOfBoundaryException("错误, 下标越界");
        }
        if(size>=elements.length){
            expandSpace();
            // 拓展空间
        }
        for(int j=size;j>i;j--)
            elements[j] = elements[j-1];
        elements[i] = e;
        size++;
        return;
    }

    private void expandSpace(){
        Object[] a = new Object[elements.length * 2];
        for(int i=0;i<elements.length;i++)
            a[i] = elements[i];
        elements = a;
    }

    public boolean insertBefore(Object obj, Object e){
        int i = indexOf(obj);
        if(i<0) return false;
        else insert(i,e);
        return true;
    }

    public boolean insertAfter(Object obj, Object e){
        int i = indexOf(obj);
        if(i<0) return false;
        else insert(i+1,e);
        return true;
    }

    public Object remove(int i) throws OutOfBoundaryException{
        if(i<0||i>size){
            throw new OutOfBoundaryException("错误, 下标越界");
        }
        Object obj = elements[i];
        for(int j=i;j<size-1;j++)
            elements[j] = elements[j+1];
        elements[--size] = null;
        return obj;
    }

    public boolean remove(Object e){
        int i = indexOf(e);
        if (i<0)
            return false;
        remove(i);
        return true;
    }

    public Object replace(int i, Object e) throws OutOfBoundaryException{
        if (i<0||i>=size)
            throw new OutOfBoundaryException("错误,指定的序号越界。");
        Object obj = elements[i];
        elements[i] = e;
        return obj;
    }

    public Object get(int i) throws OutOfBoundaryException{
        if(i<0||i>size)
            throw new OutOfBoundaryException("错误,指定的序号越界。");
        return elements[i];
    }

}