通过将 Collection 作为参数传递,使用 ArrayList 构造函数创建 ArrayList
Creating ArrayList by using ArrayList constructor by passing Collection as an argument
我正在学习集合框架。我正在通过 List 接口,List 接口的一种实现是 ArrayList。
Arraylist 中存在四个构造函数class 来创建 ArrayList 对象。
ArrayList(Collection c)
ArrayList<E> NameOfArrayList = new ArrayList<E>(collectionName);
我的问题:
问)我无法理解如果我们使用上面的构造函数创建一个arraylist对象,内部流程是什么?
问)如果我们使用此构造函数创建数组列表对象,我们是否将一个数组列表放入特定索引的另一个数组列表中?真的对此感到困惑吗?
您可能习惯使用 Javascript 或 C++ 或 Scala 等语言,您可以在其中使用单行文字元素声明和填充集合。 Java的集合框架没有这个功能(不过像Guava这样的框架可以填补这个空白)。
List< Integer > liFoo = new ArrayList<>();
liFoo.add( 6 ); // liFoo contains one element
liFoo.add( -499 ); // liFoo now contains two elements
List< Integer > liBar = new ArrayList<>( liFoo );
// liBar contains the same two elements as liFoo, in a new list
我正在学习集合框架。我正在通过 List 接口,List 接口的一种实现是 ArrayList。
Arraylist 中存在四个构造函数class 来创建 ArrayList 对象。
ArrayList(Collection c)
ArrayList<E> NameOfArrayList = new ArrayList<E>(collectionName);
我的问题:
问)我无法理解如果我们使用上面的构造函数创建一个arraylist对象,内部流程是什么?
问)如果我们使用此构造函数创建数组列表对象,我们是否将一个数组列表放入特定索引的另一个数组列表中?真的对此感到困惑吗?
您可能习惯使用 Javascript 或 C++ 或 Scala 等语言,您可以在其中使用单行文字元素声明和填充集合。 Java的集合框架没有这个功能(不过像Guava这样的框架可以填补这个空白)。
List< Integer > liFoo = new ArrayList<>();
liFoo.add( 6 ); // liFoo contains one element
liFoo.add( -499 ); // liFoo now contains two elements
List< Integer > liBar = new ArrayList<>( liFoo );
// liBar contains the same two elements as liFoo, in a new list