Java實現順序線性表

來源:互聯網
上載者:User

import java.util.Arrays;

public class SequenceList<T> {

    private int DEFAULT_SIZE=16;
    //儲存數組的長度
    private int capacity;
    //定義一個數組用於儲存順序線性表的元素
    private Object[] elementData;
    //儲存順序表中元素的當前個數
    private int size=0;
   
    public SequenceList(){
        capacity=DEFAULT_SIZE;
        elementData=new Object[capacity];
    }
    public SequenceList(T element){
        this();
        elementData[0]=element;
        size++;
    }
   
    public SequenceList(T element,int initSize){
        capacity=1;
        //把capacity=1設為大於initSize的最小2的n次方
        while(capacity<initSize){
            capacity<<=1;
        }
        elementData=new Object[capacity];
        elementData[0]=element;
        size++;
    }
    //擷取順序表的大小
    public int length(){
        return size;
    }
    //擷取順序表中索引為i處的元素
    public T get(int i){
        if(i<0||i>size-1){
            throw new IndexOutOfBoundsException("線性表索引越界");
        }
        return (T)elementData[i];
    }
    //尋找順序表中指定元素的索引
    public int locate(T element){
        for(int i=0;i<size;i++){
            if(elementData[i]==element){
                return i;
            }
        }
        return -1;
    }
    //向順序線性表中指定位置插入一個元素
    public void insert(T element,int index){
        if(index<0||index>size){
            throw new IndexOutOfBoundsException("線性表索引越界");
        }
        ensureCapacity(size+1);
        //將指定索引處之後的所有元素向後移動一格
        System.arraycopy(elementData, index, elementData, index+1, size-index);
        elementData[index]=element;
        size++;
    }
    //線上性順序表的最後處添加一個元素
    public void add(T element){
        insert(element,size);
    }
    private void ensureCapacity(int minCapacity) {
        // TODO Auto-generated method stub
        if(minCapacity>capacity){
            while(capacity<minCapacity){
                capacity<<=1;
            }
            elementData=Arrays.copyOf(elementData, capacity);
        }
    }
    public T delete(int index){
        if(index<0||index>size-1){
            throw new IndexOutOfBoundsException("線性表索引越界");
        }
        T oldValue=(T)elementData[index];
        int numMoved=size-index-1;
        if(numMoved>0){
            System.arraycopy(elementData, index+1, elementData, index, numMoved);
        }
        elementData[--size]=null;
        return oldValue;
    }
    public T remove(){
        return delete(size-1);
    }
    public boolean empty(){
        return size==0;
    }
    public void clear(){
        Arrays.fill(elementData, null);
        size=0;
    }
    public String toString(){
        if(size==0){
            return "[]";
        }else{
            StringBuilder sb=new StringBuilder("[");
            for(int i=0;i<size;i++){
                sb.append(elementData[i].toString()+", ");
            }
            int len=sb.length();
            return sb.delete(len-2, len).append("]").toString();
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SequenceList<String> list=new SequenceList<String>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        System.out.println(list.toString());
        list.insert("ddd", 1);
        list.add("aa");
        System.out.println(list.toString());
        list.delete(2);
        System.out.println(list.toString());
        System.out.println("ccc在順序線性表中的位置:"+list.locate("ccc"));
    }

}

執行結果:

[aaa, bbb, ccc]
[aaa, ddd, bbb, ccc, aa]
[aaa, ddd, ccc, aa]
ccc在順序線性表中的位置:2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.