Article View: be.comp.programming
Article #3682Re: marking a postion in a Java Collection
From: Gerrit Cornelis
Date: Sun, 09 Nov 2003 16:46
Date: Sun, 09 Nov 2003 16:46
111 lines
4944 bytes
4944 bytes
"Stino" <hehe@hotmail.com> wrote in message
news:ZvTqb.9547$uj1.222276@phobos.telenet-ops.be...
> 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
I think an iterator is a bad implementation choice (iterators prefer the
underlying list to remain static while they are iterating).
Furthermore your instance variable naming is poor, write your code so it is
easy and clear to read (i and ii as naming requires quite some memory work
from the reader to understand what object you are modifying).
Your method naming is poor, you are talking about a mark but the designated
method is called moveIterator. This methodname is a) saying how the object
is implemented b) not stating what it will do, I want to set a mark in this
object...not move an iterator (obviously you came to the same conclusion and
added a comment that it will set a mark). comments are bad : name the method
setMark().
Btw, this is the first testclass I have ever seen that asserts nothing.
proposed changes : (don't shoot me if the code doesn't compile, I never do
Java, its too complicated for me)
now, lets have a method setMark() and an additional inst var named
markPosition initially assigned -1.
setMark () {
markPosition = a.size();
}
showFrom() { //a better name is showfromMark()
Iterator myIterator = a.iterator();
int idx = 0;
while (myIterator.hasNext()) {
if(idx > markPosition) { myIterator
System.out.print(myIterator.next())};
}
>
> 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:
<3fae6f76$0$4015$ba620e4c@reader0.news.skynet.be>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!eusc.inter.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!skynet.be!skynet.be!louie!tlk!not-for-mailReferences:
<ZvTqb.9547$uj1.222276@phobos.telenet-ops.be>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-ArrivalTime: 2003-11-09 08:46:47 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!eusc.inter.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!skynet.be!skynet.be!louie!tlk!not-for-mail Reply-To: "Gerrit Cornelis" <gerrit_*remove*cornelis@belgacom.net> From: "Gerrit Cornelis" <gerrit_*remove*cornelis@belgacom.net> Newsgroups: be.comp.programming References: <ZvTqb.9547$uj1.222276@phobos.telenet-ops.be> Subject: Re: marking a postion in a Java Collection Date: Sun, 9 Nov 2003 17:46:39 +0100 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Lines: 113 Message-ID: <3fae6f76$0$4015$ba620e4c@reader0.news.skynet.be> Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 194.78.136.107 X-Trace: 1068396406 reader0.news.skynet.be 4015 194.78.136.107:28422 X-Complaints-To: usenet-abuse@skynet.be