java - 在 kotlin 中使用大小初始化 ArrayList<ArrayList<Int>>

标签 java arrays list kotlin dynamic-arrays

我正在尝试在构造函数中初始化一个具有大小的列表。但是我的列表的大小是 0。

val seqList = ArrayList<ArrayList<Int>>(N) // This has the Problem
val queries = ArrayList<Query>(Q) // This works like a charm

两个都有NQ设置为来自用户的非零输入让我们说 N = 100Q = 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/

相关文章:

java - Zoho Rest 发票 API 返回 400 错误,Java HttpClient

r - 在 R 中展平嵌套列表并选择数据

python - 访问 split 函数后的最后一个字符串以创建新列表

python - 如何在 python 中递归创建排列列表?

java - 如何在 Java 数据驱动测试中传递数组作为参数?

使用 iText 的 Java PDF 数字签名可见,但不可打印

Java搜索替换

arrays - 在 item._id 为空的数组中查找项目

python - argmax 用于沿某个轴的多维数组

Javascript 输出一个包含两个值的数组,其中两个值都用它们自己的变量分隔