美文网首页
List接口及实现

List接口及实现

作者: 萌妈码码 | 来源:发表于2018-05-24 23:24 被阅读0次

List Interface

1.两个包含相同元素和顺序的List逻辑上是相等的。

2.两种基本实现ArrayList, LinkedList。

3.位置访问。get, set, add, addAll, remove

4.搜索。indexOf, lastIndexOf

5.List Iterator相关 (ListIterator继承了Iterator)

a. 正向迭代

it=list.listIterator(); //default, at the first element

hasNext()

next()

remove() //定义在Iterator Interface中,删除上一次next()调用返回的元素

nextIndex()

b.方向迭代

it=list.listIterator(list.size()); //default, at the last element

hasPrevious()

previous()

remove() //定义在Iterator Interface中,删除上一次previous()调用返回的元素

previousIndex()

使用迭代器实现List.indexOf(E e):

Sample Code

c. 除了Iterator提供的remove()之外,ListIterator还提供了两种修改list的操作

· set(E e)覆盖上一次next() or previous()返回的元素

下面这个例子给出了如何使用set()将一个list中的某元素全部替换成另一个元素

Sample Code

·add(E e)在当前光标位置插入一个元素

下面这个例子给出了如何使用add()将一个list中的某元素全部替换成另一个list

Sample Code

6. Range-View Operations

a. subList(int fromIndex, int toIndex)返回的是原list中fromIndex(inclusive)到toIndex(exclusive)的部分。

b.注意Range-View,这个subList的背后还是原本的list。因此原list的变化在subList上都可以体现出来。

c. 所有参数类型是个List的方法都可以使用subList View。如:

a. list.subList(fromIndex, toIndex).clear();

b. int i = list.subList(fromIndex, toIndex).indexOf(o);

c. int j = list.subList(fromIndex, toIndex).lastIndexOf(o);

d. 应该慎重是使用subList,因为在backing list上的增删操作会使得之前应以的subList语义失效。借助subList(作为一种临时对象是实现)来实现对backing list上的范围操作是比较推荐的做法,比如上面的例子。

7.List Algorithms

--sort — sorts a List using a merge sort algorithm, which provides a fast, stable sort. (A stable sort is one that does not reorder equal elements.)

--shuffle — randomly permutes the elements in a List.

--reverse — reverses the order of the elements in a List.

--rotate — rotates all the elements in a List by a specified distance.

--swap — swaps the elements at specified positions in a List.

--replaceAll — replaces all occurrences of one specified value with another.

--fill — overwrites every element in a List with the specified value.

--copy — copies the source List into the destination List.

--binarySearch — searches for an element in an ordered List using the binary search algorithm.

--indexOfSubList — returns the index of the first sublist of one List that is equal to another.

--lastIndexOfSubList — returns the index of the last sublist of one List that is equal to another.

相关文章

  • List接口及实现

    List Interface 1.两个包含相同元素和顺序的List逻辑上是相等的。 2.两种基本实现ArrayLi...

  • JAVA Vector学习笔记

    Vector Vector是实现了List接口和RandomAccess接口的集合类 构造方法及变量 数组默认大小...

  • Java中List和ArrayList的区别

    List是一个接口,而ArrayList是List接口的一个实现类。 ArrayList类继承并实现了List接口...

  • ArrayList源码分析

    ArrayList源码分析 类的实现接口及继承父类 AbstractList 和 List ArrayLis...

  • Java自学-集合框架 List接口

    ArrayList与List接口 步骤 1 : ArrayList和List ArrayList实现了接口List...

  • List/set接口及实现类——ArrayList/hashSe

    List接口及实现类——ArrayList 1、Lits是元素有序并且可以重复的集合,被称为序列 2、List可以...

  • List及其实现类

    读代码跟写代码一样重要 List及实现类关系 上述类图中,Collection、List为接口;AbstractC...

  • Collection(集合)

    Collection接口 Collection接口包括List接口和Set接口 1 list接口: 包括三个实现类...

  • ArrayList

    名词解释 ArrayList是集合的一种实现,实现了接口List,List接口继承了Collection接口。Co...

  • 集合

    1、概述 List、Set、Queue都是接口,均实现Collection接口。 List下有ArrayList、...

网友评论

      本文标题:List接口及实现

      本文链接:https://www.haomeiwen.com/subject/tpvejftx.html