Ashutosh Panda
11/05/2019, 2:39 AMMike
11/05/2019, 2:51 AMIntArray(10)
will create an Integer Array of size 10, with all elements having value 0.
val stringArray = arrayOfNulls<String>(10)
If you don't want the elements to be nullable, then you can use val stringArray = Array(10) {""}
That looks 'weird', but basically you're creating an Array of size 10, and initializing all the values with an empty String. This will result in an Array<String> of size 10.Mike
11/05/2019, 2:52 AMAshutosh Panda
11/05/2019, 3:14 AMMike
11/05/2019, 3:38 AMAshutosh Panda
11/05/2019, 3:39 AMMike
11/05/2019, 3:40 AMmolikuner
11/05/2019, 8:56 AMinitialCapacity
.
val list: List<String> = ArrayList<String>(5)
Burkhard
11/05/2019, 10:37 AMinitialCapacity
has nothing to do with a fixed size. You can use it even if you want to change the length later. ArrayList
is backed by an array. If the list is full when you add, a new array is created with a bigger size. The old data is then copied into the new array.
Also while I normally would suggest using Lists as well, there is still an argument for arrays if you know you have a fixed length.molikuner
11/05/2019, 12:16 PMinitialCapacity
!= fixed size
, but I just said, that this would remove the overhead of copying the array over and over again, if you now the length in beforehand.Mike
11/05/2019, 1:02 PMinitialCapacity
constructor. I agree it's helpful if you know up front what size the list is, or set a large enough initial size before populating it to avoid the churn of constantly growing it.