Example usage for java.util ArrayList listIterator

List of usage examples for java.util ArrayList listIterator

Introduction

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

Prototype

public ListIterator<E> listIterator() 

Source Link

Document

Returns a list iterator over the elements in this list (in proper sequence).

Usage

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");

    // Get an object of ListIterator using listIterator() method

    ListIterator listIterator = aList.listIterator();
    listIterator.next();/*  ww  w .  jav  a 2s.  c  o m*/
    listIterator.next();

    // remove element returned by last next method

    listIterator.remove();
    for (String str : aList) {
        System.out.println(str);
    }
}

From source file:IteratorDemo.java

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

    al.add("C");/*from w  w  w . j a  va  2  s .com*/
    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");//www .  jav  a2 s . c  o 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 removeFromList(ArrayList<Integer> e, int dato) {
    for (Iterator<Integer> iter = e.listIterator(); iter.hasNext();) {
        int a = iter.next();
        if (a == dato) {
            iter.remove();//ww  w .  j av a 2  s . com
        }
    }
}

From source file:Main.java

/** Removes useless path components.  */
private static String canonicalize(String path) {
    String pattern = File.separator;
    if (pattern.equals("\\"))
        pattern = "\\\\";

    String[] splitted = path.split(pattern);
    ArrayList<String> pathSpec = new ArrayList<String>(splitted.length);
    for (int i = 0; i < splitted.length; i++)
        pathSpec.add(splitted[i]);/*from w  ww. j a v a2s . com*/
    // Warning: these steps must be performed in this exact order!
    // Step 1: Remove empty paths
    for (ListIterator<String> it = pathSpec.listIterator(); it.hasNext();) {
        String c = it.next();
        if (c.length() == 0 && it.previousIndex() > 0)
            it.remove();
    }
    // Step 2: Remove all occurrences of "."
    for (ListIterator<String> it = pathSpec.listIterator(); it.hasNext();) {
        String c = it.next();
        if (c.equals("."))
            it.remove();
    }
    // Step 3: Remove all occurrences of "foo/.."
    for (ListIterator<String> it = pathSpec.listIterator(); it.hasNext();) {
        String c = it.next();
        if (c.equals("..") && it.previousIndex() > 0) {
            if (it.previousIndex() == 1 && pathSpec.get(0).length() == 0) {
                // "/.." is replaced by "/"
                it.remove();
            } else if (!pathSpec.get(it.previousIndex() - 1).equals("..")) {
                it.remove();
                it.previous();
                it.remove();
            }
        }
    }
    return listToString(pathSpec);
}

From source file:net.iponweb.hadoop.streaming.parquet.TextRecordWriterWrapper.java

TextRecordWriterWrapper(ParquetRecordWriter<SimpleGroup> w, FileSystem fs, JobConf conf, String name,
        Progressable progress) throws IOException {

    realWriter = w;//  ww w.  j a  v a  2 s . c o m
    schema = GroupWriteSupport.getSchema(conf);
    factory = new SimpleGroupFactory(schema);

    recorder = new ArrayList<>();
    ArrayList<String[]> Paths = (ArrayList<String[]>) schema.getPaths();
    Iterator<String[]> pi = Paths.listIterator();

    String[] prevPath = {};

    short grpDepth = 0;
    while (pi.hasNext()) {

        String p[] = pi.next();

        // Find longest common path between prev_path and current
        ArrayList<String> commonPath = new ArrayList<String>();
        for (int n = 0; n < prevPath.length; n++) {
            if (n < p.length && p[n].equals(prevPath[n])) {
                commonPath.add(p[n]);
            } else
                break;
        }

        // If current element is not inside previous group, restore to the group of common path
        for (int n = commonPath.size(); n < prevPath.length - 1; n++) {
            recorder.add(new PathAction(PathAction.ActionType.GROUPEND));
            grpDepth--;
        }

        // If current element is not right after common path, create all required groups
        for (int n = commonPath.size(); n < p.length - 1; n++) {
            PathAction a = new PathAction(PathAction.ActionType.GROUPSTART);
            a.setName(p[n]);
            recorder.add(a);
            grpDepth++;
        }

        prevPath = p;

        PathAction a = new PathAction(PathAction.ActionType.FIELD);

        Type colType = schema.getType(p);

        a.setType(colType.asPrimitiveType().getPrimitiveTypeName());
        a.setRepetition(colType.getRepetition());
        a.setName(p[p.length - 1]);

        recorder.add(a);
    }

    // Close trailing groups
    while (grpDepth-- > 0)
        recorder.add(new PathAction(PathAction.ActionType.GROUPEND));
}

From source file:au.com.dektech.dektalk.MainActivity.java

private boolean havePermissions(ArrayList<String> permissions) {
    boolean allgranted = true;
    ListIterator<String> it = permissions.listIterator();
    while (it.hasNext()) {
        if (ActivityCompat.checkSelfPermission(this, it.next()) != PackageManager.PERMISSION_GRANTED) {
            allgranted = false;//from   w  ww.ja v a 2 s  .c o  m
        } else {
            // permission granted, remove it from permissions
            it.remove();
        }
    }
    return allgranted;
}

From source file:vteaexploration.plottools.panels.XYChartPanel.java

private double getMinimumOfData(ArrayList alVolumes, int x) {

    ListIterator litr = alVolumes.listIterator();

    //ArrayList<Number> al = new ArrayList<Number>();
    Number low = getMaximumOfData(alVolumes, x);

    while (litr.hasNext()) {
        try {//from   w w w .  jav a2s. c  om
            MicroObjectModel volume = (MicroObjectModel) litr.next();
            Number Corrected = processPosition(x, volume);
            if (Corrected.floatValue() < low.floatValue()) {
                low = Corrected;
            }
        } catch (NullPointerException e) {
        }
    }
    return low.longValue();
}

From source file:vteaexploration.plottools.panels.XYChartPanel.java

private double getMaximumOfData(ArrayList alVolumes, int x) {

    ListIterator litr = alVolumes.listIterator();

    //ArrayList<Number> al = new ArrayList<Number>();
    Number high = 0;// w  ww.jav  a 2 s.  c om

    while (litr.hasNext()) {
        try {
            MicroObjectModel volume = (MicroObjectModel) litr.next();
            Number Corrected = processPosition(x, volume);
            if (Corrected.floatValue() > high.floatValue()) {
                high = Corrected;
            }
        } catch (NullPointerException e) {
        }
    }
    return high.longValue();

}

From source file:vteaexploration.plottools.panels.XYChartPanel.java

private double getRangeofData(ArrayList alVolumes, int x) {

    ListIterator litr = alVolumes.listIterator();

    ArrayList<Number> al = new ArrayList<Number>();

    Number low = 0;/*from  w w w . j  a v  a 2 s.c  o  m*/
    Number high = 0;
    Number test;

    while (litr.hasNext()) {
        try {
            MicroObjectModel volume = (MicroObjectModel) litr.next();
            Number Corrected = processPosition(x, volume);
            al.add(Corrected);

            if (Corrected.floatValue() < low.floatValue()) {
                low = Corrected;
            }
            if (Corrected.floatValue() > high.floatValue()) {
                high = Corrected;
            }
        } catch (NullPointerException e) {
        }
    }

    return high.longValue() - low.longValue();

}