2

I have a stripped down version of Java 1.4.2 that does not have the Iterator class. I am trying to port the T2Framework source to be able to run on this target, however, I have run into the following line of code:

for (Class D : domainMap.keySet())

where domainMap is of the type java.util.HashMap. For every other Iterator in this source I've encountered so far, I have been able to just use a for loop with an index to resolve the issue of not being able to use an Iterator, however, a Set in Java does not allow you to reference its data by index. Is there another way to access the data in a Set?

codewario
  • 19,553
  • 20
  • 90
  • 159

5 Answers5

4

You can toArray it, and then use a for loop over that array:

Object[] array = domainMap.keySet().toArray();
for (int i = 0; i < array.length; i++) {
    Object o = domainMap.get(array[i]);

    // Body of loop here
}
dlev
  • 48,024
  • 5
  • 125
  • 132
4

What you don't have is the "enhanced for loop" that uses Iterable, Iterator is in Java since v1.2

You can replace every occurrence of

for( Object o : collection ) { 
}

With

for( Iterator i = collection.iterator(); i.hasNext() ; ) {
   Object o = i.next(); 
}

The former is just syntactic sugar of the later ( see the compiled code ).

See:

C:\>more > A.java
class A {
   void m() {
      for( Object o : new java.util.HashSet() ) {
      }
   }
}
^C
C:\>javac A.java

C:\>more > B.java
class B {
   void m() {
      for( java.util.Iterator i = new java.util.HashSet().iterator() ; i.hasNext() ; ) {
         Object o = i.next();
      }
   }
}
^C
C:\>javac B.java

C:\>gvim -d a.d b.d

diff

The same!

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
2

I would suggest either:

a) Creating your own java.util.Iterator class and putting it in the java.util package.

OR

b) Using the Set.toArray() function to your advantage.

Once you've converted the set to an array, it's trivial to iterate through it.

(I'd go with b if this is the only difficulty, a if there are more than one instances of this issue)

Example Set.toArray():

Object[] myArray = myMap.keySet().toArray();
for(int i = 0; i < myArray.length; i++)
    doStuff((Class)myArray[i]);

How to make your own java.util.Iterator:

Use the code from here and add that class into your classpath.

Ryan Amos
  • 5,422
  • 4
  • 36
  • 56
  • a) isn't an option, any changes we make to the runtime will not make it into production per our client. – codewario Aug 04 '11 at 18:30
  • b) Oops, must have overlooked that when reading the 1.4 docs, spent too much time coding today haha – codewario Aug 04 '11 at 18:31
  • @MetalSearGolid You won't be changing the runtime. As long as the Iterator class is still referenced, but ends up leading to a crash, you'll be able to replace the missing class by adding one to the default class loader. – Ryan Amos Aug 04 '11 at 18:44
  • I thought about that, but it seemed like a more complex solution than what it needed to be. I should have seen toArray though when I looked at the javadocs though, that was my bad. – codewario Aug 04 '11 at 18:49
1

Have you got the Enumeration, it was like Iterators in pre-generic days. An elements() method on the Set may return it

djna
  • 54,992
  • 14
  • 74
  • 117
  • i was about to suggest the same thing, but after verification, the Set interface does not have an elements() method. This method is only provided on hashtable and vector – Cygnusx1 Aug 04 '11 at 18:42
1

I don't understand... Iterator is there since java 1.2.

1) your example shows the new syntax of for loop... will not work in 1.4.2 2) I`m sure you can do

Iterator it = mySet().iterator();

and loop thru it like other respond.

edit: ok just saw your new comment up... So you really dont have iterator!!! well... the toArray is in fact the best way to do it

Cygnusx1
  • 5,329
  • 2
  • 27
  • 39