🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #3680

marking a postion in a Java Collection

#3680
From: Stino
Date: Fri, 07 Nov 2003 20:46
72 lines
3383 bytes
hi all,

I'm trying to do the following in Java.
I have a class that contains an ArrayList.
At certain points in the program I want to mark a position in the ArrayList.

e.g. If I had "A" and "B" then 'mark' and then add "C" and "D"
Then, when I say, 'showFrom()" I should only get to see "C" and "D".
I tried to use an Iterator to do this, but with no succes.
I get the following

Exception in thread "main" java.util.ConcurrentModificationException
         at
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:444)
         at java.util.AbstractList$Itr.next(AbstractList.java:417)
         at IteratorTest.moveIterator(IteratorTest.java:27)
         at IteratorTest.main(IteratorTest.java:41)

The code I tried is the following.

import java.util.*;

public class IteratorTest {
         private ArrayList a;
         private Iterator i;

         public IteratorTest() {
                 a = new ArrayList();
                 i = a.iterator();
         }

         public String toString() {
                 return a.toString();
         }

         public void add(Object o) {
                 a.add(o);
         }

         public void showAll() {
                 Iterator ii = a.iterator();
                 while (ii.hasNext())
                         System.out.print(ii.next() + " ");
         }
         // this should reset the mark
         public void moveIterator() {
                 while (i.hasNext()) i.next();
         }

         public void showFrom() {
                 Iterator ii = i;
                 while (ii.hasNext()) System.out.print(ii.next());
         }

         public static void main(String []args) {
              IteratorTest it = new IteratorTest();
              it.add("A");
              it.add("B");
              it.showAll();
              System.out.println("");
              it.moveIterator();
              it.add("C");
              it.add("D");
              it.showFrom(); // here, I hope to see only C and D
              System.out.println();
         }

}

Thanks for any suggestion on how to solve this problem.

Stino.
Technical Details
Message-ID: <mwTqb.9548$Qk3.429094@phobos.telenet-ops.be>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.arcor-online.net!feed1.news.be.easynet.net!ossa.telenet-ops.be!phobos.telenet-ops.be.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fcea4,3bfdd26d3dc7ca4f
X-Google-Attributes: gidfcea4,public
X-Google-Thread: 113ae9,3bfdd26d3dc7ca4f
X-Google-Attributes: gid113ae9,public
X-Google-ArrivalTime: 2003-11-07 12:46:11 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.arcor-online.net!feed1.news.be.easynet.net!ossa.telenet-ops.be!phobos.telenet-ops.be.POSTED!not-for-mail
From: Stino <hehe@hotmail.com>
Reply-To:  hehe@hotmail.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5b) Gecko/20030929 Thunderbird/0.2a
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: be.comp.programming,comp.lang.java.help
Subject: marking a postion in a Java Collection
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 74
Message-ID: <mwTqb.9548$Qk3.429094@phobos.telenet-ops.be>
Date: Fri, 07 Nov 2003 20:46:10 GMT
NNTP-Posting-Host: 81.164.20.247
X-Complaints-To: abuse@telenet.be
X-Trace: phobos.telenet-ops.be 1068237970 81.164.20.247 (Fri, 07 Nov 2003 21:46:10 MET)
NNTP-Posting-Date: Fri, 07 Nov 2003 21:46:10 MET
Organization: Telenet Internet