Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
In Python, the enumerate function allows you to iterate over a sequence of (index, value) pairs. For example: >>> numbers = ["zero", "one", "two"] >>> for i, s in enumerate(numbers): ... print i, s ... 0 zero 1 one 2 two Is there any way of doing this in Java?
I want to write a method that removes all elements from a collection that follow a certain pattern. In functional languages, I would use filter() with a lambda expression. However, in Java, it seems I'm stuck with this: public void removeAllBlueCars() { LinkedList<Car> carsToRemove = new LinkedList<Car>(); for (Car c : cars) { if (c.getCarColor() == Color.BLUE) { ...
Let's say I have a List object and an iterator for that list. Now I sort the list with java.util.Collections.sort() What happens to the iterator? Is its behavior still defined and can it still be used? If not, can I prevent destroying the iterators for the list? I know, this problem could be circumvented by changing the program design, cloning the list for example, but I specificly want ...
Hmmm... the Java Iterator<T> has a remove() method but not a replace(T replacement) method. Is there an efficient way to replace selected items in a List? I can use a for-loop to call get(i) and set(i) which is fine for ArrayList, but would suck for a linked list.
In a game I have a list of players, let's say like this: LinkedList<String> players = new LinkedList<String>(); I want to let each player interact with each of the other players, so I write two nested loops: Iterator<String> i1 = players.iterator(); while (i1.hasNext()) { String p1 = i1.next(); Iterator<String> i2 = players.iterator(); // But I want do th...
is there anything similar to .Net's LinkedListNode<(Of <(T>)>)..::.Next and LinkedListNode<(Of <(T>)>)..::.Previous properties in Java's java.util.LinkedList.
In C+ one can use iterators for writing to a sequence. Simplest example would be: vector<int> v; for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) { *it = 42; } I need something more complicated - keep iterator as a class member for a later use. But I don't know how to get this behavior from Java iterators. Are there writable iterators in Java at all? If not then...
Why Methode LinkedList.contains() runs quickly than such implementation: for (String s : list) if (s.equals(element)) return true; return false; I don't see great difference between this to implementations(i consider that search objects aren't nulls), same iterator and equals operation
I am having an issue removing elements of a list while iterating through the list. Code: For (WebElement element: list){ if (!element.isEnabled() || !element.isSelected()){ list.remove(element); } } I get a ConcurrentModificationException, which I totally understand. I am removing an item from a list while in the loop that goes through the list. Intuitively, that would screw ...
I am using an Iterator to iterate through collection and I want to get the current loop index. any ideas how to do that ?
Insertion or deletion of an element at a specific point of a list, assuming that we have a pointer to the node already, is a constant-time operation. - from the Wikipedia Article on Linked list Linked list traversal in a single linked list always starts from the head. We have to keep going till we satisfy a given condition. So that will make any operation worst case O(n) unless we are d...
Hi i am getting List object that contains pojo class objects of the table. in my case i have to show the table data in reverse order. mean that, for ex i am adding some rows to particular table in database when i am added recently, the data is storing at last row in table(in database). here i have to show whole content of the table in my jsp page in reverse order, mean that what i inserted rece...
This is my code to construct a possible tour of citys in a Locale l (it's not optimal it's just to give my AI search a head start). I'm getting a ConcurrentModificationException, which to my knowledge happens when more than one piece of code accesses an variable / collection and attempts to modify it. Causing this code to get unhappy: final void checkForComodification() { if (modCount != ...
I have a custom, generic, singly LinkedList which I built myself. I can add, remove etc to the List just fine. I'd like to implement the Java ListIterator to my class. How would I go about starting this? What methods do I need to add to my class? All I can find on the web is examples of using the ListIterator on the default Java LinkedList which is no good to me. Thanks!
I use Java. I already have a class for a custom object called "Subject". I have another class that only contains a Linked List of Subject objects. (called subjectsList) I wrote one of the methods (called "getSubject()") in the subjectsList class that is meant to return the Subject object at a specific index in the Linked List. To do this, I used a ListIterator. I would create a ListIterator ...
public ReversibleIterator iterator(); can anyone help me make this method? ill put up what i have done so far The ReversibleIterator should behave as follows. The first call to next or previous should return the first or last element of the list, respectively. Subsequent calls to next/previous should return the element that is next/previous with respect to the antecedent call to next/previou...
I have a list of elements containing a few particular special elements and I need to find the neighbors of these elements in constant time. This sounds easy using a doubly linked list: simply store references to the nodes containing these particular elements and check their previous and next nodes. (I also prefer using a linked list since I constantly remove and add elements. The list is large ...
Is there a way to get an element id of a list to get it later through list.get(index) when using for(Object obj: o) construction Only define a local var and manually incrementing it? Anything simpler?
If I modify a Collection while iterating over it using for-each loop, it gives ConcurrentModificationException. Is there any workaround?
I'm completely new to iterators. I have an ArrayList named "Test" with String objects. How would I go about using an iterator class? I've tried everything I can think of, it's just not making sense. Thanks for the help. Say I have an Iterator named "iter". I need to step through my ArrayList in search of a certain String. When that String is found, I need to add it to a different ArrayList nam...
I'm a student and fairly new to Java. I was looking over the different speeds achieved by the two collections in Java, Linked List, and ArrayList. I know that an ArrayList is much much faster at looking up and placing in values into its indexes. My question is: how can one make a linked list faster, if at all possible? Thanks for any help. zmahir
  /*
   * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.util;

An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). An iterator for a list of length n has n+1 possible cursor positions, as illustrated by the carets (^) below:
                      Element(0)   Element(1)   Element(2)   ... Element(n-1)
 cursor positions:  ^            ^            ^            ^                  ^
 
Note that the remove() and set(java.lang.Object) methods are not defined in terms of the cursor position; they are defined to operate on the last element returned by a call to next() or previous().

This interface is a member of the Java Collections Framework.

Author(s):
Josh Bloch
Since:
1.2
See also:
Collection
List
Iterator
Enumeration
List.listIterator()
 
 public interface ListIterator<E> extends Iterator<E> {
     // Query Operations
 
    
Returns true if this list iterator has more elements when traversing the list in the forward direction. (In other words, returns true if next() would return an element rather than throwing an exception.)

Returns:
true if the list iterator has more elements when traversing the list in the forward direction
 
     boolean hasNext();

    
Returns the next element in the list and advances the cursor position. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous() to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Returns:
the next element in the list
Throws:
NoSuchElementException if the iteration has no next element
 
     E next();

    
Returns true if this list iterator has more elements when traversing the list in the reverse direction. (In other words, returns true if previous() would return an element rather than throwing an exception.)

Returns:
true if the list iterator has more elements when traversing the list in the reverse direction
 
     boolean hasPrevious();

    
Returns the previous element in the list and moves the cursor position backwards. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next() to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Returns:
the previous element in the list
Throws:
NoSuchElementException if the iteration has no previous element
    E previous();

    
Returns the index of the element that would be returned by a subsequent call to next(). (Returns list size if the list iterator is at the end of the list.)

Returns:
the index of the element that would be returned by a subsequent call to next, or list size if the list iterator is at the end of the list
    int nextIndex();

    
Returns the index of the element that would be returned by a subsequent call to previous(). (Returns -1 if the list iterator is at the beginning of the list.)

Returns:
the index of the element that would be returned by a subsequent call to previous, or -1 if the list iterator is at the beginning of the list
    int previousIndex();
    // Modification Operations

    
Removes from the list the last element that was returned by next() or previous() (optional operation). This call can only be made once per call to next or previous. It can be made only if add(java.lang.Object) has not been called after the last call to next or previous.

Throws:
java.lang.UnsupportedOperationException if the remove operation is not supported by this list iterator
java.lang.IllegalStateException if neither next nor previous have been called, or remove or add have been called after the last call to next or previous
    void remove();

    
Replaces the last element returned by next() or previous() with the specified element (optional operation). This call can be made only if neither remove() nor add(java.lang.Object) have been called after the last call to next or previous.

Parameters:
e the element with which to replace the last element returned by next or previous
Throws:
java.lang.UnsupportedOperationException if the set operation is not supported by this list iterator
java.lang.ClassCastException if the class of the specified element prevents it from being added to this list
java.lang.IllegalArgumentException if some aspect of the specified element prevents it from being added to this list
java.lang.IllegalStateException if neither next nor previous have been called, or remove or add have been called after the last call to next or previous
    void set(E e);

    
Inserts the specified element into the list (optional operation). The element is inserted immediately before the next element that would be returned by next(), if any, and after the next element that would be returned by previous(), if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)

Parameters:
e the element to insert
Throws:
java.lang.UnsupportedOperationException if the add method is not supported by this list iterator
java.lang.ClassCastException if the class of the specified element prevents it from being added to this list
java.lang.IllegalArgumentException if some aspect of this element prevents it from being added to this list
    void add(E e);
New to GrepCode? Check out our FAQ X