4

I have a method to remove an object from the list, this is used to create a listView so the object is removed using the index of the listview item selected. But I want the method to work for several types of objects i.e., I have more than one listview.

public void removeFromList(ListView<Label> listView, ArrayList<objects?> arrayList){
        int minus = listView.getSelectionModel().getSelectedIndex();
        arrayList.remove(minus);
        listView.getItems().remove(minus);
}
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
Billies Wesley
  • 437
  • 1
  • 6
  • 21

1 Answers1

7

Use the wildcard bound:

ArrayList<?> arrayList

This allows you to pass any ArrayList to the method.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 3
    You also can use `List>` for every kind of List and ` extends A>` if you want to say "I will allow multiple types but from..." For example you want to use employee/worker/...-list so ? need to extend the class `Person` – LenglBoy Mar 26 '18 at 07:35
  • 1
    @LenglBoy indeed. After all, `List>` is ([roughly](https://stackoverflow.com/questions/8055389/whats-the-difference-between-and-extends-object-in-java-generics)) the same as `List extends Object>`, so it's implicitly upper bounded to `Object`. If you want a tighter bound, you just need to be explicit. – Andy Turner Mar 26 '18 at 08:11