Java by API/java.util/ListIterator
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
ListIterator: add(T e)
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Main{
public static void main(String[] a) {
List<String> list = new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
ListIterator<String> iter = list.listIterator(list.size());
while (iter.hasPrevious()) {
System.out.println(iter.previous());
iter.add("a");
break;
}
System.out.println(list);
}
}
ListIterator: hasNext()
/**
*Output:
Original contents of al: C A E B D F
Modified list backwards: F+ D+ B+ E+ A+ C+
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
System.out.print("Original contents of al: ");
Iterator<String> itr = al.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
ListIterator<String> litr = al.listIterator();
while (litr.hasNext()) {
String element = litr.next();
litr.set(element + "+");
}
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}
}
}
ListIterator: hasPrevious()
/**
*Output:
Original contents of al: C A E B D F
Modified list backwards: F+ D+ B+ E+ A+ C+
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
System.out.print("Original contents of al: ");
Iterator<String> itr = al.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
ListIterator<String> litr = al.listIterator();
while (litr.hasNext()) {
String element = litr.next();
litr.set(element + "+");
}
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}
}
}
ListIterator: next()
/**
*Output:
Original contents of al: C A E B D F
Modified list backwards: F+ D+ B+ E+ A+ C+
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MainClass {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
System.out.print("Original contents of al: ");
Iterator<String> itr = al.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
ListIterator<String> litr = al.listIterator();
while (litr.hasNext()) {
String element = litr.next();
litr.set(element + "+");
}
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}
}
}
ListIterator: nextIndex()
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());
}
}
ListIterator: previous()
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Main{
public static void main(String[] a) {
List<String> list = new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
ListIterator<String> iter = list.listIterator(list.size());
while (iter.hasPrevious()) {
System.out.println(iter.previous());
iter.add("a");
break;
}
System.out.println(list);
}
}
ListIterator: previousIndex()
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());
}
}
ListIterator: remove()
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);
}
}
}
ListIterator: set(T e)
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);
}
}
}