Java Tutorial/Collections/ListIterator

Материал из Java эксперт
Перейти к: навигация, поиск

Add or insert an element to ArrayList using Java ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
    ListIterator<String> listIterator = aList.listIterator();
    listIterator.next();
    listIterator.add("Added Element");
    for (String str: aList){
      System.out.println(str);
    }
  }
}





Get Previous and next index using Java ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
    ListIterator<String> listIterator = aList.listIterator();
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());
    // advance current position by one using next method
    listIterator.next();
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());
  }
}





Iterate through elements ArrayList using ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    ListIterator itr = arrayList.listIterator();
    System.out.println("in forward direction");
    while (itr.hasNext()) {
      System.out.println(itr.next());
    }
    System.out.println("in backward direction");
    while (itr.hasPrevious()) {
      System.out.println(itr.previous());
    }
  }
}
/*in forward direction
1
2
3
4
5
in backward direction
5
4
3
2
1
*/





Iterate through elements of Java LinkedList using ListIterator

import java.util.ListIterator;
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    ListIterator itr = lList.listIterator();
    System.out.println("forward direction");
    while (itr.hasNext()){
      System.out.println(itr.next());
    }
    System.out.println("reverse direction");
    while (itr.hasPrevious()){
      System.out.println(itr.previous());
    }
  }
}





ListIterator: In reverse order

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class MainClass {
  public static void main(String[] a) {
    List list = new LinkedList();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    ListIterator iter = list.listIterator(list.size());
    while (iter.hasPrevious()) {
      System.out.println(iter.previous());
    }
  }
}





ListIterator: Like an Iterator

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class MainClass {
  public static void main(String[] a) {
    List list = new LinkedList();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    ListIterator iter = list.listIterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
  }
}



A
B
C
D


Modifying the list that the iterator came from: add(), remove(), and set()

public void add(Object element)
public void remove()
public void set(Object element)



D
[A, B, C, a, D]


Remove an element from ArrayList using Java ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
    // Get an object of ListIterator using listIterator() method
    ListIterator listIterator = aList.listIterator();
    listIterator.next();
    listIterator.next();
    // remove element returned by last next method
    listIterator.remove();
    for (String str: aList){
      System.out.println(str);
    }
  }
}





Replace an element from ArrayList using Java ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
    ListIterator<String> listIterator = aList.listIterator();
    listIterator.next();
    listIterator.set("100");
    for (String str : aList) {
      System.out.println(str);
    }
  }
}