集合基础-List

        List  是常见的数据类型。List是有序的Collection。在Java中,List一共有三个实现类,分别是ArrayList , Vector 和LinkedList。

 ArrayList的底层是由数组实现的。它允许对元素进行快速随机访问。但是我们知道,数组的缺点是每个元素之间不能有间隔。当数组大小不满足时,我们需要对其进行扩容。在其底层,我们是通过将原有数组复制到一个更大空间的储存空间中的。当从ArrayList的中间位置插入或者删除元素时,由于我们实现是通过的数组。那么,对数组进行移动,复制的代价比较高。因此使用ArrayList,适合随机查找和遍历,不适合插入和删除。

注意:ArrayList是线程不安全的。Vector的方法是加了synchronized是线程安全的。LinkedList也是线程不安全的。

        查看源码

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    private static final long serialVersionUID = 8683452581122892189L;
    private static final int DEFAULT_CAPACITY = 10;
    private static final Object[] EMPTY_ELEMENTDATA = {};
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    transient Object[] elementData; // non-private to simplify nested class access
    private int size;

    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

从上图不难看出,ArrayList在Java里的实现方式,最基础的还是通过了Object数组来实现。

那么我们也手写一份ArrayList,并且实现最基础的add,insert,remove,get等操作,

package 集合.Array.手写ArrayList;

import javax.naming.NameNotFoundException;
import java.util.Arrays;

/**
 * @program:多线程和IO
 * @descripton:ArrayList_
 * @author:ZhengCheng
 * @create:2021/10/8-14:51
 **/
public class ArrayList_ <T>{
    private int initSize = 10;
    private int nowSize ;
    private Object[] arr ;
    public ArrayList_ () {
        init();
    }

    @Override
    public String toString() {
        return "ArrayList_{" +
                "arr=" + Arrays.toString(arr) +
                '}';
    }

    private void init() {
        arr = new Object[initSize];
        nowSize = 0;
    }
    public void add(T val){
        if (nowSize == initSize/3*2){
            initSize *= 2;
            Object[] temp = new Object[initSize];
            for (int i = 0; i < nowSize; i++) {
                temp[i] = arr[i];
            }
            arr = temp;
        }
        arr[nowSize] = val;
        nowSize++;
    }
    public void add(int index,T val ){
        if (nowSize == initSize/3*2){
            initSize *= 2;
            Object[] temp = new Object[initSize];
            for (int i = 0; i < nowSize; i++) {
                temp[i] = arr[i];
            }
            arr = temp;
        }
        if (arr[index] == null){
            arr[index] = val;
        }else {
            Object[] temp = new Object[initSize];
            for (int i = 0 , j = 0; i < initSize; i++,j++) {
                if (i == index && i == j){
                    temp[i++] = val;
                }
                temp[i] = arr[j];
            }
            arr =temp;
        }
        nowSize++;
    }
    public T get(int index){
        checkIndex(index);
        return (T)arr[index];
    }

    private void checkIndex(int index) {
        if (index > nowSize){
            throw new NullPointerException("越界");
        }
    }
    public boolean remove(T val)  {
        int index;
        if ((index = check(val)) != -1){
            Object[] temp = new Object[initSize];
            for (int i = 0; i < nowSize; i++) {
                if (index == i){
                    i++;
                }
                temp[i] = arr[i];
            }
        }else {
            System.out.println("没有该元素");
            return false;
        }
        return true;
    }

    private int check(T val) {
        for (int i = 0; i < arr.length; i++) {
            if (val.equals(arr[i])){
                return i;
            }
        }
        return -1;
    }
}

 

LinkedList相信大家都知道。LinkedList也就是链表。是链式的存储结构。非常适合数据的动态插入和删除。其次,由于链表的存储数据方式,其存储域是不连续的,不连续的存储于会使得我们随机访问和遍历的速度比较慢。在Java的内部实现里,LinkedList还提供了List中没有实现的一些方法。可以用于操作表头和表尾元素,可以当做堆栈,队列和双向队列使用。

下面我们实现一种基础的LinkedList

package 集合.LinkedList.手写LinkedList;

import 多线程.DeadLock.DiningTable;

/**
 * @program:多线程和IO
 * @descripton:
 * @author:ZhengCheng
 * @create:2021/10/8-15:33
 **/
public class LinkedList_<T> {
    Node head;

    public LinkedList_() {
        init();
    }

    public void show(){
        Node temp = head;
        while (temp.next!=null){
            System.out.print(temp+" ");
            temp = temp.next;
        }
        System.out.print(temp);
    }

    private void init() {
        head = new Node(0, null);
    }

    //get add remove 插入节点
    public void add(Node node) {
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
    }

    public Node get(T val) {
        Node temp = head;
        while (temp.next != null && temp.next.getT() != val) {
            temp = temp.next;
        }
        if (temp.next.getT().equals(val)){
            return temp.next;
        }
        if (temp.next == null) {
            System.out.println("没有" + val);
        }
        return null;
    }

    public void remove(T val) {
        Node temp = head;
        while (temp.next != null && temp.next.getT() != val) {
            temp = temp.next;
        }
        if (temp.next == null) {
            System.out.println("没有" + val);
        } else {
            System.out.println("移除成功");
            temp.next = temp.next.next;
        }
    }

    public void insert(int index ,Node node){
        //保证是能插入的不是空的
        Node temp = head ;
        for (int i = 0; i < index-1; i++) {
            if (temp.next!=null){
                temp = temp.next;
            }
        }
        if (temp.next == null){
            temp.next = node;
        }
        if (index != 1){
            node.next = temp.next;
            temp.next = node;
        }else {
            temp.next = head;
            head = temp;
        }

    }


}

class Node<T> {
    private int id;
    private T t;
    public Node next;

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", t=" + t +
                '}';
    }

    public Node(int id, T t) {
        this.id = id;
        this.t = t;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}
public class testDemo {
    public static void main(String[] args) {
        LinkedList_<String> linkedList = new LinkedList_<>();
        linkedList.add(new Node(1,"a"));
        linkedList.add(new Node(2,"b"));
        linkedList.add(new Node(3,"c"));
        linkedList.add(new Node(4,"d"));
        linkedList.add(new Node(5,"e"));
        Node a = linkedList.get("a");
        System.out.println(a);
        linkedList.insert(3,new Node(0,"insert"));
        linkedList.remove("c");
        linkedList.show();
    }
}

 Vector

对于Vector,我们是需要在使用多线程处理时,才会使用Vector。因为其使用了synchronized修饰,故其是线程安全的。

实际使用的情况较少。了解基础API即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值