Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a problem with the Iterator in J#. In standard Java this code would have run through all the elements in �myVector� and would have worked fine. I have imported both java.util.Vector and java.util.Iterator.

 


Iterator myIterator = myVector.iterator();
while (myIterator.hasNext())
{
AnObject myObject = (AnObject) myIterator.next();
} 

 

In J# I receive this error and I don�t know how to fix it

 

Cannot find method 'iterator()' in 'java.util.Vector'

 

If anyone has any clues I would be very grateful.

 

Thanks in advance,

Harold Clements

  • 2 weeks later...
Posted

If you haven't noticed Java's Vector Class by default doesn't have a method for returning an iterator. It does however inherit off AbstractList, which in essence has a listIterator method, but i don't think the Vector class actually provided implementation to return a listIterator of all the objects inside the list. Generally iterators in Java are used on for example LinkedList's which is farily popular to use iterators on, and LinkedList has a method which returns it called the listIterator() method.

 

To traverse a Vector list just simply do it as if you were doing it with a ArrayList, Vector in Java is essentially the same as an ArrayList i think except it's synchronized.

Posted
What I was really trying to get at was that the above code (using an iterator with a vector) works fine if you were programming with the Java JDK1.4.2. However in J# some methods (which are available in Java) are not in J#. For example there is also a 'get' method in the JDK version of Vector with in non-existent in J#, which makes it very hard to migrate.
Posted
What I was really trying to get at was that the above code (using an iterator with a vector) works fine if you were programming with the Java JDK1.4.2. However in J# some methods (which are available in Java) are not in J#. For example there is also a 'get' method in the JDK version of Vector with in non-existent in J#' date=' which makes it very hard to migrate.[/quote']

 

 

Yeah i know what you mean... but the reason why they don't have some of the methods, or probably they do is compatibility reasons with the framework, because in other languages like C# it's rather similar to Java, and to make it cross compatible, they standardise some of the method naming i think... so in J# instead of the get method we have the elementAt method.

Posted

Thanks Winston, I feel a little stupid as I know the �elementAt� and have used it in my Java applications numerous times just did not associate it with the �get� method.

 

It takes me back to the early days of learning Java when the tutor said to me �There is sometimes more then one method in the class that does, more of less, the same thing. That�s why they keep deprecating methods in each new version�.

 

Well am happy now, I have been stuck on this for a while now.

 

Cheers again,

Harold Clements

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...