0

Hello I have the following code

RekeningApp app;
Persoon persoon;
JComboBox personenList;

PersoonView(RekeningApp app) {

    this.app = app;

    personenList = new JComboBox();

    personenList.addItemListener(new ItemChangeListener());


}

Now I need a Foreach loop that goes through an arraylist filled with Persoon objects that is in the RekeninApp class.

For this arraylist I have a getter in RekeningApp like this

List<Persoon> Pers = new ArrayList<Persoon>();

 public List getPersonen() {

    return Pers;

}

So right after I make the combobox there must be a foreach loop only the problem is that when I make a loop It says the types are incompatible.

for (Persoon p : app.getPersonen()) {
        personenList.addItem(p);    
}
Reshad
  • 2,570
  • 8
  • 45
  • 86

2 Answers2

3

You need to have a typed List to avoid casting.

public List<Persoon> getPersonen() {
    return Pers;
}
Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

when I make a loop It says the types are incompatible.

That is because you are using a raw type List as return type. Iterating over such List will get you an Object and not Persoon. Change the return type to List<Person>:

public List<Persoon> getPersonen() {
    return Pers;
}

Related Post:

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525