当前位置:网站首页>Listiterator list iterator
Listiterator list iterator
2022-06-13 05:02:00 【Jason_ LH1024】
Source code analysis :
public interface List<E>{
Iterator<E> iterator();
ListIterator<E> listIterator();
boolean add(E e);
}
public abstract class AbstractList<E>{
int modCount = 0;
}
//ArrayList Inherited a class and implemented an interface
public class ArrayList<E> extends AbstractList<E> implements List<E>{
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
}
}
public ListIterator<E> listIterator() {
return new ListItr(0);
}
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount; // Assign the actual modified value to the expected modified value
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
Achieve it :
package com.it04;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("java");
// adopt list A collection of listIterator() Method to get
// List iterator
ListIterator<String> lit = list.listIterator();
// Positive traversal
while (lit.hasNext()){
String s = lit.next();
System.out.println(s);
}
System.out.println("====================");
// Reverse traversal
while (lit.hasPrevious()){
String s = lit.previous();
System.out.println(s);
}
// Get list iterator method traversal
// ListIterator<String> lit = list.listIterator();
while (lit.hasNext()){
String s = lit.next();
if (s.equals("world")){
// It's using ListIterator Of list To use the add method *****
lit.add(" Simple and honest ");
}
}
System.out.println(list);
}
}
// It's using ListIterator Of list To use the add method *****
边栏推荐
- Advanced C language - Section 1 - data storage
- Violence enumeration~
- RTSP streaming using easydarwin+ffmpeg
- 2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation
- C language learning log 2.19
- PostgreSQL Guide: inside exploration (Chapter 10 basic backup and point in time recovery) - Notes
- Analysis on the usage, response and global delivery of provide/inject
- QT using layout manager is invalid or abnormal
- Time display of the 12th Blue Bridge Cup
- Chapter 18 pagination: Introduction
猜你喜欢
关于匿名内部类
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
Section 4 - arrays
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
Mind mapping series - Database
2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
Draw a hammer
Configuration used by automatic teaching evaluation script
Win8.1和Win10各自的優勢
Solution to sudden font change in word document editing
随机推荐
Several methods of identifying equivalent circuit of circuit drawing
C language learning log 1.22
Violence enumeration~
Luogu p1088 Martians
Elliptic curve encryption
Sampo Lock
All blog navigation
Regular expressions in QT
QT signal is automatically associated with the slot
Explain the role of key attribute in V-for
Luogu p1036 number selection
RMQ、LCA
shell变量学习笔记
rainbow
Advanced C - Section 3 - character functions and string functions
Win8.1和Win10各自的优势
Win8.1和Win10各自的優勢
Analysis of the principle of V-model and its application in user defined components
Sort (internal sort) + external sort
C language exercise 1