<分区>
在处理我的代码时,我使用了 Collections
并遇到了一些对我来说似乎很奇怪的东西。我想知道为什么以前从未问过这个问题,尽管我提到的这个类和方法从 Java 1.4 就存在
/**
* Returns an array list containing the elements returned by the
* specified enumeration in the order they are returned by the
* enumeration. This method provides interoperability between
* legacy APIs that return enumerations and new APIs that require
* collections.
*
* @param e enumeration providing elements for the returned
* array list
* @return an array list containing the elements returned
* by the specified enumeration.
* @since 1.4
* @see Enumeration
* @see ArrayList
*/
public static <T> ArrayList<T> list(Enumeration<T> e) {
ArrayList<T> l = new ArrayList<>();
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
这是我正在经历的方法。我真的很想知道为什么 API 写得不干净(我认为)。
- 没有对输入集合进行
null
检查,这是标准 API 应该始终考虑的事情。 - 创建的
ArrayList
存储在ArrayList
的引用中,通常存储在List
引用中。返回类型也是如此。
我没有在 Java 错误库中为此提出错误,正如 Javadoc 已经指出的那样,这些情况是已知的。
有什么建议吗?