做收益的网站多少钱,网站 建设设计,3322域名注册,微信软文范例大全100文章目录List接口概述List接口常用方法ArrayList实现类LinkedList实现类Vector实现类List接口概述
List集合类中元素有序、且可重复#xff0c;集合中的每个元素都有其对应的顺序索引
List容器中的元素都对应一个整数型的序号记载其在容器中的位置#xff0c;可以根据 序号…
文章目录List接口概述List接口常用方法ArrayList实现类LinkedList实现类Vector实现类List接口概述
List集合类中元素有序、且可重复集合中的每个元素都有其对应的顺序索引
List容器中的元素都对应一个整数型的序号记载其在容器中的位置可以根据 序号存取容器中的元素
List接口的实现类有ArrayList、LinkedList和Vector List接口常用方法
List除了从Collection集合继承的方法外List 集合里添加了一些根据索引来操作集合元素的方法。
void add(int index, Object ele):在index位置插入ele元素
boolean addAll(int index, Collection eles):从index位置开始将eles中的元素添加进来
Object get(int index):获取指定index位置的元素
int indexOf(Object obj):返回obj在集合中首次出现的位置
int lastIndexOf(Object obj):返回obj在当前集合中末次出现的位置
Object remove(int index):移除指定index位置的元素并返回此元素
Object set(int index, Object ele):设置指定index位置的元素为ele
List subList(int fromIndex, int toIndex):返回[fromIndex,toIndex)处的子集合 ArrayList实现类
线程不安全效率较高。底层使用数组进行存储。进行查找、末尾添加等操作相对较多的数据通常采用ArrayList实现类。
JDK1.7类似于单例中饿汉式
ArrayList list new ArrayList(); //无参构造器底层创建了长度为10的Object[]数组elementData
list.add(123); //elementData[0] new Integer(123);
...
list.add(11); //如果此次的添加导致底层elementData数组容量不够则会扩容默认情况下扩容为原来的1.5倍同时将原有的数组中的数据复制过去//有参构造器
ArrayList listTwo new ArrayList(int capacity);JDK1.8类似于单例中懒汉式
ArrayList list new ArrayList(); //无参构造器底层Object[] elementData初始化为{}即还没有创建长度为10的数组
list.add(123); //第一次调用add()时底层才创建长度为10的数组
//其余与JDK1.7一样注意Arrays.asList(…)方法返回的 List 集合既不是 ArrayList 实例也不是 Vector 实例。Arrays.asList(…)返回值是一个固定长度的 List 集合。 LinkedList实现类
线程不安全效率较高。对于频繁进行插入、删除的操作建议使用LinkedList类效率较高。
LinkedList list new LinkedList(); //内部声明了Node类型的first和last属性用于记录首末元素默认值为null
list.add(123); //创建Node对象将123封装到Node对象中该对象中还有prev和next两个变量分别用来记录前一个和下一个元素的位置Node类型的定义
private static class NodeE {E item;NodeE next;NodeE prev;Node(NodeE prev, E element, NodeE next) {this.item element;this.next next;this.prev prev;}
}Vector实现类
Vector是线程安全的。大多数操作与ArrayList相同。通过Vector()构造器创建对象时底层都创建长度为10的数组。在扩容方面默认扩容为原来的数组长度的2倍。
在各种List中最好把ArrayList作为缺省选择。当插入、删除频繁时使用LinkedListVector总是比ArrayList慢所以尽量避免使用。