【发布时间】:2016-12-26 12:31:39
【问题描述】:
List<Employee> empLList = new LinkedList<>();
empLList.add(...)
Iterator<Employee> descItr = empLList.descendingIterator();
在上面的代码中,我无法访问 descendingIterator 并出现以下错误
cannot find symbol
symbol: method descendingIterator()
location: variable empLList of type List<Employee>
要获得 descendingIterator,我必须将 empLList 重铸为 LinkedList
Iterator<Employee> descItr = ((LinkedList) empLList).descendingIterator();
我的问题:一般来说,以上是使用泛型的一个缺点,即每次我们需要将对象转换回子类以访问子类的方法,或者泛型应该像那样工作。
或者,如果我们依赖太多子类的方法,我们不应该使用泛型
或者我错过了什么
我对示例中 GENERICS 的使用而不是所使用的集合感到好奇。
【问题讨论】:
标签: java generics linked-list