ArrayList和LinkedList的简单实现

本文详细解析了ArrayList和LinkedList两种数据结构的底层实现原理。ArrayList使用动态数组存储元素,通过调整数组大小来适应元素数量的变化;而LinkedList则采用双向链表的方式,每个元素由节点构成,包含前驱和后继指针,适用于频繁插入和删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ArrayList

public class ArrrayList1 {
    Object[] elementData;
    int size;
    public ArrrayList1() {
        this(10);
    }
    public ArrrayList1(int c){
        if(c>0){
            elementData=new Object[c];
        }else{
            elementData=new Object[10];
        }
    }
    public boolean add(Object obj){
        if(size+1>elementData.length){
            Object[] newArray=new Object[size+(size>>1)];
            System.arraycopy(elementData,0,newArray,0,size);
            elementData=newArray;
        }
        elementData[size++]=obj;
       return true;
   }
    public boolean add(int index,Object obj){
        if(index<0||index>=size){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(size>elementData.length){
            Object[] newArray=new Object[size+(size>>1)];
            System.arraycopy(elementData,0,newArray,0,size);
            elementData=newArray;
        }
        System.arraycopy(elementData,index,elementData,index+1,size-index);
        elementData[index]=obj;
        return true;
    }
    public Object get(int index){
        if(index<0||index>=size){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return elementData[index];
    }
    public Object remove(int index){
        if(index<0||index>=size){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Object oldvlue=elementData[index];
        System.arraycopy(elementData,index+1,elementData,index,size-index-1);
        elementData[--size]=null;
        return oldvlue;
    }
    public static void main(String[] args){
        ArrrayList1 arrrayList1=new ArrrayList1();
        arrrayList1.add("da");
        arrrayList1.add("yeye");
        arrrayList1.add("zhang");
        arrrayList1.add("1");
        arrrayList1.add("2");
        arrrayList1.add("3");arrrayList1.add("8");
        arrrayList1.add("4");
        arrrayList1.add("5");
        arrrayList1.add("6");
        arrrayList1.add("7");

        arrrayList1.remove(2);
        for(int i=0;i<arrrayList1.size;i++){
            System.out.println(arrrayList1.get(i));
        }
    }
}

ArrayList的底层实现是数组

LinkedList

public class LinkList {
    Node first;//头
    Node last;//尾
    int size;
    public void add(Object obj){
        Node n=new Node();
        if(first==null){
            first=n;
            first.previous=null;
            first.obj=obj;
            first.next=null;
            first=n;
            last=n;
        }else{
            n.previous=last;
            last.next=n;
            n.obj=obj;
            n.next=null;
            last=n;
        }
        size++;
    }
    public Object get(int index){
        Node temp=first;
        for(int i=0;i<index;i++){
            temp=temp.next;
        }
        return temp.obj;
    }
    public void move(Object obj){
        Node temp=first;
        for(int i=0;i<size;i++){
            if(temp.obj.equals(obj)){
                Node up=temp.previous;
                Node down=temp.next;
                up.next=down;
                down.previous=up;
                size--;
            }
            temp=temp.next;

        }
    }
    public static void main(String[] args){
        LinkList linkList=new LinkList();
        linkList.add("zhangrui");
        linkList.add("ruirui");
        linkList.add("zhang");
        linkList.move("ruirui");
        for(int i=0;i<linkList.size;i++){
            System.out.println(linkList.get(i));
        }


    }
}
class Node{//节点
    Node previous;
    Object obj;
    Node next;
}

LinkedList的底层实现是链表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值