listiterator

List with Comparable Vs TreeSet

前提是你 提交于 2019-12-04 23:37:58
问题 Option 1: Make a list which implements Comparable and sort it using collections.sort(List l) every time you add a value. Option 2: Make a TreeSet (which keeps itself sorted all the time). Which one will be faster? I am asking this because List gives me the option of ListIterator which I need in my case, since it lets me add an element while iterating. 回答1: The most important differences: Criterion | List with Collections.sort | TreeSet ----------------+----------------------------+---------

Java which element in the ListIterator is considered previous?

 ̄綄美尐妖づ 提交于 2019-12-04 13:00:52
I'm currently trying to learn how to implement my own ListIterators. I have most of it implemented and ready to go, except I'm confused by the previous() method. By standard convention, can I get an explanation of how previous() is usually interpreted. i.e.: >cursor< dog cat fish bird frog snake According to Oracles Java Platform 7 API: E previous() 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

How To Use ListIterator?

岁酱吖の 提交于 2019-12-04 05:35:31
问题 I was using iterator for ArrayList as : List<String> al = new ArrayList<>(); // ----- Logic for adding elements----- Iterator it = al.iterator(); // logic to retrieve elements---- Then it tried to work on ListIterator, like this . ListIterator li = al.listIterator(); while(li.hasNext()) { System.out.print(li.next()+" "); } It Worked ... I tried this for backward retrieval ListIterator li = al.listIterator(); while(li.hasPrevious()) { System.out.print(li.previous()+" "); } But its not working.

Trying to remove an object with list iterator

假装没事ソ 提交于 2019-12-03 17:34:22
I'm trying to remove an object from a list using the list iterator. I've gone through the other solutions on the website and none have alleviated the error "Exception in thread "main" java.util.ConcurrentModificationException" here is my code that is not executing : void PatronReturn(String bookName) { // get to beginning while(listIterator.hasPrevious()) { listIterator.previous(); } while(listIterator.hasNext()){ Book b = listIterator.next(); if (listIterator.next().getBookTitle().equals(bookName)) { //listIterator.next(); //listIterator.remove(); books.remove(b); //listIterator.next(); /

List with Comparable Vs TreeSet

人盡茶涼 提交于 2019-12-03 14:20:09
Option 1: Make a list which implements Comparable and sort it using collections.sort(List l) every time you add a value. Option 2: Make a TreeSet (which keeps itself sorted all the time). Which one will be faster? I am asking this because List gives me the option of ListIterator which I need in my case, since it lets me add an element while iterating. The most important differences: Criterion | List with Collections.sort | TreeSet ----------------+----------------------------+--------- cost of adding | O(n log n) | O(log n) equal sort keys | permitted | eliminated as duplicates iteration |

How To Use ListIterator?

女生的网名这么多〃 提交于 2019-12-02 09:45:13
I was using iterator for ArrayList as : List<String> al = new ArrayList<>(); // ----- Logic for adding elements----- Iterator it = al.iterator(); // logic to retrieve elements---- Then it tried to work on ListIterator, like this . ListIterator li = al.listIterator(); while(li.hasNext()) { System.out.print(li.next()+" "); } It Worked ... I tried this for backward retrieval ListIterator li = al.listIterator(); while(li.hasPrevious()) { System.out.print(li.previous()+" "); } But its not working. The below code is working. ListIterator<String> li = al.listIterator(al.size()); while(li.hasPrevious(

Why iterator.forEachRemaining doesnt remove element in the Consumer lambda?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 18:45:49
Lets have a look at this example: public class ListIteratorTest { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("element1"); list.add("element2"); list.add("element3"); list.add("element4"); ListIterator<String> iterator = list.listIterator(); } } And now, this works fine: // prints elements out, and then appropriately removes one after another while (iterator.hasNext()){ System.out.println(iterator.next()); iterator.remove(); } while this throws an IllegalStateException: // throws IllegalStateException, why? iterator.forEachRemaining(n -> { System

Why iterator.forEachRemaining doesnt remove element in the Consumer lambda?

江枫思渺然 提交于 2019-11-30 02:58:42
问题 Lets have a look at this example: public class ListIteratorTest { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("element1"); list.add("element2"); list.add("element3"); list.add("element4"); ListIterator<String> iterator = list.listIterator(); } } And now, this works fine: // prints elements out, and then appropriately removes one after another while (iterator.hasNext()){ System.out.println(iterator.next()); iterator.remove(); } while this throws an

How does an Iterator in Java know when to throw ConcurrentModification Exception

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 13:13:54
I have the following code which throws ConcurrentModificationException because I am using two different iterators on the same list and one of them is modifying the list. So, the second iterator throws the exception when reading the list because some other iterator has modified the list. List<Integer> list = new ArrayList<>(); populate(list);//A method that adds integers to list ListIterator<Integer> iterator1 = list.listIterator(); ListIterator<Integer> iterator2 = list.listIterator(); while (iterator1.hasNext()) { if(iterator1.next() < 5) iterator1.remove(); } while (iterator2.hasNext()){ if

Add elements to a List while iterating over it. (Java) [duplicate]

无人久伴 提交于 2019-11-27 04:45:20
Possible Duplicate: Java: adding elements to a collection during iteration My problem is that I want to expand a list with new elements while iterating over it and I want the iterator to continue with the elements that I just added. From my understanding the ListIterator.add() adds an element before the current element in the list, not after it. Is it possible to achieve this in some other way? You can't modify a Collection while iterating over it using an Iterator , except for Iterator.remove() . However, if you use the listIterator() method, which returns a ListIterator , and iterate over