i wrote simple class:
public class Report<T> implements Iterable {
private ObservableList<T> items = FXCollections.observableArrayList();
// ...
public ObservableList<T> getItems() {
return items;
}
public Iterator<T> iterator {
return items.iterator();
}
// ...
}
But when i try to use foreach loop like this i get Incompatible types error:
Report<FinRecord> report = new Report<>()
for (FinRecord r : report) {
// ...
}
This code works fine, but i wanted cleaner code and i don't understand why previous code returns Object.
for (FinRecord r : report.getItems()) {
// ...
}
Is it java's peculiarity, so it creates Iterator not Iterator<T> or i am missing something?
Your class has to implements Iterable<T>.
public class Report<T> implements Iterable<T>
{
private ObservableList<T> items = FXCollections.observableArrayList();
// ...
public ObservableList<T> getItems() {
return items;
}
public Iterator<T> iterator() {
return items.iterator();
}
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With