Write a program to create Kotlin array of given size.
There are scenarios where we need to create an array of particular data type and we know only about length of that array. How do we create array of that particular data type of given size ?
In this post, we deal with such scenarios. Note that we know only about size and data type of that array.
We can create an array of given size
- Using Array constructor
- Using special class for primitive data type – IntArray, DoubleArray, FloatArray etc.
- Using arrayOfNulls() library function
1. Create array using Array constructor
We can use Array constructor to create an array of given size as below –
val read = Scanner(System.`in`) |
val arr = Array(n) { i -> i.toString() } |
println( "Elements in array: " ) |
arr.forEach { println(it) } |
When you run the program, output will be –
Array(n) created an array of given size. lambda function provided with Array initialises the elements of array. You can write your own logic inside it to initialise the elements in array.
2. Create an array using special class for primitive data type.
Kotlin has specialised class to represent array of primitive types without boxing overhead.
For example,
– IntArray for integer data type,
– FloatArray for float data type etc.
These classes are NOT inherited from Array class. Instead, they have same set of methods and properties. Each of them have a corresponding factory function.
IntArray creates an array, of given size, of integer data type where each element is initialised with 0.
Let’s say n = 5, so, array will be [0,0,0,0,0]
Program –
val read = Scanner(System.`in`) |
println( "Elements in array: " ) |
arr.forEach { println(it) } |
When you run above program, output will be –
If you want to initialise it with any other value,
Program –
val read = Scanner(System.`in`) |
val arr = IntArray(n) { 10 } |
println( "Elements in array: " ) |
arr.forEach { println(it) } |
When you run above program, output will be –
Or, if you want to write your own logic to initialise elements in array, you can write it inside lambda function.
val read = Scanner(System.`in`) |
val arr = IntArray(n) {it * 1 } |
println( "Elements in array: " ) |
arr.forEach { println(it) } |
When you run program, output will be –
Similarly, you can use special classes of different primitive data types in kotlin.
Lists are –
Data type |
Class |
Description |
Integer |
IntArray |
val arr = IntArray(n) |
Float |
FloatArray |
val arr = FloatArray(n) |
Double |
DoubleArray |
val arr = DoubleArray(n) |
Boolean |
BooleanArray |
val arr = BooleanArray(n) |
Char |
CharArray |
val arr = CharArray(n) |
Byte |
ByteArray |
val arr = ByteArray(n) |
Long |
LongArray |
val arr = LongArray(n) |
Short |
ShortArray |
val arr = ShortArray(n) |
Unsigned Int |
UIntArray |
val arr = UIntArray(n) |
Unsigned Byte |
UByteArray |
val arr = UByteArray(n) |
Unsigned Short |
UShortArray |
val arr = UShortArray(n) |
3. Create Kotlin array using arrayOfNulls() library function
If you want to create Kotlin array of given size and initialise each elements with null, you can use arrayOfNulls() library function.
For example,
val arr = arrayOfNulls<Int>(n) |
Above code creates an integer array of size n. You can pass different data type as well.
For example,
val arr = arrayOfNulls<Float>(n) |
val arr = arrayOfNulls<String>(n) |
Program –
val read = Scanner(System.`in`) |
val arr = arrayOfNulls<String>(n) |
println( "Elements in array: " ) |
arr.forEach { println(it) } |
When you run the program, output will be –
Similarly, you can use different data type.
Using Integer data type –
val read = Scanner(System.`in`) |
val arr = arrayOfNulls<Int>(n) |
println( "Elements in array: " ) |
arr.forEach { println(it) } |
When you run above program, output will be –
Note that irrespective of data type, value of each element is null. It was null for string as well.
3.1 Create Kotlin Array of Custom Data Type Object
If you want to create Kotlin array of given size of custom class object, you can do so using arrayOfNulls() library function. You just need to pass class name and size of array.
For example,
val read = Scanner(System.`in`) |
val arr = arrayOfNulls<CustomClass>(n) |
println( "Elements in array: " ) |
arr.forEach { println(it) } |
class CustomClass(name: String) |
When you run the program, output will be –
Here, CustomClass is a class.