Reputation: 21
When trying to compile the following code, I get the error:
[ERROR] /C:/workspace/Oefeningen/3/Opdracht 2/programmeren_ex-adreslijst-java/src/main/java/be/uantwerpen/AdresLijst.java:[39,16] incompatible types: java.util.ArrayList<be.uantwerpen.Adres> cannot be converted to be.uantwerpen.AdresLijst
I'm unsure what this exactly means and thus how to fix it. Any help would be appreciated.
Please ignore the dutch names, they're not important.
package be.uantwerpen;
import java.util.ArrayList;
public class AdresLijst implements AdresLijstIf {
private String naam;
public ArrayList<Adres> adressen;
public ArrayList<Adres> result;
public AdresLijst(String naam) {
this.naam = naam;
this.adressen = new ArrayList<Adres>();
}
public String getNaam() {
return naam;
}
public void adresToevoegen(Adres adres) {
this.adressen.add(adres);
}
public void adresVerwijderen(Adres adres) {
this.adressen.remove(adres);
}
public int aantalAdressen() {
System.out.println("Er zitten " + adressen.size() + " adressen in deze lijst");
}
public AdresLijst zoekNaam(String query) {
result = new ArrayList<Adres>();
for(Adres adres : adressen) {
if(adres.getNaam().startsWith(query) == true) {
this.result.add(adres);
}
}
return result;
}
}
Upvotes: 0
Views: 8449
Reputation: 2265
Your method signature public AdresLijst zoekNaam(String query)
should return a AdresLijst
but returns a ArrayList<Adres>
.
You should either change method signature to :
public ArrayList<Adres> zoekNaam(String query)
Either change the returned value with the type you need.
Upvotes: 0
Reputation: 102968
let's not disregard dutch names as unimportant :) I do speak it and that made it a lot easier to understand what your code is trying to do. Which is why you should attempt to write method names in english if you can; it'll be a lot more familiar to other programmers.
In your zoekNaam
method, the return type is declared as AdresLijst
. Whilst conceptually a List<Adres>
sure sounds like an AdresLijst, the compiler doesn't know that, and the typing system doesn't work that way: It's guns and grandmas as far as the compiler is concerned (they are completely unrelated; a List<Adres>
is not an AdresLijst
, and an AdresLijst
is not a List<Adres>
, and neither can be converted to the other automatically).
So, when you return result;
in that method, with result being a List<Adres>
, the compiler is complaining: Nope - you need to return an AdresLijst
, and not a List<Adres>
.
Option 1 is to make a new AdresLijst
object and set up its result
field with the appropriate stuff. But that's probably not a good idea:
You have a more fundamental design issue. Your fields are the state of your AdresLijst. Your zoekNaam
call changes this state - it modifies the result
field. That means a search is not 'fleeting' - a search modifies the list.
Except, you don't actually use this state anywhere. The right move is a bit more convoluted:
You need to first get rid of that result
field; it should not be part of the state of a list (it's a 'fleeting' aspect: Some code calls zoekNaam
, and gets a result back. It's not up to the code of AdresLijst to remember the result, whomever called you has the job of taking the results and storing them, or printing them, or express them in interpretive dance form - it's not your job to worry about what they do with it. Your job is just to run the search and provide the results).
Then, either change the return type to List<Adres>
, or, make a new AdresLijst
object and initialize its adressen
value with your search result:
// now that result is no longer a field, make a local var instead:
List<Adres> result = new ArrayList<Adres>();
// ... rest of your zoekNaam, but remove 'return result;'
// and now we make a new AdresLijst object,
// give it a proper name..
AdresLijst out = new AdresLijst(this.naam + " (Zoekresultaten)");
// add all our results to it...
out.adressen.addAll(result);
// and return it
return out;
Upvotes: 1