1. 程式人生 > 其它 >java----順序表 (資料結構)

java----順序表 (資料結構)

技術標籤:Java語言資料結構java

連結串列

順序表

1. 順序表的功能

a> 增添

末尾直接新增資料

 	// a. 末尾新增
    public void add(E data) {
        if (this.capacity == this.size) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.capacity);
            this.
capacity = this.elem.length; } this.elem[this.size] = data; this.size++; }

某個特定位置新增資料

	// b. 插入到指定位置
    public void add(int index, E data) {
        // 插入位置不合法
        if (index < 0 || index > this.size) {
            System.out.println("插入異常");
            return
; } // 擴容 if (this.capacity == this.size) { this.elem = Arrays.copyOf(this.elem, 2 * this.capacity); this.capacity = this.elem.length; } // 移動元素 for (int i = this.size; i > index; i--) { this.elem[i] = this.elem[i - 1
]; } // 插入 this.elem[index] = data; this.size++; }

b> 刪除

  • 根據所給定的下標, 刪除對應元素.
	// a. 通過下標刪除順序表對應的元素
    public void remove(int index) {
        for (int i = index; i < this.size; i++) {
            this.elem[i] = this.elem[i + 1];
        }
        this.size--;
    }
  • 刪除順序表中第一次出現的 key 的關鍵字
    // b. 刪除第一次出現的 key 關鍵字
    public void remove(E key) {
        for (int i = 0; i < this.size; i++) {
            if (key == this.elem[i]) {
                this.elem[i] = this.elem[this.size - 1];
                this.size--;
                return;
            }
        }
    }
  • 刪除所有的關鍵字 key
    // c. 刪除在順序表中出現的所有 key 關鍵字
    public void removeAll(E key) {
        for (int i= 0; i < this.size; i++) {
            // 使用 while 是因為刪除關鍵字 key 時, 是將最後一個元素放至要刪除的位置
            // 將元素個數 -1, 所以需要對剛剛放至刪除位置的元素也要進行判斷.
            while (key == this.elem[i]) {
                this.elem[i] = this.elem[this.size - 1];
                this.size--;
            }
        }
    }

c> 修改

  • 修改某位置的資料
 	    // 3. 改
    public void update(int index, E date) {
        this.elem[index] = date;
    }

d> 查詢

  • 查詢給定下標對應的資料
    // a. 通過下標查詢,返回順序表中對應的值
    public E findIndex(int index) {
        return this.elem[index];
    }
  • 查詢第一個出現 key 關鍵字的位置
    // b. 順序查詢, 找到與關鍵字匹配的元素下標, 返回下標
    public int findElem(E key) {
        for (int i = 0; i < this.size; i++) {
            if (key == this.elem[i]) {
                return i;
            }
        }
        return -1;
    }
  • 判斷是否包含給定關鍵字
    // c. 查詢是否包含 key 關鍵字
    public boolean contains(E key) {
        for (int i = 0; i < this.size; i++) {
            if (key == this.elem[i]) {
                return true;
            }
        }
        return false;
    }

e>清空

  • 清空單鏈表
       // 5. 清空
    public void clear() {
        this.size = 0;
    }

f> 顯示

  • 顯示順序表中所有元素
      // 6. 顯示
    public void display() {
        for (int i = 0; i < this.size; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

g> 長度

  • 求順序表的長度
      // 7. 求長度
    public int getLength() {
        return this.size;
    }

2. 原始碼

package ArrayList1;

import java.util.Arrays;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/28 22:10
 */
public class MyArrayList<E> {
    private E[] elem;
    private int size;
    private int capacity;

    public MyArrayList(int capacity) {
        this.capacity = capacity;
        this.size = 0;
        this.elem = (E[]) new Object[this.capacity];
    }
    // 1. 增
    // a. 末尾新增
    public void add(E data) {
        if (this.capacity == this.size) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.capacity);
            this.capacity = this.elem.length;
        }
        this.elem[this.size] = data;
        this.size++;
    }
    // b. 插入到指定位置
    public void add(int index, E data) {
        // 插入位置不合法
        if (index < 0 || index > this.size) {
            System.out.println("插入異常");
            return;
        }
        // 擴容
        if (this.capacity == this.size) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.capacity);
            this.capacity = this.elem.length;
        }
        // 移動元素
        for (int i = this.size; i > index; i--) {
            this.elem[i] = this.elem[i - 1];
        }
        // 插入
        this.elem[index] = data;
        this.size++;
    }
    // 2.刪
    // a. 通過下標刪除順序表對應的元素
    public void remove(int index) {
        for (int i = index; i < this.size; i++) {
            this.elem[i] = this.elem[i + 1];
        }
        this.size--;
    }
    // b. 刪除第一次出現的 key 關鍵字
    public void remove(E key) {
        for (int i = 0; i < this.size; i++) {
            if (key == this.elem[i]) {
                this.elem[i] = this.elem[this.size - 1];
                this.size--;
                return;
            }
        }
    }
    // c. 刪除在順序表中出現的所有 key 關鍵字
    public void removeAll(E key) {
        for (int i= 0; i < this.size; i++) {
            // 使用 while 是因為刪除關鍵字 key 時, 是將最後一個元素放至要刪除的位置
            // 將元素個數 -1, 所以需要對剛剛放至刪除位置的元素也要進行判斷.
            while (key == this.elem[i]) {
                this.elem[i] = this.elem[this.size - 1];
                this.size--;
            }
        }
    }
    // 3. 改
    public void update(int index, E date) {
        this.elem[index] = date;
    }
    // 4. 查
    // a. 通過下標查詢,返回順序表中對應的值
    public E findIndex(int index) {
        return this.elem[index];
    }
    // b. 順序查詢, 找到與關鍵字匹配的元素下標, 返回下標
    public int findElem(E key) {
        for (int i = 0; i < this.size; i++) {
            if (key == this.elem[i]) {
                return i;
            }
        }
        return -1;
    }

    // c. 查詢是否包含 key 關鍵字
    public boolean contains(E key) {
        for (int i = 0; i < this.size; i++) {
            if (key == this.elem[i]) {
                return true;
            }
        }
        return false;
    }
    // 5. 清空
    public void clear() {
        this.size = 0;
    }
    // 6. 顯示
    public void display() {
        for (int i = 0; i < this.size; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
    // 7. 求長度
    public int getLength() {
        return this.size;
    }
}


3. 測試類

public class Main {
    public static void main(String[] args) {

        MyLinkedList myLinkedList = new MyLinkedList();
//        myLinkedList.addEnd(1000);
        myLinkedList.addIndex(0,100);
        myLinkedList.addFirst(2);
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(5);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(10);
        myLinkedList.partition(3);
        myLinkedList.display();
//        myLinkedList.addEnd(5);
//        myLinkedList.addEnd(6);
//        //-1, 6 是非法插入
//        myLinkedList.addIndex(-1,10);
//        myLinkedList.addIndex(6,14);
//        //頭插
//        myLinkedList.addIndex(0,11);
//        //尾插
//        myLinkedList.addIndex(6,12);
//        //中間插
//        myLinkedList.addIndex(5,13);
//        myLinkedList.remove(0);
//        //刪除頭結點
//        myLinkedList.remove(11);
//        //刪除中間節點
//        myLinkedList.remove(2);
//        //刪除尾結點
//        myLinkedList.remove(12);
//        //列印
//        myLinkedList.addIndex(4,5);
//        myLinkedList.display();
//        System.out.println(myLinkedList.contains(11));
//        System.out.println(myLinkedList.contains(100));
//        System.out.println(myLinkedList.contains(100));
//        myLinkedList.removeAllKey(5);
//        myLinkedList.addIndex(0,3);
//        myLinkedList.removeAllKey(3);
//        myLinkedList.display();
//        myLinkedList.reverseList();
//        myLinkedList.display();
        myLinkedList.remove(100);
//        System.out.println(myLinkedList.middleList());
//        myLinkedList.clear();
//        System.out.println(myLinkedList.findKthRe(2));
//        myLinkedList.display();
    }
}

在這裡插入圖片描述