listiterator

ListIterator previous method not working

强颜欢笑 提交于 2019-12-23 08:33:21
问题 package wrap; import java.util.*; public class ArrayListDemo { public static void main(String [] args){ ArrayList<String> a=new ArrayList<String>(); a.add("B"); a.add("C"); a.add("D"); ListIterator<String> i=a.listIterator(); while(i.hasPrevious()){ System.out.println(i.previous()); } } } The program works fine for hasNext() and next() methods but for hasPrevious() and previous() it displays a message as below:: <terminated> ArrayListDemo [Java Application] C:\Program Files (x86)\Java\jre7

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

烈酒焚心 提交于 2019-12-17 20:42:33
问题 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

Why iterators are not a solution for CuncurentModificationException?

这一生的挚爱 提交于 2019-12-13 09:55:50
问题 I wanted to avoid syncing so I tried to use iterators. The only place where I modify array looks as follows: if (lastSegment.trackpoints.size() > maxPoints) { ListIterator<TrackPoint> points = lastSegment.trackpoints.listIterator(); points.next(); points.remove(); } ListIterator<TrackPoint> points = lastSegment.trackpoints.listIterator(lastSegment.trackpoints.size()); points.add(lastTrackPoint); And array traversal looks as follows: for (Iterator<Track.TrackSegment> segments = track

Iteration over a list in Java

☆樱花仙子☆ 提交于 2019-12-11 04:47:16
问题 The problem is as follows: Write a static method subsets that uses recursive backtracking to find every possible sub-list of a given list. A sub-list of a list L contains 0 or more of L's elements. Your method should accept a List of strings as its parameter and print every sub-list that could be created from elements of that list, one per line. For example, suppose a variable called list stores the following elements: [Janet, Robert, Morgan, Char] The call of subsets(list); would produce

How to remove odd positions from a list?

偶尔善良 提交于 2019-12-11 02:03:45
问题 #include<iostream> #include<list> using namespace std; void compute(int num) { list<int> L; list<int>::iterator i; list<int>::iterator i2; int p; cout<<"Enter the number of numbers\n"; cin>>p; int a; for(int k=1;k<=p;k++) { cin>>a; L.push_back(k); } cout<<endl; for(i=L.begin() ; i!=L.end() ; ++i) { cout<<*i<<endl; } long int k=1; for(i=L.begin() ; i!=L.end() ; ++i ) { if(k%2!=0) //This is where I try and delete values in odd positions { i2=L.erase(i); } k++; } for(i=L.begin() ; i!=L.end() ; +

Correct implementation of List Iterator methods

前提是你 提交于 2019-12-10 23:16:44
问题 To better learn about Iterators, I am to write them myself to try and get the correct functionality of them. I'm having an issue getting the correct behavior out of the ListIterator previous method. For example, the JavaDoc states that: Alternating calls to next and previous will return the same element repeatedly. My Iterator class Node<Item> { public Item data; public Node<Item> next; public Node<Item> previous; public Node() { data = null; next = null; previous = null; } public Node(Item i

Index of minimum element in a std::list

怎甘沉沦 提交于 2019-12-09 16:41:22
问题 If I have a std::vector<int> , I can obtain the index of the minimum element by subtracting two iterators: int min_index = std::min_element(vec.begin(), vec.end()) - vec.begin(); However, with containers that don't have random access iterators, for example a std::list<int> , this doesn't work. Sure, it is possible to do something like int min_index = std::difference(l.begin(), std::min_element(l.begin(), l.end())); but then I have to iterate twice through the list. Can I get the index of the

Adding items to a HashMap while looping with Iterator

佐手、 提交于 2019-12-08 03:34:56
问题 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 - HashMap<Pair<Integer, Integer>, Object> And the iterator like this - Iterator<Entry<Pair<Integer, Integer>, Object>> iter; With Object (not the real name) being a class from my program. Does anybody know

Java which element in the ListIterator is considered previous?

一笑奈何 提交于 2019-12-06 06:51:42
问题 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

Trying to remove an object with list iterator

廉价感情. 提交于 2019-12-05 04:35:11
问题 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