Example usage for java.util LinkedList listIterator

List of usage examples for java.util LinkedList listIterator

Introduction

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

Prototype

public ListIterator<E> listIterator(int index) 

Source Link

Document

Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

Usage

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // set Iterator at specified index
    Iterator x = list.listIterator(1);

    // print list with the iterator
    while (x.hasNext()) {
        System.out.println(x.next());
    }/*from w  w w  .j a v a  2  s.c  om*/
}

From source file:Employee.java

public static void main(String args[]) {
    LinkedList<Employee> phonelist = new LinkedList<Employee>();

    phonelist.add(new Employee("A", "1"));
    phonelist.add(new Employee("B", "2"));
    phonelist.add(new Employee("C", "3"));

    Iterator<Employee> itr = phonelist.iterator();

    Employee pe;//from w w w .jav a  2  s  .  co m
    while (itr.hasNext()) {
        pe = itr.next();
        System.out.println(pe.name + ": " + pe.number);
    }
    ListIterator<Employee> litr = phonelist.listIterator(phonelist.size());

    while (litr.hasPrevious()) {
        pe = litr.previous();
        System.out.println(pe.name + ": " + pe.number);
    }
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Get a valid source component from the component path.  This is similar to
 * {@link ApplicationRegistry#getValidSourceComponent(Component)} but is able to find the source component even
 * when a pesky dialog is in the in way as long as the dialog had {@link #recordDialogsAncestorComponent(Dialog, Component)}
 * call on it./*from   www . jav a 2  s.  c om*/
 *
 * @param component the component to start the search on
 *
 * @return a valid source component that has an ApplicationFunction annotation.
 *
 * @throws IllegalArgumentException if a valid source component could not be found.
 */
@Nonnull
public static Component getValidSourceComponentAcrossDialogs(Component component) {
    LinkedList<Component> path = new LinkedList<>();
    do {
        path.add(component);

        if (component.getClientProperty(CLIENT_PROP_DIALOGS_ANCESTRY) instanceof Component)
            component = (Component) component.getClientProperty(CLIENT_PROP_DIALOGS_ANCESTRY);
        else
            component = component.getParent();

    } while (component != null);

    final ListIterator<Component> listIterator = path.listIterator(path.size());
    while (listIterator.hasPrevious()) {
        final Component previous = listIterator.previous();
        if (previous.getClass().isAnnotationPresent(ApplicationFunction.class))
            return previous;
    }
    throw new IllegalArgumentException("Component path does not contain an application function.");
}

From source file:au.edu.ausstage.networks.SearchManager.java

/**
 * A method to take a group of collaborators and output JSON encoded text
 * Unchecked warnings are suppressed due to internal issues with the org.json.simple package
 *
 * @param collaborators the list of collaborators
 * @return              the JSON encoded string
 *///ww  w. j  a  va 2s .c  o m
@SuppressWarnings("unchecked")
private String createJSONOutput(LinkedList<Collaborator> collaborators) {

    // assume that all sorting and ordering has already been carried out
    // loop through the list of collaborators and add them to the new JSON objects
    ListIterator iterator = collaborators.listIterator(0);

    // declare helper variables
    JSONArray list = new JSONArray();
    Collaborator collaborator = null;

    while (iterator.hasNext()) {

        // get the collaborator
        collaborator = (Collaborator) iterator.next();

        // add the Json object to the array
        list.add(collaborator.toJsonObject());
    }

    // return the JSON encoded string
    return list.toString();

}

From source file:edu.umd.cfar.lamp.viper.geometry.BoundingBox.java

/**
 * Gets a set of boxes which covers all and only the pixels covered by
 * <code>A</code> and <code>B</code>.
 * //  w  w  w.  ja va2s  .  co  m
 * @param A
 *            a set of boxes to union with
 * @param B
 *            a set of boxes to union with
 * @return a set of boxes corresponding to the region shared by A and B
 */
public static BoundingBox union(BoundingBox A, BoundingBox B) {
    BoundingBox temp = new BoundingBox();
    LinkedList aList;
    temp.composed = true;
    int x = Math.min(A.rect.x, B.rect.x);
    int y = Math.min(A.rect.y, B.rect.y);
    int x2 = Math.max(A.rect.x + A.rect.width, B.rect.x + B.rect.width);
    int y2 = Math.max(A.rect.y + A.rect.height, B.rect.y + B.rect.height);

    temp.rect = new Rectangle(x, y, x2, y2);

    if (A.composed)
        aList = (LinkedList) A.pieces.clone();
    else {
        aList = new LinkedList();
        aList.add(A);
    }

    if (B.composed)
        temp = B.copy();
    else {
        temp.pieces = new LinkedList();
        temp.pieces.add(B.copy());
        temp.composed = true;
    }

    ListIterator iter = aList.listIterator(0);
    while (iter.hasNext()) {
        BoundingBox child = (BoundingBox) iter.next();
        Iterator ti = temp.pieces.iterator();
        LinkedList childRemains = null;

        /* remove an offending piece of the child */
        while (ti.hasNext() && (null == (childRemains = ((BoundingBox) ti.next()).subtractFrom(child))))
            ;

        /*
         * Add the broken pieces into the list and break back to top loop
         * remove the offending rectangle and replace it with its shards,
         * then clean up those.
         */
        if (childRemains != null) {
            ti = childRemains.iterator();
            iter.remove();
            while (ti.hasNext()) {
                iter.add(ti.next());
                iter.previous();
            }
        }
    }
    temp.pieces.addAll(aList);
    temp.simplify();
    return (temp);
}

From source file:org.mycard.net.network.RequestQueue.java

/***
 * debug tool: prints request queue to log
 *//*  w w  w. j a va  2s. co  m*/
synchronized void dump() {
    //HttpLog.v("dump()");
    StringBuilder dump = new StringBuilder();
    int count = 0;
    Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter;

    // mActivePool.log(dump);

    if (!mPending.isEmpty()) {
        iter = mPending.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next();
            String hostName = entry.getKey().getHostName();
            StringBuilder line = new StringBuilder("p" + count++ + " " + hostName + " ");

            LinkedList<Request> reqList = entry.getValue();
            @SuppressWarnings({ "rawtypes", "unused" })
            ListIterator reqIter = reqList.listIterator(0);
            while (iter.hasNext()) {
                Request request = (Request) iter.next();
                line.append(request + " ");
            }
            dump.append(line);
            dump.append("\n");
        }
    }
    // HttpLog.v(dump.toString());
}

From source file:au.edu.ausstage.networks.LookupManager.java

/**
 * A method to take a group of collaborators and output XML encoded text
 *
 * @param collaborators the list of collaborators
 * @return              the HTML encoded string
 *///from  www  . j  a va  2 s .c  o m
private String createXMLOutput(java.util.LinkedList<Collaborator> collaborators) {

    // assume that all sorting and ordering has already been carried out
    // loop through the list of collaborators build the XML
    ListIterator iterator = collaborators.listIterator(0);

    // declare helper variables
    StringBuilder xmlMarkup = new StringBuilder("<?xml version=\"1.0\"?><collaborators>");
    Collaborator collaborator = null;
    int count = 0;

    while (iterator.hasNext()) {

        // get the collaborator
        collaborator = (Collaborator) iterator.next();

        xmlMarkup.append("<collaborator id=\"" + collaborator.getId() + "\">");

        xmlMarkup.append("<url>" + StringEscapeUtils.escapeXml(collaborator.getUrl()) + "</url>");
        xmlMarkup.append("<givenName>" + collaborator.getGivenName() + "</givenName>");
        xmlMarkup.append("<familyName>" + collaborator.getFamilyName() + "</familyName>");
        xmlMarkup.append("<name>" + collaborator.getName() + "</name>");
        xmlMarkup.append("<function>" + collaborator.getFunction() + "</function>");
        xmlMarkup.append("<firstDate>" + collaborator.getFirstDate() + "</firstDate>");
        xmlMarkup.append("<lastDate>" + collaborator.getLastDate() + "</lastDate>");
        xmlMarkup.append("<collaborations>" + collaborator.getCollaborations() + "</collaborations>");
        xmlMarkup.append("</collaborator>");

        // increment the count
        count++;
    }

    // add a comment
    xmlMarkup.append("<!-- Contributors listed: " + count + " -->");

    // end the table
    xmlMarkup.append("</collaborators>");

    return xmlMarkup.toString();

}

From source file:au.edu.ausstage.networks.LookupManager.java

/**
 * A method to take a group of collaborators and output JSON encoded text
 * Unchecked warnings are suppressed due to internal issues with the org.json.simple package
 *
 * @param collaborators the list of collaborators
 * @return              the JSON encoded string
 *///from w w  w.java  2 s  .c  o m
@SuppressWarnings("unchecked")
private String createJSONOutput(LinkedList<Collaborator> collaborators) {

    // assume that all sorting and ordering has already been carried out
    // loop through the list of collaborators and add them to the new JSON objects
    ListIterator iterator = collaborators.listIterator(0);

    // declare helper variables
    JSONArray list = new JSONArray();
    JSONObject object = null;
    Collaborator collaborator = null;

    while (iterator.hasNext()) {

        // get the collaborator
        collaborator = (Collaborator) iterator.next();

        // start a new JSON object
        object = new JSONObject();

        // build the object
        object.put("id", collaborator.getId());
        object.put("url", collaborator.getUrl());
        object.put("givenName", collaborator.getGivenName());
        object.put("familyName", collaborator.getFamilyName());
        object.put("name", collaborator.getName());
        object.put("function", collaborator.getFunction());
        object.put("firstDate", collaborator.getFirstDate());
        object.put("lastDate", collaborator.getLastDate());
        object.put("collaborations", new Integer(collaborator.getCollaborations()));

        // add the new object to the array
        list.add(object);
    }

    // return the JSON encoded string
    return list.toString();

}

From source file:au.edu.ausstage.networks.LookupManager.java

/**
 * A method to take a group of collaborators and output HTML encoded text
 *
 * @param collaborators the list of collaborators
 * @return              the HTML encoded string
 *//*w  w  w .j ava  2  s  .  com*/
private String createHTMLOutput(LinkedList<Collaborator> collaborators) {

    // assume that all sorting and ordering has already been carried out
    // loop through the list of collaborators and build the HTML
    ListIterator iterator = collaborators.listIterator(0);

    // declare helper variables
    StringBuilder htmlMarkup = new StringBuilder("<table id=\"key-collaborators\">");
    String[] functions = null;
    String[] tmp = null;
    String firstDate = null;
    String lastDate = null;
    Collaborator collaborator = null;
    int count = 0;

    // add the header and footer
    htmlMarkup.append("<thead><tr><th>Name</th><th>Period</th><th>Function(s)</th><th>Count</th></tr></thead>");
    htmlMarkup.append("<tfoot><tr><td>Name</td><td>Period</td><td>Function(s)</td><td>Count</td></tr></tfoot>");

    while (iterator.hasNext()) {

        // get the collaborator
        collaborator = (Collaborator) iterator.next();

        // start the row
        htmlMarkup.append("<tr id=\"key-collaborator-" + collaborator.getId() + "\">");

        // add the cell with the link and name
        htmlMarkup.append("<th scop=\"row\"><a href=\"" + StringEscapeUtils.escapeHtml(collaborator.getUrl())
                + "\" title=\"View " + collaborator.getName() + " record in AusStage\">");
        htmlMarkup.append(collaborator.getName() + "</a></th>");

        // build the dates
        tmp = DateUtils.getExplodedDate(collaborator.getFirstDate(), "-");
        firstDate = DateUtils.buildDisplayDate(tmp[0], tmp[1], tmp[2]);

        tmp = DateUtils.getExplodedDate(collaborator.getLastDate(), "-");
        lastDate = DateUtils.buildDisplayDate(tmp[0], tmp[1], tmp[2]);

        // add the cell with the collaboration period
        htmlMarkup.append("<td>" + firstDate + " - " + lastDate + "</td>");

        // add the functions
        htmlMarkup.append("<td>" + collaborator.getFunction().replaceAll("\\|", "<br/>") + "</td>");

        // add the count
        htmlMarkup.append("<td>" + collaborator.getCollaborations() + "</td>");

        // end the row
        htmlMarkup.append("</tr>");

        // increment the count
        count++;
    }

    // end the table
    htmlMarkup.append("</table>");

    // add a comment
    htmlMarkup.append("<!-- Contributors listed: " + count + " -->");

    return htmlMarkup.toString();

}

From source file:com.xmobileapp.rockplayer.LastFmEventImporter.java

/**********************************************
 * /*from   www .  j  a v  a2s.  co m*/
 * insertListInListByDate
 * @param singleArtistEventList
 * 
 **********************************************/
public void insertListInListByDate(LinkedList<ArtistEvent> singleArtistEventList) {
    /*
     * If this artist List is empty just return
     */
    if (singleArtistEventList.isEmpty())
        return;

    /*
     * If the big list is empty just add this one to it
     */
    if (artistEventList.isEmpty()) {
        artistEventList.addAll(singleArtistEventList);
        return;
    }

    /*
     * Insert the items (normal case)
     */
    ListIterator<ArtistEvent> artistEventListIterator = artistEventList.listIterator(0);
    ListIterator<ArtistEvent> singleArtistEventListIterator = singleArtistEventList.listIterator(0);
    ArtistEvent artistEvent;
    ArtistEvent singleArtistEvent;
    while (singleArtistEventListIterator.hasNext()) {
        /*
         * Not yet at the end of the big list
         */
        if (artistEventListIterator.hasNext()) {
            singleArtistEvent = singleArtistEventListIterator.next();
            artistEvent = artistEventListIterator.next();
            while (singleArtistEvent.dateInMillis > artistEvent.dateInMillis) {
                if (artistEventListIterator.hasNext())
                    artistEvent = artistEventListIterator.next();
                else {
                    if (artistEventListIterator.previousIndex() >= MAX_EVENT_LIST_SIZE) {
                        return;
                    } else {
                        break; // TODO: add missing items to the big list
                    }
                }
            }
            artistEventListIterator.previous();
            artistEventListIterator.add(singleArtistEvent);
        }
        /*
         * At the end of the big list (but not of the 'small' list
         */
        else {
            if (artistEventListIterator.previousIndex() >= MAX_EVENT_LIST_SIZE)
                return;
            singleArtistEvent = singleArtistEventListIterator.next();
            artistEventListIterator.add(singleArtistEvent);
            artistEventListIterator.next();
        }
    }

    /*
     * Keep the list size in check
     */
    while (artistEventList.size() > MAX_EVENT_LIST_SIZE)
        artistEventList.removeLast();
}