Initialize ArrayList <ArrayList <Int>> with size in kotlin

I am trying to initialize a list with size in the constructor. But the size of my list is 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. N = 100 and Q = 100

While debugging my code, I found out that queries.size() = 100 , but seqList.size() = 0

Is my assumption wrong that seqList should also be initialized with N ArrayList<Int> objects.

+5
source share
1 answer

I suppose you're wrong, I'm afraid. Quoted from the ArrayList documentation :

Provides an implementation of MutableList that uses the resizable array as a backup storage.

This implementation does not provide a way to manage capacity , since support for the JS array itself resizes. There is no speed advantage for pre-distributing array sizes in JavaScript, so this implementation does not include any of the features and concepts of "gain growth".

Constructor, in particular:

 ArrayList(capacity: Int = 0) 

Creates an empty ArrayList.

An empty ArrayList created, so providing 100 as an argument will not result in the creation of elements inside the list.

+5
source

Source: https://habr.com/ru/post/1270830/


All Articles