简体   繁体   English

初始化ArrayList <ArrayList<Int> &gt;大小在kotlin

[英]Initialise ArrayList<ArrayList<Int>> with a size in kotlin

I am trying to initialise a list with a size in the constructor. 我试图在构造函数中初始化一个大小的列表。 But the size of my list is 0. 但是我的列表大小为0。

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

I have both N and Q set as non zero inputs from the user lets say N = 100 and Q = 100 我将NQ设置为来自用户的非零输入,假设N = 100Q = 100

While debugging my code I found out that, queries.size() = 100 but seqList.size() = 0 调试我的代码时,我发现, queries.size() = 100seqList.size() = 0

Is my assumption incorrect, that seqList should also have been initialized with N ArrayList<Int> objects. 我的假设是不正确的, seqList也应该用N ArrayList<Int>对象初始化。

You're assumption isn't correct, I'm afraid. 我担心你的假设不正确。 Quoted from the documentation of ArrayList : 引自ArrayList文档

Provides a MutableList implementation, which uses a resizable array as its backing storage. 提供MutableList实现,该实现使用可调整大小的数组作为其后备存储。

This implementation doesn't provide a way to manage capacity , as backing JS array is resizeable itself. 此实现不提供管理容量的方法 ,因为支持JS数组本身可以调整大小。 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. 在JavaScript中预先分配数组大小没有速度优势,因此该实现不包括任何容量和“增长增量”概念。

The constructor particularly: 特别是构造函数:

 ArrayList(initialCapacity = 0)) 

Creates an empty ArrayList. 创建一个空的ArrayList。

An empty ArrayList is created, thus providing 100 as an argument will not create elements inside the list. 创建一个空的 ArrayList ,因此提供100作为参数将不会在列表中创建元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM