Collection子接口 其一:List接口List

接口存储结构:元素有序,且可重复,每个元素都有对应的索引
根据索引获取容器元素

实现类有:ArrayList、LinkedList、Vector

三个实现类的异同?

- 都实现了List接口,存储数据的特点相同、存储有序的、可重复的数据

- ArrayList  是List接口的主要实现类,线程不安全的,但是效率高,底层Object[] elementData存储

- LinkedList  双向链表结构存储,对于插入和删除频繁操作,效率比ArrayList高

- Vector  初代List接口实现类,线程安全,效率低。底层Object[]存储

List的接口常用方法【索引支持】

在指定索引位置添加元素

public void add(int index, E element)

    /**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}

从参数的容器对象的索引位置开始到结尾,添加元素到调用此方法的容器对象

public boolean addAll(int index, Collection<? extends E> c)

    /**
* Inserts all of the elements in the specified collection into this
* list, starting at the specified position. Shifts the element
* currently at that position (if any) and any subsequent elements to
* the right (increases their indices). The new elements will appear
* in the list in the order that they are returned by the
* specified collection's iterator.
*
* @param index index at which to insert the first element from the
* specified collection
* @param c collection containing elements to be added to this list
* @return <tt>true</tt> if this list changed as a result of the call
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws NullPointerException if the specified collection is null
*/
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index); Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved); System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}

按索引获取元素

public E get(int index)

    /**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
rangeCheck(index); return elementData(index);
}

返回元素的索引位置,如果是重复了多个元素,返回第一个的位置

public int indexOf(Object o)

    /**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

返回元素的索引位置,如果多个重复元素,返回最后一个出现的

public int lastIndexOf(Object o)

    /**
* Returns the index of the last occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the highest index <tt>i</tt> such that
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

移除参数的索引上的元素,并返回元素

public E remove(int index)

    /**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their
* indices).
*
* @param index the index of the element to be removed
* @return the element that was removed from the list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
rangeCheck(index); modCount++;
E oldValue = elementData(index); int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work return oldValue;
}

修改指定索引位置的元素

public E set(int index, E element)

    /**
* Replaces the element at the specified position in this list with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
rangeCheck(index); E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}

截取一段,返回指定起始位置到结束位置的元素到一个新的容器上 【返回子集合】

public List<E> subList(int fromIndex, int toIndex)

    /**
* Returns a view of the portion of this list between the specified
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. (If
* {@code fromIndex} and {@code toIndex} are equal, the returned list is
* empty.) The returned list is backed by this list, so non-structural
* changes in the returned list are reflected in this list, and vice-versa.
* The returned list supports all of the optional list operations.
*
* <p>This method eliminates the need for explicit range operations (of
* the sort that commonly exist for arrays). Any operation that expects
* a list can be used as a range operation by passing a subList view
* instead of a whole list. For example, the following idiom
* removes a range of elements from a list:
* <pre>
* list.subList(from, to).clear();
* </pre>
* Similar idioms may be constructed for {@link #indexOf(Object)} and
* {@link #lastIndexOf(Object)}, and all of the algorithms in the
* {@link Collections} class can be applied to a subList.
*
* <p>The semantics of the list returned by this method become undefined if
* the backing list (i.e., this list) is <i>structurally modified</i> in
* any way other than via the returned list. (Structural modifications are
* those that change the size of this list, or otherwise perturb it in such
* a fashion that iterations in progress may yield incorrect results.)
*
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
} static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}

List支持的三种遍历方式

public class Array {
public static void main(String[] args) {
List list = new ArrayList();
list.add("元素1");
list.add("元素2");
list.add("元素3");
list.add("元素4");
list.add("元素5");
list.add("元素6"); // 第一种 迭代器实现遍历
Iterator iterator = list.iterator(); while (iterator.hasNext()){
System.out.println(iterator.next());
} // 第二种 ForEach
for (Object object : list) {
System.out.println(object);
} // 第三种 For
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}

注意点: remove的重载

如何判断注入的是按元素删除?还是按索引删除?

- 按元素删除,建议使用 new Integer(10) 这样对基本类型数据包装再注入   arrayList.remove(new Integer(5));

- 按索引删除,默认注入数字值即可   arrayList.remove(3);


ArrayList的底层源码查看【未完待续...】

JDK8

底层elementData初始化为0

但是注释依赖没改,仍然称为10初始化容量

只有在第一次调用add后初始化10的容量,再往后大于10【右移1 除2】增加1.5倍容量

【Java】Collection子接口:其一 List 列接口的更多相关文章

  1. Java基础-Collection子接口之List接口

    Java基础-Collection子接口之List接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们掌握了Collection接口的使用后,再来看看Collection接口中 ...

  2. Java基础-Collection子接口之Set接口

    Java基础-Collection子接口之Set接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 学习Collection接口时,记得Collection中可以存放重复元素,也可 ...

  3. Collection子接口(List/Set/Queue/SortedSet)

    Collection基本的子接口: List:能够存放反复内容 Set:不能存放反复内容,全部反复的内容靠hashCode()和equals()两个方法区分 Queue:队列接口 SortedSet: ...

  4. Java面向对象之 接口: [修饰符] interface 接口名 {...};子接口:[修饰符] interface 接口名 extends 父接口,父接口2...{...}

    1.什么是接口? 类比抽象类,把功能或者特性类似的一类 抽象的更彻底,可以提炼出更加特殊的"抽象类"----接口 2.如何定义接口 语法:  [修饰符] interface 接口名 ...

  5. Java学习关于集合框架的基础接口--Collection接口

     集合框架(Collection  Framework)是Java最强大的子系统之一,位于java.util 包中.集合框架是一个复杂的接口与和类层次,提供了管理对象组的最新技术.Java集合框架标准 ...

  6. java Collection接口

    Collection 1——————Set子接口:无序,不允许重复. 2——————List子接口:有序,允许重复. Set和List对比: 1.set:检索元素的效率比较低,删除和插入效率比较高,删 ...

  7. 自顶向下理解Java集合框架(三)Map接口

    Map基本概念 数据结构中Map是一种重要的形式.Map接口定义的是查询表,或称查找表,其用于储存所谓的键/值对(key-value pair),其中key是映射表的索引. JDK结构中还存在实现Ma ...

  8. 数据结构-List接口-LinkedList类-Set接口-HashSet类-Collection总结

    一.数据结构:4种--<需补充> 1.堆栈结构:     特点:LIFO(后进先出);栈的入口/出口都在顶端位置;压栈就是存元素/弹栈就是取元素;     代表类:Stack;     其 ...

  9. Effective Java 第三版——41.使用标记接口定义类型

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  10. Java第二十天,Map集合(接口)

    Map接口 一.定义 Map集合是双列集合,即一个元素包含两个值(一个key,一个value),Collection集合是单列集合. 定义格式: public interface Map<K,V ...

随机推荐

  1. 一个或多个C文件编译KO

    参考文档:.c文件如何编译为ko的MAKEFILE文件编写 - young525 - 博客园 (cnblogs.com) 文档组织结构 header目录:存放头文件 source目录:存放源文件 单个 ...

  2. drawio中添加数学公式

    1.drawio简介 drawio是一款免费开源的流程图绘制软件,由于软件免费,而且模块也很丰富,我比较喜欢用它. 软件下载地址:https://github.com/jgraph/drawio-de ...

  3. NumPy 通用函数(ufunc):高性能数组运算的利器

    NumPy 通用函数(ufunc) 简介 NumPy 通用函数(ufunc),代表"通用函数",是一类用于对 ndarray 对象进行逐元素运算的高性能函数.ufunc 使 Num ...

  4. go 1.6 废弃 io/ioutil 包后的替换函数

    go 1.6 废弃 io/ioutil  包后的替换函数 io/ioutil 替代 ioutil.ReadAll -> io.ReadAll ioutil.ReadFile -> os.R ...

  5. 手动解压安装mysql8.0 on windows my.ini

    1.解压"mysql-8.0.24-winx64.zip"到d:\Soft 2.在"D:\Soft\mysql-8.0.24-winx64"目录新建一个my.i ...

  6. 如何解决系统报错:nf_conntrack: table full, dropping packets

    问题 在系统日志中(/var/log/messages),有时会看到大面积的下面的报错: nf_conntrack: table full, dropping packet 这说明系统接到了大量的连接 ...

  7. readonly和disable的区别是什么?

    Readonly和Disabled两种属性的写法如下: 1.<input type="text" name="name" value="xxx& ...

  8. 模拟epoll的饥饿场景

    说明 一直听说epoll的饥饿场景,但是从未在实际环境中面对过,那么能不能模拟出来呢?实际的情况是怎样呢? 模拟步骤 基于epoll写一个简单的tcp echo server,将每次read返回的字节 ...

  9. CountDownLatch demo演示裁判和选手赛跑

    # CountDownLatch demo演示裁判和选手赛跑 package com.example.core.mydemo; import java.util.concurrent.CountDow ...

  10. #PowerBi 1分钟学会,用PowerBi获取数据库最近90天的数据(DATE_SUB)

    在powerbi报表中,我们往往会对数据源进行日常刷新,powerbi链接了数据库的情况下,根据日期灵活取数是我们必须掌握的一个技能. 在本文中,我们将介绍如何使用 SQL 的 DATE_SUB 函数 ...