Java 數組實現線性表

線性表的API:
=====================================================================
void add(Item item) 追加新元素 Item get(int idx) 獲取指定索引的元素 void set(Item item, int idx) 設置指定索引處的元素 void insert(Item item, int idx) 插入新的元素到指定索引處 void remove(int idx) 刪除指定索引的元素 int indexOf(Item item) 獲取指定元素的索引 void clear() 清空線性表 boolean isEmpty() 線性表是否爲空 int length() 線性表中元素個數
=====================================================================


1.建立ListInterface接口:
package lists;

/**
 * @author yuanweimin(aszed)
 * @email ywm00@qq.com
 * @date 2019/2/10 10:52
 */

public interface ListInterface<Item> extends Iterable<Item> {
void add(Item item); Item get(int idx); void set(Item item, int idx); void insert(Item item, int idx); void remove(int idx); int indexOf(Item item); void clear(); boolean isEmpty(); int length(); }
實現接口的方法:
package lists;

/**
 * @author yuanweimin(aszed)
 * @email ywm00@qq.com
 * @date 2019/1/6 8:54
 */

import java.util.Arrays;
import java.util.Iterator;

/**
 * 線性表的順序實現(sequence list)
 *
 * @param <Item> 元素類型
 */

public class SequenceList<Item> implements ListInterface<Item> {

    private final static int DEFAULT_SIZE = 10;     // 默認大小
    private int size = 0;                           // 實際大小
    private Item[] arr;

    public SequenceList(int cap) {
        if (cap > 0) {
            this.arr = (Item[]) new Object[cap];
        } else {
            throw new RuntimeException("參數不合法");
        }
    }

    public SequenceList() {
        this(DEFAULT_SIZE);
    }

    // 檢查索引是否合法
    private void check(int idx) {
        if (idx < 0 || idx > size)
            throw new RuntimeException("超出邊界範圍");
    }

    // 擴容
    private void grow(int minCap) {
        int oldCapacity = arr.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCap < 0)
            newCapacity = minCap;
        arr = Arrays.copyOf(arr, newCapacity);
    }

    @Override
    public void add(Item item) {
        if (arr.length <= size) grow(size + 1);
        arr[size] = item;
        size++;
    }

    @Override
    public Item get(int idx) {
        check(idx);
        return arr[idx];
    }

    @Override
    public void set(Item item, int idx) {
        check(idx);
        arr[idx] = item;
    }

    @Override
    public void insert(Item item, int idx) {
        check(idx);
        grow(size + 1);
        size++;
        for (int i = size; i > idx; i--) {
            arr[i] = arr[i - 1];
        }
        arr[idx] = item;
    }

    @Override
    public void remove(int idx) {
        check(idx);
        size--;
        for (int i = idx; i < size; i++) {
            arr[i] = arr[i + 1];
        }
    }

    @Override
    public int indexOf(Item item) {
        int loc = -1;
        for (int i = 0; i < size; i++) {
            if (item.equals(arr[i])) {
                loc = i;
                break;
            }
        }
        return loc;
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++)
            arr[i] = null;
        size = 0;
    }

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

    @Override
    public int length() {
        return size;
    }

    @Override
    public Iterator<Item> iterator() {
        return new Iterator<Item>() {

            private int current = 0;

            @Override
            public boolean hasNext() {
                return current != size;
            }

            @Override
            public Item next() {
                Item item = arr[current];
                current++;
                return item;
            }
        };
    }

    /**
     * 測試順序表
     *
     * @param args
     */
    public static void main(String[] args) {

        SequenceList<Integer> s1 = new SequenceList<>(3);
        s1.add(1);
        s1.add(2);
        s1.add(3);
        s1.add(5);
        s1.insert(9, 2);
        s1.add(3);

        s1.remove(2);

        s1.set(8, 2);

        for (int ele : s1) {
            System.out.println(ele);
        }

        System.out.println("index of 5 : " + s1.indexOf(5));

        System.out.println(s1.get(2));

        System.out.println(s1.isEmpty());

        s1.clear();
        System.out.println(s1.length());
    }
}
相關文章
相關標籤/搜索