• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Bizarre Incompatible Types Complaint

 
Ranch Hand
Posts: 224
2
  • 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
 
Marshal
Posts: 26077
71
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: 224
2
  • 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.
 
Let nothing stop you! Not even this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic