ArrayList(线性表)Java实现

ArrayList(线性表)

定义:
线性表是 n(n>0) 个相同类型数据元素构成的有限序列,其中n为线性表的长度。

在这里插入图片描述

只讨论单位操作,因为范围操作不过是多次单位操作而已,对于数据结构而言知道最基本的操作即可实现稍复杂的操作

除此之外有些函数方法返回的可能是元素,也可能是bool值,而把所需值传给引用参数

线性表应有的基本操作:

FunctionDescribeO
ArrayList()建立一个新的空表1
ArrayList(Array)用数组元素依次建立线性表n
getItem(idx)返回索引为idx的元素,idx为-1时返回最后一个1
getIdx(item)返回遇到的第一个与item相同的元素索引,无返回-1n
removeItem(idx)删除索引为idx的元素n
size()返回线性表的元素数量1
contains(item)判断元素item是否在线性表中n
empty()判空1
insert(idx, item)在索引idx处,插入新元素itemn
push(item)尾插item元素1
clear()清空线性表1
set(idx, item)将索引idx处元素更改为item,并返回原元素1
expand()扩容n
package ArrayListPackage;

import java.util.Collection;
import java.util.Objects;

/**
 * 在java中,我们同样倾向于使用面向对象
 * 以及泛型编程来真正实现ArrayList
 *
 * 应该实现的接口如下 :有些接口比较简单,不必实现,进行了一定的简化,
 * 可能会改了些名称,但大体功能都已经实现了,大家愿意自己实现剩余的几个接口的话
 * 可以自己进行拓展
 *
 * | `Function`        | `Describe`                                     | `O`  |
 * | ----------------- | ---------------------------------------------- | ---- |
 * | ArrayList()       | 建立一个新的空表                               | 1    |
 * | ArrayList(Array)  | 用数组元素依次建立线性表                       | n    |
 * | getItem(idx)      | 返回索引为idx的元素,idx为-1时返回最后一个     | 1    |
 * | getIdx(item)      | 返回遇到的第一个与item相同的元素索引,无返回-1 | n    |
 * | removeItem(idx)   | 删除索引为idx的元素                            | n    |
 * | size()            | 返回线性表的元素数量                           | 1    |
 * | contains(item)    | 判断元素item是否在线性表中                     | n    |
 * | empty()           | 判空                                           | 1    |
 * | insert(idx, item) | 在索引idx处,插入新元素item                    | n    |
 * | push(item)        | 尾插item元素                                   | 1    |
 * | clear()           | 清空线性表                                     | 1    |
 * | set(idx, item)    | 将索引idx处元素更改为item,并返回原元素        | 1    |
 * | expand()          | 扩容                                           | n    |
 */

public class ArrayListTest {
    public static void main(String[] args) {
        System.out.println("ArrayListTest\n");
        Integer[] a = {1, 2, 3};
        Character[] cs = {'a', 'b', 'c', 'd', 'e'};
        ArrayList<Character> arrayList = new ArrayList<>(cs);
        arrayList.show();

//        arrayList.insert(0, 'x');
//        arrayList.insert(-1, 'x');
//        arrayList.insert(1, 'x');
//        arrayList.push('x');

//        System.out.println(arrayList.set(0, 'x'));

//        System.out.println(arrayList.get(0));
//        System.out.println(arrayList.get(-1));
//        System.out.println(arrayList.get(1));

//        arrayList.clear();
//        if (arrayList.contains('x')) System.out.println("存在x");
//        else System.out.println("不存在x");
//        if (arrayList.contains('a')) System.out.println("存在a");
//        else System.out.println("不存在a");

        arrayList.show();
    }
}

class ArrayList<T> {
    private Object[] container;
    private int _size;
    private int _capacity;

    public ArrayList() {
        /*
         * 无参构造,申请一块适当大的数组
         * 初始容量设为20
         * */

        this._capacity = 20;
        this.container = new Object[this._capacity];
    }

    public ArrayList(
            Collection<T> c) {
        this.container = c.toArray();
        this._capacity = c.size();
        this._size = c.size();
    }


    public ArrayList(T[] arr) {
        this.container = arr.clone();
        this._size = arr.length;
        this._capacity = arr.length;
    }

    private void expand() {
        // 扩容函数,每次扩容为capacity的1.5倍
        this._capacity = (int) (this._capacity * 1.5);
        Object[] new_container = new Object[this._capacity];
        for (int i = 0; i < this._size; i++) {
            new_container[i] = this.container[i];
        }
        this.container = new_container;
        // 由于java的垃圾回收机制,不用管原数组的内存问题
    }

    // 增
    public void push(Object t) {
        /*
        * 尾插,在容量不足时扩容
        * */
        if (this._capacity == this._size) expand();
        this.container[this._size++] = t;
    }

    public boolean insert(int idx, Object t) {
        /*
        *在索引idx处插入t元素
        * 若索引不和法则返回false
        * 当索引为-1时认为是在尾部前插入
        * 容量不足时扩容
        * */

        if(idx < -1 || idx >= this._capacity) return false;
        if(idx == -1) idx = this._size - 1;
        if (this._capacity== this._size) expand();


        for (int i = this._size; i > idx; i--) {
            this.container[i] = this.container[i - 1];
        }
        this.container[idx] = t;
        this._size++;

        return true;
    }

    // 删
    public boolean remove(int idx){
        /*
        * 索引处理,不合法返回false,若为-1视为末位
        * */
        if(idx < -1 || idx >= this._capacity) return false;
        if(idx == -1) idx = this._size - 1;

        for (int i = idx; i < this._size - 1; i++) {
            this.container[i] = this.container[i + 1];
        }

        this._size -- ;
        return true;
    }

    public void clear(){
        /*
        * 把size置零,视为清空
        * */

        this._size = 0;
    }

    // 改
    public T set(int idx, T t){
        /*
        * idx不合法返回原值t,idx为-1视为末位
        * */
        if(idx < -1 || idx >= this._capacity) return t;
        if(idx == -1) idx = this._size - 1;


        Object temp;
        temp = this.container[idx];
        this.container[idx] = t;
        return (T)temp;
    }

    // 查
    public T get(int idx){
        /*
        * idx不合法不做处理,idx为-1视为末位
        * */
        if (idx == -1) idx = this._size - 1;
        return ((T) this.container[idx]);
    }

    public int size(){return this._size;}

    public boolean empty(){return this._size == 0;}

    public boolean contains(T t){
        for (Object o: this.container) {
            if (o.equals(t)) return true;
        }
        return false;
    }
    // 规则输出
    public void show() {
        System.out.print("[");
        for (int i = 0; i < this._size - 1; i++) {
            System.out.print(this.container[i] + ", ");
        }
        if(this._size > 0)System.out.print(this.container[this._size - 1]);
        System.out.print("]\n");
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值