【数据结构-Java描述】List接口/ArrayList与顺序表/LinkedList与链表

一.List接口

1.线性表

线性表是n个数据元素的有限序列,常见的线性表有顺序表、链表、栈、队列...
线性表上存放的元素呈一条连续的直线,所以说线性表在逻辑上是连续的,但在物理结构上不一定连续,因为线性表是可以由数组和链表实现的。

2.List接口

List是一个继承至Collection的接口,从数据结构的角度看,List是一个线性表,可以存储一组不唯一,类型相同,插入顺序有序的对象,并可以通过List接口提供的方法进行增删改查。

使用List接口能够精确的控制每个元素插入的位置,能够通过索引(元素在List中的位置)来访问List中的元素,第一个元素的索引为0,List允许有相同的元素。

List接口中的常用方法

boolean add(E e)尾插 e
void add(int index, E element)将 e 插入到 index 位置
boolean addAll(Collection<? extends E> c)尾插 c 中的元素
E remove(int index)删除 index 位置元素
boolean remove(Object o)删除遇到的第一个 o
E get(int index)获取下标 index 位置元素
E set(int index, E element)将下标 index 位置元素设置为 element
void clear()清空
boolean contains(Object o)判断 o 是否在线性表中
int indexOf(Object o)返回第一个 o 所在下标
int lastIndexOf(Object o)返回最后一个 o 的下标
List<E> subList(int fromIndex, int toIndex)截取部分 list

 List是一个接口,是不能够直接进行实例化的,使用List接口,需要去实例化List接口的具体实现类,这里有ArrayList和LinkedList。


二.ArrayList与顺序表

1.顺序表

线性表分为顺序表及链表,顺序表上存储的数据元素在内存单元是连续分布的,因此顺序表既逻辑连续也物理连续。一般情况下采用数组存储。在数组上完成数据的增删查改。

2.模拟实现一个顺序表

我们可以用数组实现一个顺序表,在数组的基础上实现增删改查。

这里我们实现的顺序表可以自动扩容,每次以原数组大小的两倍进行扩容。

import java.util.Arrays;

public class MyArraylist {

    public int[] elem;
    public int usedSize;//使用大小
    private static final int DEFAULT_SIZE = 5;

    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if (isFull()) resize();
        elem[usedSize] = data;
        usedSize++;
    }

    //2倍扩容
    public void resize() {
        elem = Arrays.copyOf(elem, 2 * elem.length);
    }

    /**
     * 判断当前的顺序表是不是满的!
     *
     * @return true:满   false代表空
     */
    public boolean isFull() {
        return usedSize == elem.length;
    }


    private boolean checkPosInAdd(int pos) {
        if (pos<0 || pos > usedSize) return false;
        return true;//合法
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if (checkPosInAdd(pos)) {
            if (isFull()) resize();
            for(int i=usedSize;i>pos;i--){
                elem[i]=elem[i-1];
            }
            elem[pos]=data;
            usedSize++;
        }
    }

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

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind) return i;
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if (pos < usedSize) return elem[pos];
        return -1;
    }

    private boolean isEmpty() {
        return usedSize == 0;
    }

    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        if (pos < usedSize) {
            elem[pos] = value;
        }
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public void remove(int key) {
        if(isEmpty()) return;
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == key) {
                while (i + 1 < usedSize) {
                    elem[i] = elem[i + 1];
                    i++;
                }
                usedSize--;
                break;
            }
        }
    }

    // 获取顺序表长度
    public int size() {
        return usedSize;
    }

    // 清空顺序表
    public void clear() {
        usedSize = 0;
        elem = null;
    }
}

 3.ArrayList

在Java集合框架中,ArrayList是一个普通的类,实现了List接口,我们可以直接通过实例化ArrayList来实现一个顺序表,

Arrlist的构造:

ArrayList() //无参构造
ArrayList(Collection<? extends E> c) //利用其他 Collection 构建 ArrayList
ArrayList(int initialCapacity) //指定顺序表初始容量

ArrayLisy的常用方法和上述List常用方法一致。

4.ArrayList的遍历与扩容

ArrayList的遍历可以通过For+下标,foreach和迭代器实现:

//使用下标+for遍历
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
} 
System.out.println();

//使用foreach遍历
for (Integer integer : list) {
System.out.print(integer + " ");
} 
System.out.println();
//使用迭代器遍历
Iterator<Integer> it = list.listIterator();
while(it.hasNext()){
System.out.print(it.next() + " ");
}

ArrayList是一个动态类型的顺序表,在插入元素的过程中会自动扩容,

ArrayList的扩容机制如下:

