Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bizarre Incompatible Types Complaint

 
Kevin Simonson
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an application that currently uses an {ArrayList} to build up a list of a lot of elements when I don't know ahead of time how large the list is going to be. But due to the way I'm doing it, it would be nice if I could eliminate a (potentially high) number of elements whose indices are above a given value, without having to call {remove()} on each and every one of those elements.

So I wrote a class {Derived} that extends {ArrayList} and therefore inherits all of {ArrayList}'s methods, while overriding {iterator()} and {add()}, and adding {reset()} that allows you to get rid of those pesky high index elements in constant time. To do a more thorough job I probably should have overridden the other {add()} method, and maybe others, but I thought this program should be enough for proof of concept.

The actual program is:

But when I try to compile this I get the bizarre error message:

Derived.java:25: incompatible types
found : El
required: El
return get( position++);
^
1 error

How in the world can type {El} be incompatible with itself? I don't understand this at all. Can anyone explain what this compiler message actually means? Is this an indication that my compiler might be buggy?

Kevin S
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 5 you declare a type variable named "El". Then at line 9 you declare another type variable, also named "El". Apparently (judging from the error message) those two declarations declare two different type variables which just happen to have the same name.

I'm guessing here, but try



and change line 41 to match that change. Here you aren't declaring a new type variable any more, you are using the existing variable which was declared by the outer class. Give that a try and see if I'm right.
 
Kevin Simonson
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:At line 5 you declare a type variable named "El". Then at line 9 you declare another type variable, also named "El". Apparently (judging from the error message) those two declarations declare two different type variables which just happen to have the same name.

I'm guessing here, but try



and change line 41 to match that change. Here you aren't declaring a new type variable any more, you are using the existing variable which was declared by the outer class. Give that a try and see if I'm right.

Thanks, Paul; that did the trick; my code is working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic