Java数组列表

通过对数组的学习,我们了解到数组的三大特点,即:数据存储的连续性、数据类型的一致性、数据大小的固定性。这也正导致了数组在做数据存储时有着不可避免的局限性。于是乎,强大的Java语言为程序猿们提供了一个实现可变数组的类——ArrayList。

1.ArrayList的源码分析

①ArrayList的父类及已实现的接口

public class ArrayList<E> extends AbstractList<E>
     implements List<E>, RandomAccess, Cloneable, java.io.Serializable
//继承AbstractList类

②ArrayList的属性

大小属性:
/**
 * The size of the ArrayList (the number of elements it contains).
 *数组列表的大小(包含元素数)
 * @serial
 */private int size;
数组属性:
/**
 * Shared empty array instance used for empty instances.

 * 用于空实例的共享空数组实例
 */
private static final Object[] EMPTY_ELEMENTDATA = {};

③简介ArrayList常用方法(API文档介绍)

ArrayList实现List接口、底层使用数组保存所有元素(private transient Object[] elementData; ),故其操作基本上是对数组的操作
public void add(int index,E element);
//将指定的元素插入此列表中的指定位置(添加数据)

public E get(int index)
//返回此列表中指定位置上的元素(访问数据)

public int size();
//返回此列表中的元素数(获取元素个数)

public E set(int index,E element);
//用指定的元素替代此列表中指定位置上的元素(修改数据)

public void remove(int index);
//移除此列表中指定位置上的元素(删除数据)

public void clear();
//移除此列表中的所有元素。此调用返回后,列表将为空(清空列表)

public boolean isEmpty();
//如果此列表中没有元素,则返回 true(判断元素是否存在列表中)

2.ArrayList常用方法

①存储:
   /**
 * {@inheritDoc}
 *
 * <p>This implementation always throws an

 * 这个实现总是抛出一个
 * {@code UnsupportedOperationException}.

 * 不支持操作异常(当不支持请求的操作时,抛出该异常)
 *
 * @throws UnsupportedOperationException {@inheritDoc}

 * 不支持操作异常
 * @throws ClassCastException            {@inheritDoc}

 * 强制转换异常
 * @throws NullPointerException          {@inheritDoc}

 * 空指针异常
 * @throws IllegalArgumentException      {@inheritDoc}

 * 非法数据异常
 * @throws IndexOutOfBoundsException     {@inheritDoc}
 * 下标越界异常
public void add(int index, E element) {
    throw new UnsupportedOperationException();
}
②访问:
/**
 * {@inheritDoc}
 *
 * @throws IndexOutOfBoundsException {@inheritDoc}
 * 下标越界异常
 */
abstract public E get(int index);
③获取元素个数;
④修改数据:
/**
 * {@inheritDoc}
 *
 * <p>This implementation always throws an
 * {@code UnsupportedOperationException}.
 *
 * @throws UnsupportedOperationException {@inheritDoc}
 * @throws IndexOutOfBoundsException     {@inheritDoc}
 */
public E remove(int index) {
    throw new UnsupportedOperationException();
}
⑤清空列表:
/**
 * Removes all of the elements from this list (optional operation).

 * 移除列表中所有元素(可选操作)
 * The list will be empty after this call returns.

 * 此调用返回后,列表将为空
 *
 * <p>This implementation calls {@code removeRange(0, size())}.
 *
 * <p>Note that this implementation throws an

 * 注意,这个实现抛出了一个 
 * {@code UnsupportedOperationException} unless {@code remove(int

 * 不支持操作异常除非remve()
 * index)} or {@code removeRange(int fromIndex, int toIndex)} is

 * 或removeRange()方法
 * overridden.

 * 被重写
 *
 * @throws UnsupportedOperationException if the {@code clear} operation
 *         is not supported by this list
 */
public void clear() {
    removeRange(0, size());
}
⑥判断元素是否存在列表中。

3.ArrayList+泛型

在类定义的时候,某种类型不确定,则可以使用泛型。泛型代表的是基本类型以外的类型,常用的指代关系是——E : 元素;K :键;V :值 。
存在泛型的情况:
public class MyArrayList extends ArrayList{
public static void main(String [] args){
//String为非基本类型
MyArrayList myArrayList=new MyArrayList();
myArrayList.add(0, “afaf”);
myArrayList.add(1, “dewd”);
myArrayList.add(2, “ad”);
myArrayList.add(3, “afad”);
myArrayList.remove(3);
System.out.println(myArrayList);
}
}

4.ArrayList优缺点

优势——

①支持自动改变大小的功能(最大的优势);
②利用泛型可以用于存储不同非基本类型的数据;
③可以灵活的插入元素 ;
④可以灵活的删除元素 。

劣势——

跟数组相比,速度较慢。主要影响效率的是,每当执行Add、AddRange、Insert、InsertRange等添加元素的方法,都会检查内部数组的容量是否不够了,如果是,它就会以当前容量的两倍来重新构建一个数组,将旧元素Copy到新数组中,然后丢弃旧数组。

更多内容:点击这里

关注微信公众号,最新技术干货实时推送
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值