3 Replies Latest reply: Nov 27, 2007 5:50 AM by mlk RSS

    Problem with Iterator<?>

    807603
      hello

      plz check out the following code
      public class MyClass {
      
      public Iterator<?> doSomething(Iterator<?> input) {
      
        //Iterator<?> it = input;
        int count = 0;
        while (input.hasNext()) {
          input.next();
          if (count == 0)
            input.remove();
          ++count ;
          return input;
        }
      }
      
      public class TestMyClass {
      
      public void test() {
        Collection<String> a = new Vector<String>();
           a.add(" 1");
           a.add(" 2");
           a.add(" 3");
           a.add(" 4");
           a.add(" 5");
           Iterator<?> it = a.iterator();
                
           printCollection(a);
           printCollection2(it);
                
           it = myClass.filter(it);
                
           printCollection(experts);
           printCollection2(it);
        }
      
      
           private void printCollection(Collection<?> col) {
                System.out.println("\nprint collection\n");
                for (Object e : col) {
                     System.out.println(e);
                }
                System.out.println("\n\n");
           }
           
           private void printCollection2(Iterator<?> iterator) {
                System.out.println("\nprint collection2 with iterator\n");
                while (iterator.hasNext()) {
                     System.out.println(iterator.next());
                }
                System.out.println("\n\n");
           }
      }
      i create the objects, everything is fine. but when i want to read out the contents of the iterator after calling doSomething in MyClass nothing is printed.

      isn't it possible to alter iterator in another class and give it back to caller?

      i havn't used generics before, please tell me what I do wrong.

      cheers
      david