🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: be.comp.programming
3 messages
3 total messages Started by Stino Fri, 07 Nov 2003 20:45
marking a postion in a Java Collection
#3679
Author: Stino
Date: Fri, 07 Nov 2003 20:45
72 lines
3403 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.
Re: marking a postion in a Java Collection
#3681
Author: Tim Van Wassenhove
Date: Fri, 07 Nov 2003 20:55
31 lines
1853 bytes
On 2003-11-07, Stino <hehe@hotmail.com> wrote:
> 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)
>
>          public void showFrom() {
>                  Iterator ii = i;
>                  while (ii.hasNext()) System.out.print(ii.next());
>          }
>

Een gokje, ArrayList is niet thread-safe?


-- 
Visit my website at http://home.scarlet.be/~vwadsl
My jabber handle is aardbei@jabber.org
Re: marking a postion in a Java Collection
#3682
Author: Gerrit Cornelis
Date: Sun, 09 Nov 2003 16:46
111 lines
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.
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads