What if you could organize many pieces of information neatly and work with them easily, without juggling dozens of separate variables?
Why Array creation and usage in Kotlin? - Purpose & Use Cases
Imagine you want to keep track of your daily expenses for a week. You try writing each amount on a separate piece of paper or in different variables like expense1, expense2, expense3, and so on.
This manual way is slow and confusing. If you want to add a new day or calculate the total, you have to update many places. It's easy to make mistakes or forget some values.
Using arrays lets you store all your expenses in one place. You can easily add, access, or change any day's expense by its position. This saves time and reduces errors.
val expense1 = 10 val expense2 = 20 val expense3 = 15 // To sum: val total = expense1 + expense2 + expense3
val expenses = arrayOf(10, 20, 15) val total = expenses.sum()
Arrays let you handle many related values together, making your programs simpler and faster to write and understand.
Tracking daily temperatures for a week to find the hottest day or average temperature becomes easy with arrays.
Arrays store multiple values in one variable.
They let you access and change values by position.
Using arrays reduces mistakes and saves time.
Practice
Solution
Step 1: Understand array creation syntax
Kotlin usesarrayOf()to create an array with given elements.Step 2: Check each option
val arr = arrayOf(1, 2, 3)usesarrayOf(1, 2, 3)which is correct.val arr = [1, 2, 3]uses square brackets which is invalid in Kotlin.val arr = IntArray(1, 2, 3)which uses invalid constructor arguments for IntArray.val arr = Array(3) { 1, 2, 3 }tries to pass multiple values in a lambda which is incorrect.Final Answer:
val arr = arrayOf(1, 2, 3) -> Option BQuick Check:
Use arrayOf() for arrays with elements [OK]
- Using square brackets [] like in other languages
- Confusing IntArray with Array<Int>
- Trying to pass multiple values in Array constructor lambda
Solution
Step 1: Understand Array constructor usage
Kotlin'sArray(size) { init }creates an array of given size, initializing each element with the lambda result.Step 2: Evaluate options
val arr = Array(5) { 0 }correctly usesArray(5) { 0 }to create an array of 5 zeros.val arr = Array(0, 0, 0, 0, 0)is invalid syntax.val arr = Array(5, 0)is invalid syntax.val arr = IntArray(5, 0)is invalid becauseIntArrayconstructor takes only size or size with init lambda, not two parameters.Final Answer:
val arr = Array(5) { 0 } -> Option CQuick Check:
Use Array(size) { value } to initialize [OK]
- Passing two parameters to Array constructor incorrectly
- Confusing IntArray constructor parameters
- Passing multiple values directly to Array constructor
val arr = arrayOf(10, 20, 30) println(arr[1])
Solution
Step 1: Understand array indexing
Arrays in Kotlin are zero-indexed, soarr[0]is 10,arr[1]is 20, andarr[2]is 30.Step 2: Check the printed value
The code printsarr[1], which is 20.Final Answer:
20 -> Option AQuick Check:
Array index 1 holds 20 [OK]
- Confusing index 1 with first element
- Expecting 30 instead of 20
- Thinking arrays start at 1
val arr = arrayOf(1, 2, 3) arr[3] = 4 println(arr.joinToString())
Solution
Step 1: Check array size and indexing
The array has 3 elements with indices 0, 1, 2. Accessing index 3 is out of bounds.Step 2: Understand runtime behavior
Assigningarr[3] = 4causes anIndexOutOfBoundsExceptionat runtime.Final Answer:
IndexOutOfBoundsException at arr[3] assignment -> Option DQuick Check:
Array indices must be within 0 to size-1 [OK]
- Trying to assign value outside array bounds
- Thinking array size auto-expands
- Confusing immutability with index errors
Solution
Step 1: Understand array initialization with lambda
Kotlin'sArray(size) { index -> value }creates an array where each element is initialized by the lambda using the index.Step 2: Check each option
val arr = Array(4) { index -> index * index }uses explicit lambda parameterindexand correctly computes index squared.val arr = arrayOf(4) { index -> index * index }is invalid becausearrayOfdoes not take a size and lambda.val arr = intArrayOf(4) { index -> index * index }is invalid syntax for theintArrayOffactory function.val arr = Array(4) { -> it * it }has invalid lambda syntax whereitis undefined.Final Answer:
val arr = Array(4) { index -> index * index } -> Option AQuick Check:
Use Array(size) with index lambda for computed values [OK]
- Confusing it with index in lambda
- Using arrayOf() for dynamic initialization
- Not using lambda for element computation
Ready to prove it?
Pass the test and this pattern moves unknown
