Example usage for java.util ListIterator hasNext

List of usage examples for java.util ListIterator hasNext

Introduction

In this page you can find the example usage for java.util ListIterator hasNext.

Prototype

boolean hasNext();

Source Link

Document

Returns true if this list iterator has more elements when traversing the list in the forward direction.

Usage

From source file:Main.java

public static void main(String args[]) {
    ArrayList<Integer> arrlist = new ArrayList<Integer>();

    arrlist.add(1);//from w w  w  .j  av a  2 s . c  om
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ListIterator<Integer> iterator = arrlist.listIterator();
    while (iterator.hasNext()) {
        Integer i = iterator.next();
        iterator.remove();

    }
    System.out.println(arrlist);

}

From source file:Main.java

public static void main(String args[]) {
    ArrayList<Integer> arrlist = new ArrayList<Integer>();

    arrlist.add(1);/*  w ww.  ja va 2s .c  o m*/
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ListIterator<Integer> iterator = arrlist.listIterator(2);
    while (iterator.hasNext()) {
        Integer i = iterator.next();
        iterator.remove();

    }
    System.out.println(arrlist);

}

From source file:MainClass.java

public static void main(String[] args) {
    String[] coins = { "A", "B", "C", "D", "E" };

    List src = new LinkedList();
    for (int i = 0; i < coins.length; i++)
        src.add(coins[i]);//from  w w w  . j  a va 2 s  .c o m

    List dst = new ArrayList();
    for (int i = 0; i < coins.length; i++)
        dst.add("");

    Collections.copy(dst, src);

    ListIterator liter = dst.listIterator();

    while (liter.hasNext())
        System.out.println(liter.next());

    Collections.fill(src, "no coins");

    liter = src.listIterator();

    while (liter.hasNext())
        System.out.println(liter.next());
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Oracle");
    list.add("SQL");
    list.add("CSS");
    list.add("XML");
    System.out.println("List: " + list);
    // Get the list iterator
    ListIterator<String> iterator = list.listIterator();
    while (iterator.hasNext()) {
        int index = iterator.nextIndex();
        String element = iterator.next();
        System.out.println("Index=" + index + ", Element=" + element);
    }/*from w  w  w  .j a  v a 2 s  .  co m*/
    // Reuse the iterator to iterate from the end to the beginning
    while (iterator.hasPrevious()) {
        int index = iterator.previousIndex();
        String element = iterator.previous();
        System.out.println("Index=" + index + ",  Element=" + element);
    }
}

From source file:IteratorDemo.java

public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();

    al.add("C");/* ww w .  java2  s .  c  o m*/
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");

    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
        String element = litr.next();
        litr.set(element + "+");
    }

    itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    while (litr.hasPrevious()) {
        String element = litr.previous();
        System.out.print(element + " ");
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();

    al.add("C");/* ww w. j  av a  2 s.co m*/
    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 + " ");
    }
}

From source file:Main.java

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());
    }/* www . ja  v  a 2s.  c  o  m*/
    System.out.println("reverse direction");
    while (itr.hasPrevious()) {
        System.out.println(itr.previous());
    }
}

From source file:Employee.java

public static void main(String[] args) {
    String[] names = { "A", "B", "C", "D" };

    double[] salaries = { 2.0, 5.0, 6.0, 4.0 };

    List l = new ArrayList();

    for (int i = 0; i < names.length; i++)
        l.add(new Employee(names[i], salaries[i]));

    Collections.sort(l);/*from   w  w w.ja  va2 s.co m*/

    ListIterator liter = l.listIterator();

    while (liter.hasNext())
        System.out.println(liter.next());

    Collections.sort(l, new Employee.SalaryComparator());

    liter = l.listIterator();

    while (liter.hasNext())
        System.out.println(liter.next());
}

From source file:Main.java

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());
    }// ww  w.j  a v  a  2s.  c om

    System.out.println("in backward direction");
    while (itr.hasPrevious()) {
        System.out.println(itr.previous());
    }
}

From source file:LinkedListTest.java

public static void main(String[] args) {
    List<String> a = new LinkedList<String>();
    a.add("Amy");
    a.add("Carl");
    a.add("Erica");

    List<String> b = new LinkedList<String>();
    b.add("Bob");
    b.add("Doug");
    b.add("Frances");
    b.add("Gloria");

    // merge the words from b into a

    ListIterator<String> aIter = a.listIterator();
    Iterator<String> bIter = b.iterator();

    while (bIter.hasNext()) {
        if (aIter.hasNext())
            aIter.next();/*from   ww  w.  j  a  v a  2s .  c  o m*/
        aIter.add(bIter.next());
    }

    System.out.println(a);

    // remove every second word from b

    bIter = b.iterator();
    while (bIter.hasNext()) {
        bIter.next(); // skip one element
        if (bIter.hasNext()) {
            bIter.next(); // skip next element
            bIter.remove(); // remove that element
        }
    }

    System.out.println(b);

    // bulk operation: remove all words in b from a

    a.removeAll(b);

    System.out.println(a);
}