Kotlin Array With Examples


What is an Array? 

Let's understand Array with an example - 

  • You need to store 50 favorite songs of the user. If you define 50 variables for storing these songs then it becomes messy to manage the code. Why? You will be handling all these 50 variables - managing functionality like previous and next, playing a particular song will be too complex.
  • Array comes into the picture to rescue. You will be only creating a single object that will store your 50 songs like a list i.e. list of 50 songs. Now you can access all these songs using just one variable i.e. array object.
  • Array - is a simple object that can store multiple values of the same type and has a fixed size. You need to define the type of values that will be stored into the array and you need to define the size.


Explanation -
  • To define an array, we use arrayOf function and pass comma-separated values inside this function. In the above image, we have a created an array of 3 elements which are of string data type.
  • Values inside the array are indexed i.e. every item has an index associated and that index starts from 0 i.e. first element is stored at index 0, the second element is stored at index 1, and so on.
  • Once you define the array, you can change the size. You can't add or remove elements from the array i.e. fixed size. Values we have used are of string type. Thus, the data type of the array is Array<String>

fun main() {
   val songs = arrayOf("Hello", "Final Countdown", "Highway To Hell")   
   
   //elements are stored at index starting from 0
   println(songs[0])  
   println(songs[1]) 
   println(songs[2]) 
}
  
  • In the above code snippet, we have defined another array named songs. To access the elements defined in the array - we use arrayname[index]
  • To access the first element, we have used songs[0], for the second element - songs[1]
  • You can get or set the value of an array element by using its index. But if you try to access elements beyond the array's size - you will get an exception (error) i.e. Array Index Out Of Bounds

Array operations

fun main() {
   val songs = arrayOf("Hello", "Final Countdown", "Highway To Hell")   
   
   //length of the array
   println(songs.size) // --> 3
   
   //Set the value
   songs[1] = "In the End"
    
   //check if array contains particular element
   println(songs.contains("Hello")) // --> true
   
   //index of a particular element
   println(songs.indexOf("Highway To Hell")) // --> 2
  
}
       
Explanation - 
  • Array has a size property that is used to access the size of the array. i.e. yourArray.size
  • There are different methods that you can access on your array object for different operations. For instance, if you want to check if the array contains a particular element, you can use the .contains method. You can refer to the official doc for more operations.

Looping through the array i.e. Traversing the array

You can use a for loop to loop through the elements of the array. In Kotlin, we can even access the index of the elements along with the value. Refer to below code snippet - 
fun main() {
   val songs = arrayOf("Hello", "Final Countdown", "Highway To Hell")   
   
   for(song in songs) {
     println(song)
   }
   
   //with index
   for( (index, song) in songs.withIndex()){
     println(index.toString() + " - " + song)     
   }
  
}

/*Output

Hello
Final Countdown
Highway To Hell
0 - Hello
1 - Final Countdown
2 - Highway To Hell

*/

Explanation - 
  • We have used to for loops in the above snippet. The first one will just print the elements.
  • Second, for loop uses withIndex() method to provide indexes as well.

That's it for this article. For more articles on Kotlin Tutorials For Beginner Series - refer to this.

Happy Learning !! Cheers From CheezyCode.

Comments

Popular posts from this blog

Create Android Apps - Getting Started

Kotlin Type Checking and Smart Cast with Examples