如果我像这样声明一个新列表:
List<String> listExample = someFunction();
将使用什么列表接口(interface)实现?
编辑:感谢您迄今为止的回答。什么被认为是执行此操作的干净方法,我应该始终用 new 声明列表吗?
最佳答案
正如 Eran 评论的那样,这完全取决于 someFunction();
返回。两者 ArrayList<E>
LinkedList实现了List接口(interface)。
你可以试试,
System.out.println("" + listExample.getClass());
找出已实现的内容。来自 docs
,
public final Class<?> getClass()
Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.
关于Java列表默认实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26750916/