Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Impasse: compiler/eclipse bug

Thread view: 
Twisted - 12 Jul 2006 00:09 GMT
This code is part of an inner class for a custom collection class. The
parent class contains an instance member called "inner" of type Map<T,
Integer> and has a type parameter (T).

    private class Iterator<T> implements java.util.Iterator<T> {
        private java.util.Iterator<Map.Entry<T, Integer>> i;

        Iterator () {
            i = inner.entrySet().iterator();
        }

        public T next () {
            Map.Entry<T, Integer> entry = i.next();

        }
    }

That's what I have so far and it complains about the constructor's only
line of code:

Type mismatch: cannot convert from Iterator<Map.Entry<T,Integer>> to
Iterator<Map.Entry<T,
Integer>>

In other words, cannot convert from foo to foo. Yes. The types are the
same. The iterator() call on the entrySet() return value returns a
java.util.Iterator<Map.Entry<T, Integer>>; the instance variable i is
explicitly declared as being of this same type.

Now what do I do?
Twisted - 12 Jul 2006 00:20 GMT
[snip]

Eh -- removing the <T> from "public class Iterator<T>" removed.
Apparently it considered this to be a separate T from the nesting
class's own <T>, and it was the two Ts it considered to differ in the
two iterator types.

This new code compiles with no errors or warnings; soon I will have
implemented a working Bag.

    private class Iterator implements java.util.Iterator<T> {
        private java.util.Iterator<Map.Entry<T, Integer>> i;
        private Map.Entry<T, Integer> entry;
        private int qty;

        Iterator () {
            i = inner.entrySet().iterator();
            qty = 0;
        }

        public T next () {
            if (qty == 0) {
                entry = i.next();
                qty = entry.getValue().intValue();
            }
            --qty;
            return entry.getKey();
        }

        public boolean hasNext () {
            return (qty > 0) || i.hasNext();
        }

        public void remove () {
            int n = entry.getValue().intValue() - 1;
            if (n > 0) {
                entry.setValue(new Integer(n));
            }
            assert qty == 0;
            i.remove();
        }
    }
Hendrik Maryns - 12 Jul 2006 13:59 GMT
Twisted schreef:
> [snip]
>
[quoted text clipped - 5 lines]
> This new code compiles with no errors or warnings; soon I will have
> implemented a working Bag.

You do know about Jakarta Commons Collections, right?  It has several
Bag implementations.

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Twisted - 12 Jul 2006 17:07 GMT
> You do know about Jakarta Commons Collections, right?

Nope.

> It has several Bag implementations.

Fascinating.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.