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 –
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter size:") val n = read.nextInt() // creates array of given size and initializes it. val arr = Array(n) { i -> i.toString() } println("Elements in array: ") // Print each element in array. arr.forEach { println(it) } }
When you run the program, output will be –
Enter size: 5 Elements in array: 0 1 2 3 4
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]
val arr = IntArray(n)
Program –
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter size:") val n = read.nextInt() val arr = IntArray(n) println("Elements in array: ") // Print each element in array. arr.forEach { println(it) } }
When you run above program, output will be –
Enter size: 5 Elements in array: 0 0 0 0 0
If you want to initialise it with any other value,
Program –
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter size:") val n = read.nextInt() val arr = IntArray(n) {10} println("Elements in array: ") // Print each element in array. arr.forEach { println(it) } }
When you run above program, output will be –
Enter size: 5 Elements in array: 10 10 10 10 10
Or, if you want to write your own logic to initialise elements in array, you can write it inside lambda function.
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter size:") val n = read.nextInt() // Write your own logic inside lambda function to initialise elements in array val arr = IntArray(n) {it * 1} println("Elements in array: ") // Print each element in array. arr.forEach { println(it) } }
When you run program, output will be –
Enter size: 5 Elements in array: 0 1 2 3 4
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 –
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter size:") val n = read.nextInt() val arr = arrayOfNulls<String>(n) println("Elements in array: ") // Print each element in array. arr.forEach { println(it) } }
When you run the program, output will be –
Enter size: 5 Elements in array: null null null null null
Similarly, you can use different data type.
Using Integer data type –
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter size:") val n = read.nextInt() val arr = arrayOfNulls<Int>(n) println("Elements in array: ") // Print each element in array. arr.forEach { println(it) } }
When you run above program, output will be –
Enter size: 5 Elements in array: null null null null null
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,
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter size:") val n = read.nextInt() val arr = arrayOfNulls<CustomClass>(n) println("Elements in array: ") // Print each element in array. arr.forEach { println(it) } } class CustomClass(name: String)
When you run the program, output will be –
Enter size: 4 Elements in array: null null null null
Here, CustomClass is a class.