FAQ Database Discussion Community


Removing an item from a list in Java

java,arrays,list,iterator,listiterator
I've scoured this site (as well as the web) and for some reason cannot find an answer that works. Either I get an index out of bounds error, or the next part of my code won't execute. All I am trying to do is remove a item from a list...

Adding items to a HashMap while looping with Iterator

java,iterator,hashmap,listiterator
I have a program that loops through a HashMap using an Iterator, and inside the loop, I'm adding to the HashMap - which is causing a ConcurrentModificationException. I've seen that ListIterator has an add() function that handles this, but Iterator does not. The HashMap is set up like this -...

UnsupportedOperationException when trying to remove a list of items from another list

java,listiterator
I have coded below, i dint know what is wrong but validDatesIterator.remove() is giving me UnsupportedOperationException exception. Using java 1.6 List<Integer> validDates = Arrays.asList(26,27,28,1,2,3,4); List<Integer> daysToBeRemoved = Arrays.asList(1,2); Iterator<Integer> validDatesIterator = validDates.listIterator(); while(validDatesIterator.hasNext()) { Integer curValue = validDatesIterator.next(); for(Integer dayToRemove:daysToBeRemoved) { if(curValue.equals(dayToRemove)) { validDatesIterator.remove(); break; } } }...

Iterator for an array list of Class

java,collections,iterator,listiterator
Am New to Java so please bear with me if my question seems to be silly. I am learning collections,I have written a program which will store student names id and marks.I am storing all these in a Arraylist. Using for loop I am able to print the values in...

Iterating twice over HashMap Java

java,iterator,hashmap,listiterator
I have a HashMap declared as static HashMap<String,ArrayList<Integer>> inverted_index = new HashMap<String,ArrayList<Integer>>(); I iterated over its keys as public static void printInvertedIndex() throws FileNotFoundException{ PrintWriter writer = new PrintWriter(new File("InvertedIndex.txt")); Iterator it = inverted_index.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); writer.println(pair.getKey() + " " + pair.getValue()); it.remove(); } writer.close();...

why java API prevents us to call add and remove together?

java,list,collections,listiterator
As per Java API- IllegalStateException - if neither next nor previous have been called, or remove or add have been called after the last call to next or previous remove()- Removes from the list the last element that was returned by next() or previous() (optional operation). This call can only...

Is linkedList.listIterator(linkedList.size()) optimized?

java,linked-list,reverse,listiterator
I'm trying to create a reverse ListIterator for a LinkedList, and was going to just implement it as a wrapper to linkedList.listIterator(linkedList.size()) that swaps the next and previous operations, but then realized that if LinkedList#listIterator(int) is implemented to just traverse forwards to the position specified, using it to start at...

Python Linked List with Nodes. Iterable

python,linked-list,listiterator
I need some help writing an __iter__() method for my UnorderedList() class. I tried this: def __iter__(self): current = self while current != None: yield current But the while loop doesn't stop. Here is the rest of my classes and code: class Node: def __init__(self,initdata): self.data = initdata self.next =...

How to conditionally remove an element from a list using an iterator?

c++,linked-list,listiterator
Problem: I am writing a simple file manager application. In this program I have a "Directory" class: class Directory { public: Directory(string address, string directoryname) { this->path = address; this->name = directoryname; } string GetFullPath(){ return path == "/" ? path + name : path + "/" + name; }...