使用 kotlin 中的大小初始化 ArrayList<ArrayList<Int>>
Initialise ArrayList<ArrayList<Int>> with a size in kotlin
我正在尝试在构造函数中初始化一个具有大小的列表。但是我的列表的大小是 0.
val seqList = ArrayList<ArrayList<Int>>(N) // This has the Problem
val queries = ArrayList<Query>(Q) // This works like a charm
我将 N
和 Q
都设置为来自用户的非零输入让我们说
N = 100
和 Q = 100
在调试我的代码时我发现,queries.size() = 100
但是 seqList.size() = 0
我的假设不正确吗,seqList
也应该用 N ArrayList<Int>
个对象初始化。
恐怕你的假设不正确。
引自ArrayList
的documentation:
Provides a MutableList
implementation,
which uses a resizable array as its backing storage.
This implementation doesn't provide a way to manage capacity, as
backing JS array is resizeable itself. There is no speed advantage to
pre-allocating array sizes in JavaScript, so this implementation does
not include any of the capacity and "growth increment" concepts.
构造函数特别是:
ArrayList(initialCapacity = 0))
Creates an empty ArrayList.
创建了一个 empty ArrayList
,因此提供 100 作为参数将不会在列表中创建元素。
我正在尝试在构造函数中初始化一个具有大小的列表。但是我的列表的大小是 0.
val seqList = ArrayList<ArrayList<Int>>(N) // This has the Problem
val queries = ArrayList<Query>(Q) // This works like a charm
我将 N
和 Q
都设置为来自用户的非零输入让我们说
N = 100
和 Q = 100
在调试我的代码时我发现,queries.size() = 100
但是 seqList.size() = 0
我的假设不正确吗,seqList
也应该用 N ArrayList<Int>
个对象初始化。
恐怕你的假设不正确。
引自ArrayList
的documentation:
Provides a
MutableList
implementation, which uses a resizable array as its backing storage.This implementation doesn't provide a way to manage capacity, as backing JS array is resizeable itself. There is no speed advantage to pre-allocating array sizes in JavaScript, so this implementation does not include any of the capacity and "growth increment" concepts.
构造函数特别是:
ArrayList(initialCapacity = 0))
Creates an empty ArrayList.
创建了一个 empty ArrayList
,因此提供 100 作为参数将不会在列表中创建元素。