This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 44563
    
  34
It wasn’t about generics, was it? You should have written unmodifiableList().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44563
    
  34
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: 44563
    
  34
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: 44563
    
  34
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: 44563
    
  34
Yes, that looks more like it!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44563
    
  34
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: 44563
    
  34
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: 44563
    
  34
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic compile error
 
It's not a secret anymore!