Implemented Iterator , but the next() method for some reason returns the Object type, although I specified a return type of Task .

 ArrayTaskList atl = new ArrayTaskList(); atl.add(t); atl.add(t1); Iterator i = atl.iterator(); while(i.hasNext()){ System.out.println(i.next()); } 

next() method code:

 @Override public Task next() { int i = cursor; if (i > size()) { throw new NoSuchElementException(); } cursor++; return arrayTask[secondCursor = i]; } 
  • 2
    You use some non-standard ArrayTaskList class, we do not know how it is implemented and why you expect that there will be a correct Iterator. And how do you check the type of the returned object? - Vartlok
  • one
    Show the code of the method ArrayTaskList.iterator() - Nofate ♦

1 answer 1

You do not generic an iterator:

 List<Element> list = new ArrayList<>(); Iterator<Element> = list.iterator(); 
  • The question author has an ArrayTaskList class, not a List. Perhaps it is already typed. - Vartlok
  • @Vartlok most likely. Only the answer is about the second line, not about the first. - etki