我正在尝试在构造函数中初始化一个具有大小的列表。但是我的列表的大小是 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>
初始化对象。
最佳答案
恐怕你的假设不正确。
引自 documentation ArrayList
:
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作为参数不会在列表中创建元素。
关于java - 在 kotlin 中使用大小初始化 ArrayList<ArrayList<Int>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45661441/