wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Generic compile error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic compile error" Watch "Generic compile error" New topic
Author

Generic compile error

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


UnmodifiableWrapper.java:error: incompatible types
List<UnmodifiableWrapper> myList=Collections.unmodifiableCollection(new ArrayList<UnmodifiableWrapper>());
^
required: List<UnmodifiableWrapper>
found: Collection<UnmodifiableWrapper>
1 error


My one regret in life is that I am not someone else.
- Woody Allen
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Sorry , i got my mistake.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
It wasn’t about generics, was it? You should have written unmodifiableList().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
I think I can see a logic error there, too.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Campbell Ritchie wrote:It wasn’t about generics, was it? You should have written unmodifiableList().


Yes it was not about generic, there is a return type mismatch,I should have written over there,

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
That is another way to solve the compiler error, but it is syntactically very different. Have you worked out what I think the logic error is?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Campbell Ritchie wrote:Have you worked out what I think the logic error is?

No, i couldn't figure out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
How many elements does your List contain?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Oh sorry, i didn't post my full program.


one two six ten
one two six ten Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1075)
at UnmodifiableWrapper.main(UnmodifiableWrapper.java:34)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
Yes, that looks more like it!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
You do realise that remove() never works in a for‑each loop?
Have you tried to add() an object to the myList after creating the unmodifiable Collection?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
No, remove() isn’t in that loop. It was late when I wrote about remove()
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Yea, i had made my collection read-only.That's i wanted to test.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
And what happens when you put this line before your for‑each loop, in line 31?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Campbell Ritchie wrote:And what happens when you put this line before your for‑each loop, in line 31?


Of course, it would be added, because myList is still like normal ArrayList but my backed unmodifiableList is only read-only, And that element should come in iteration of unmodifiableList at first place.
 
wood burning stoves
 
subject: Generic compile error