1. 检测是否真正需要扩容,如果是调用grow准备扩容
2. 预估需要库容的大小
    初步预估按照1.5倍大小扩容
    如果用户所需大小超过预估1.5倍大小,则按照用户所需大小扩容
    真正扩容之前检测是否能扩容成功,防止太大导致扩容失败
3. 使用copyOf进行扩容

二.LinkedList与链表

1.ArrayList的缺陷

ArrayList的底层是一个数组,其在内存单元是连续储存的,所以当我们想插入或删除一个顺序表中的元素时,需要将数组进行前后挪移,该操作的时间复杂度为O(n);所以,当涉及插入和删除较多的操作时,就不适合使用ArrayList了,但是Java提供了另一数据结构LinkedList即链表。

2.链表

链表在物理存储结构上是非连续存的构,数据元素之间逻辑顺序是通过链表中的引用链接次序实现。



链表种类较多,存在带头与不带头链表,单向与双向链表,循环与非循环链表。

3.无头单向链表的模拟实现

class SingleLinkedList {
        public int val;
        public SingleLinkedList next;
        public SingleLinkedList head;
        public int usersize;
        SingleLinkedList(int data) {
            this.val=data;
        }
        SingleLinkedList() {

        }
//头部插入
    public void addFirst(int data){
        SingleLinkedList Node=new SingleLinkedList(data);
        Node.next=head;
        head=Node;
        usersize++;
    }
//尾插插入
    public void addLast(int data){
        if(this.head==null){
            addFirst(data);
            return;
        }
        SingleLinkedList cur=head;
            while(cur.next!=null){
                cur=cur.next;
            }
        SingleLinkedList Node=new SingleLinkedList(data);
        cur.next=Node;
        usersize++;
    }
//指定位置插入
    public boolean addIndex(int index,int data){
            if(index>usersize || index<0) return false;
            if(index==usersize) {
                addLast(data);
                return true;
            }
            if(index==0) {
                addFirst(data);
                return true;
            }
           SingleLinkedList cur=head;
           SingleLinkedList prev = null;
           while(index>0){
               prev=cur;
               cur=cur.next;
               index--;
           }
           SingleLinkedList Node=new SingleLinkedList(data);
           prev.next=Node;
           Node.next=cur;
           usersize++;
           return true;
    }
    //查找是否包含关键字key是否在单链表当中public boolean contains(int key){
        SingleLinkedList cur=head;
        while(cur!=null){
            if(cur.val==key) return true;
            cur=cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点public void remove(int key){
        SingleLinkedList cur=head;
        SingleLinkedList prev=null;
        while(cur!=null){
            if(cur.val==key) {
                if(prev==null){
                    this.head=head.next;
                    usersize--;
                    return;
                }else{
                    prev.next=cur.next;
                    usersize--;
                    return;
                }
            }
            prev=cur;
            cur=cur.next;
        }
    }
//删除全部关键字为key的节点
    public void removeAllKey(int key){
        SingleLinkedList cur=head;
        SingleLinkedList prev=head;
        while(cur!=null){
            if(cur.val==key) {
                if(prev==this.head){
                    this.head=head.next;
                }else{
                    prev.next=cur.next;
                }
                usersize--;
                prev=cur.next;
                cur=cur.next;
                continue;
            }
            prev=cur;
            cur=cur.next;
        }
    }
    //得到单链表的长度
   public int size(){
            return usersize;
    }
    public void display(){
            SingleLinkedList cur=head;
            while(cur!=null){
                System.out.println(cur.val+" ");
                cur=cur.next;
            }
    }

    public void clear(){
            this.head.next=null;
            this.head=null;
    }
}

 4.LinkedList

LinkedList的底层是双向链表结构,由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。用LinkeList可以实现链表,也可以实现栈,双端队列。
5.LinkedList的构造与遍历

LinkedList() //无参构造
public LinkedList(Collection<? extends E> c) 
//使用其他集合容器中元素构造List

LinkedList是双向链表,所以可以进行正反两头遍历。

// foreach遍历
for (int e:list) {
System.out.print(e + " ");
}
System.out.println();
// 使用迭代器遍历---正向遍历
ListIterator<Integer> it = list.listIterator();
while(it.hasNext()){
System.out.print(it.next()+ " ");
} 
System.out.println();
// 使用反向迭代器---反向遍历
ListIterator<Integer> rit = list.listIterator(list.size());
while (rit.hasPrevious()){
System.out.print(rit.previous() +" ");
}

6.ArrayList与LinkedList区别

存储空间上物理上一定连续逻辑上连续,但物理上不一定连续
随机访问支持O(1)不支持:O(N)
头插需要搬移元素,效率低O(N)只需修改引用的指向,时间复杂度为O(1)
插入空间不够时需要扩容没有容量的概念
应用场景元素高效存储+频繁访问任意位置插入和删除频繁

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值