Example usage for java.util ArrayList iterator

List of usage examples for java.util ArrayList iterator

Introduction

In this page you can find the example usage for java.util ArrayList iterator.

Prototype

public Iterator<E> iterator() 

Source Link

Document

Returns an iterator over the elements in this list in proper sequence.

Usage

From source file:Main.java

public static void main(String[] args) {
    ArrayList aList = new ArrayList();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("java2 s .com");
    System.out.println("ArrayList: ");
    System.out.println(aList);/*from w  w  w .j ava2  s.co m*/
    Iterator itr = aList.iterator();
    String strElement = "";
    while (itr.hasNext()) {
        strElement = (String) itr.next();
        if (strElement.equals("2")) {
            itr.remove();
            break;
        }
    }
    System.out.println("ArrayList after removal : ");
    System.out.println(aList);
}

From source file:Person.java

public static void main(String args[]) {
    ArrayList<Person> names = new ArrayList<Person>();
    names.add(new Person("E", "T"));
    names.add(new Person("A", "G"));
    names.add(new Person("B", "H"));
    names.add(new Person("C", "J"));

    Iterator iter1 = names.iterator();
    while (iter1.hasNext()) {
        System.out.println(iter1.next());
    }/*  www  . jav a  2s.c o m*/
    Collections.sort(names, new PersonComparator());
    Iterator iter2 = names.iterator();
    while (iter2.hasNext()) {
        System.out.println(iter2.next());
    }
}

From source file:Person.java

public static void main(String args[]) {
    ArrayList<Person> names = new ArrayList<Person>();
    names.add(new Person("E", "T"));
    names.add(new Person("A", "G"));
    names.add(new Person("B", "H"));
    names.add(new Person("C", "J"));

    Iterator iter1 = names.iterator();
    while (iter1.hasNext()) {
        System.out.println(iter1.next());
    }//  w  ww  .  ja va2 s  .  c om
    System.out.println("Before sorting");
    Collections.sort(names, new PersonComparator());
    Iterator iter2 = names.iterator();
    while (iter2.hasNext()) {
        System.out.println(iter2.next());
    }
}

From source file:Main.java

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

    list.add("A");
    list.add("B");
    list.add("C");
    list.add("C");
    list.add("C");
    list.add("C");
    list.add("C");
    list.add("C");

    for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
        String s = iter.next();/*from w ww . j  a  v  a  2  s.com*/
        if (s.equals("B")) {
            iter.remove();
        } else {
            System.out.println(s);
        }
    }

    for (String s : list) {
        System.out.println(s);
    }
}

From source file:MainClass.java

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

    al.add("C");//ww  w . j a v  a 2 s  . c om
    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) {
    ArrayList<String> aList = new ArrayList<String>();

    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");

    for (String str : aList) {
        System.out.println(str);//from  w w w  .j av  a2 s  .  c  om
    }
    Iterator itr = aList.iterator();

    // remove 2 from ArrayList using Iterator's remove method.

    String strElement = "";

    while (itr.hasNext()) {
        strElement = (String) itr.next();
        if (strElement.equals("2")) {
            itr.remove();
            break;
        }
    }
    for (String str : aList) {
        System.out.println(str);
    }
}

From source file:com.mycompany.asyncreq.Main.java

public static void main(String[] args) throws JsonProcessingException, JSONException, IOException {

    Configuration config = new Configuration();
    // Name tables with lowercase_underscore_separated
    RestTemplate restTemplate = new RestTemplate();
    config.setNamingStrategy(new ImprovedNamingStrategy());
    try {//from  ww  w  . j a va  2 s.c  om

        ArrayList<String> ArrReq = GenData();
        String addr;
        for (Iterator<String> i = ArrReq.iterator(); i.hasNext();) {
            try {
                addr = i.next();
                Thread.sleep(1000);
                Root tRoot = restTemplate.getForObject(addr, Root.class);
                tRoot.addr = addr;
                if (!tRoot.dataset.isEmpty())
                    if (tRoot.dataset.size() == tRoot.validation.count.value)
                        ObjToCsv(tRoot, "all");

                    else {
                        for (int reg = 1; reg < 5; reg++) {
                            Thread.sleep(1000);
                            addr = "http://comtrade.un.org/api/get?max=50000&type=C&freq=M&px=HS&ps=2014&r=804&p="
                                    + tRoot.dataset.get(0).getptCode() + "&rg=" + reg + "&cc=All&fmt=json";
                            Root tRootImp = restTemplate.getForObject(addr, Root.class);
                            tRootImp.addr = addr;
                            if (!tRootImp.dataset.isEmpty())
                                if (tRootImp.dataset.size() == tRootImp.validation.count.value)
                                    ObjToCsv(tRootImp, Integer.toString(reg));
                                else
                                    System.out.println("addr:  " + tRootImp.addr + "\n");
                        }
                    }
                else
                    System.out.println("addr:  " + tRoot.addr + ":null" + "\n");
            } catch (Exception e) {
                System.err.println(e.getMessage());

            }
        }

    } catch (Exception e) {
        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }
}

From source file:Main.java

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

    list.add("Monday");
    list.add("Tuesdag");
    list.add("Wednesday");
    list.add("Thursday");
    list.add("Friday");
    list.add("Saturday");
    list.add("Sunday");

    Iterator<String> iterator = null;

    iterator = list.iterator();
    while (iterator.hasNext()) {
        String element = iterator.next();
        System.out.println(element);
    }/*from  w  w w.j a v a2  s  .co  m*/

    for (iterator = list.iterator(); iterator.hasNext();) {
        String element = iterator.next();
        System.out.println(element);
    }

    for (String element : list) {
        System.out.println(element);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/* w ww .  j av  a  2s. c  o m*/
        BufferedReader input = new BufferedReader(new FileReader(args[0]));
        ArrayList list = new ArrayList();
        String line;
        while ((line = input.readLine()) != null) {
            list.add(line);
        }
        input.close();

        Collections.reverse(list);

        PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
        for (Iterator i = list.iterator(); i.hasNext();) {
            output.println((String) i.next());
        }
        output.close();
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:OldStyle.java

public static void main(String args[]) {
    ArrayList list = new ArrayList();

    // These lines store strings, but any type of object 
    // can be stored.  In old-style code, there is no  
    // convenient way restrict the type of objects stored 
    // in a collection 
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");

    Iterator itr = list.iterator();
    while (itr.hasNext()) {

        // To retrieve an element, an explicit type cast is needed 
        // because the collection stores only Object. 
        String str = (String) itr.next(); // explicit cast needed here. 

        System.out.println(str + " is " + str.length() + " chars long.");
    }//from  ww  w  .ja  v  a  2 s .co m
}