ArrayList和LinkedList用代码实现底层方法

Arrayllst和linkedList的使用方法
一,Arrayllst
(1)ArrayList简介
ArrayList是一个其容量能够动态增长的动态数组。它继承了AbstractList,实现了List、RandomAccess, Cloneable, java.io.Serializable。
基本的ArrayList,长于随机访问元素,但是在List中间插入和移除元素时较慢。同时,ArrayList的操作不是线程安全的!一般在单线程中才使用ArrayList,而在多线程中一般使用Vector或者CopyOnWriteArrayList。

(2)ArrayList 常见方法及代码实现

public class MyArrayList {
    public int [] elem;//定义一个数组
    public int usedSize;//数组的有效数据个数
    public static final int capacity = 10;//初始容量

    public MyArrayList() {
        this.elem = new int[capacity];
        this.usedSize = 0;
    }


    //public int size() { return this.usedSize; }


    //判断是否是满了
    private boolean isFull() {
        if(this.usedSize == this.elem.length) {
            return true;
        }
        return false;
        //return this.usedSize == this.elem.length;
    }
    //在pos位置新增个数
    public void add(int pos,int data) {
        //如果满了进行扩容
        if(isFull()) {
            this.elem =
                   Arrays.copyOf(this.elem,2*this.elem.length);

        }

        //判断pos合法性
        check(pos);
        //if(pos < 0 || pos > this.usedSize) {
         //   return ;
       // }
        for(int i = this.usedSize-1 ;i >= pos;i--) {
            this.elem[i+1] = this.elem[i];

        }
        this.elem[pos] = data;
        this.usedSize++;
    }


    public void display() {
        for(int i = 0;i < this.usedSize;i++) {
            System.out.print(this.elem[i] + " ");

        }
        System.out.println();
        //System.out.println(Arrays.toString(this.elem));
    }

    //查看是否包含某个元素
    public boolean contains (int toFind) {
        for(int i = 0;i < this.usedSize;i++) {
            if(this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素
    public int search(int toFind) {
        for(int i = 0;i < this.usedSize;i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }
    //判断pos是否合法
    private void check(int pos) {
        if(pos < 0 || pos > this.usedSize) {
            throw new RuntimeException("pos位置不合法");
        }
}

    //判断是否为空
    private boolean isEmpty() {
        return this.usedSize == 0;
    }
    //获取某个元素
    public int getPos(int pos) {
        //判断是否合法
    if(isEmpty()) {
        //return -1;
        throw new RuntimeException("顺序表为空");//手动抛出异常
    }
        if(pos < 0 || pos >=this.usedSize) {
           // System.out.println("pos位置不合法");
            throw new RuntimeException("pos位置不合法");
        }
        return this.elem[pos];
    }

    public void remove(int key) {
        int n = search(key);
        if(n == -1) {
            System.out.println("没有你找的数字!");
        }
        for(int i = n;i < this.usedSize;i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

    //获取元素个数
    public int getSize() {
        return this.usedSize;
    }


    public void clear() {
        this.usedSize = 0;
    }

}

二,LinkedList
(1)LinkedList简介
linkedlist是一个继承与AbstractSequentialList的双向链表,它也可以被当做堆栈队列或双端队列进行操作,LinkedList实现list接口,能进行队列操作,LinkedList实现Deque接口,即能将LinkedList当做双端队列使用,

(2)LinkedList的常见方法及代码实现

class Node {
    public int data;
    public Node next;
    public Node(int data) {
        this.data = data;
        this.next  = null;
    }
}

public class MyLinkedList {


    public Node head;//保存单链表的头

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
            return;
        }
        node.next = this.head;  //把当前头节点的地址给要插入的节点的next里面
        this.head = node;//把当前节点的头给node
    }

    public void display() {
        Node cur = this.head;

        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;//next里面放的是下一个节点的地址,走完一个之后 ,找到next地址里面对应的node赋值为新的cur
        }
    }

    //尾插法
    public void addList(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
            return;
        }
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;//指向下一个节点
        }
        cur.next = node;
    }

    //是否包含某个节点
    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //元素个数
    public int listSize() {
        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //任意位置加一个数
    public void addIndex(int index, int data) {
        if (index == 0) {
            addList(data);
        }
        if (index == listSize()) {
            addList(data);
        }
        Node cur = search(index);
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;


    }

    //查找某个数
    public Node search(int index) {
        if (index < 0 || index > this.listSize()) {
            //System.out.println("没有这个位置");
            throw new RuntimeException("index位置不合法");
        }
        Node cur = this.head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        return cur;
    }

    //除去某个数
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.data == key) {
            this.head = this.head.next;
            return;
        }
        Node prve = searchPrve(key);
        if (prve == null) {
            System.out.println("没有你要删除的值");
            return;
        }
        Node del = prve.next;
        prve.next = del.next;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值