Sharing our technical trials in terms of Ubuntu software installs, Blogger hacks, Android and Kotlin learning.
More on Arrays – Kotlin Tutorials for beginners
More on Arrays – Kotlin Tutorials for beginners

More on Arrays – Kotlin Tutorials for beginners

More on array’s will cover topics like type of an array, size of an array, and editing items in an array.

I. ARRAYS AND TYPE

1. Defining the type implicitly

  • Compiler infers the type of an array based on the type of the values assigned
  • var intArray = arrayOf(1,2,3) – Array of type Int
  • var mixedArray = arrayOf(‘a’,20,”Hello”) – Mixed Array
Example:
 fun main (args:Array<String>)
{

     var mixedArray = arrayOf('a',20,"Hello")

     println("Item at first index is - "+mixedArray[0])
     println("At second index is - "+mixedArray[1])
     println("And at third index is - "+mixedArray[2])
      
}
Output

Item at first index is – a

At second index is – 20

And at third index is – Hello

Explanation

As compiler infers the type of an array based on the type of the values assigned, you can have mixed values

2. Defining the type explicitly

  • Type can also be defined explicitly using Array<Type>, when you explicitly mention the type array can hold only those type of values
  • var intArray : Array<Int> = arrayOf(10,20,30)

Example
 fun main (args:Array<String>)
{

     var intArray : Array<Int> = arrayOf('a',20,30)

     println("Item at first index "+intArray[0])
      
}
Output

Kotlin: Type inference failed. Expected type mismatch: inferred type is Array<Any> but Array<Int> was expected

Explanation

As we have explicitly mentioned Array<Int>, this array can hold only Int values. Trying to assign any other type will give error.

II. ARRAYS AND SIZE

1. Dot operator ( . )

  • You can know the size of an array by using dot operator ( . ) and calling size on the array object
Example
 fun main (args:Array<String>)
{

     var intArray = arrayOf(1,2,3)

     println("Size of array is "+intArray.size)
      
}
Output

Size of array is 3

2. While accessing the items in an array, if index is greater than the size of an array, the compiler throws error

Example
 fun main (args:Array<String>)
{

      var intArray  = arrayOf(10,100,1000 )

      println("Print item at index 3 "+intArray[3])
      
}
Output

java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

Explanation

intArray has 3 items 10, 100, 1000 as indexes start from 0, the indexes for this array would be 0,1,2

[0] – first item 10

[1] – second item 100

[2] – third item 1000

[3] – refers to the fourth item

The array has only three items and we are trying to fetch the fourth item which is not present, hence the compiler throws error

III. UPDATING AN ARRAY

1. Updating an item in an array

  • As mentioned in our previous post Introduction to Arrays array objects are mutable, meaning you can update the values
Example
 fun main (args:Array<String>)
{

      var intArray  = arrayOf(9,20,30)

      intArray[0] = 10

      println("Print item at first index "+intArray[0])
      
}
Output

Print item at first index 10

Explanation

var intArray = arrayOf(9,20,30)

intArray has items 9,10,20, if you want to change the value of the first item from 9 to value 10

Access the first index intArray[0] use equals operator ( = ) and assign the new value

2. Type check while updating an item in an array

  • While you have the provision to update the values, you need to make sure that the new value is of the right type
Example
 fun main (args:Array<String>)
{

     var intArray  = arrayOf(10,20,30)

      intArray[0] = 'a'

      println("Print item at first index "+intArray[0])
      
}
Output

Kotlin: The character literal does not conform to the expected type Int

Explanation

While declaring, item at the first index intArray[0] was declared as Int, therefore if you want to update the value at index 0 it has to be of the similar type i,e Int

As in our example we are trying to assign a character value, the compiler throws error

That brings us to the end of this topic on Array’s. In our next chapter we will learn about Loops in kotlin