This article explores different ways to print an array in Kotlin.

1. Using index-based for-loop

The standard solution to print arrays in Kotlin is using an index-based loop.

Download Code

Output:

1
2
3
4
5

2. Using foreach loop

In Kotlin, you can replace the above index-based for-loop with a foreach loop.

Download Code

Output:

1
2
3
4
5

3. Using forEach() function

Alternatively, you can use the forEach() function to print an array.

Download Code

4. Using contentToString() function

Another solution is to simply print the string representation of the contents of the given array. This can be done using the contentToString() function.

Download Code

That’s all about printing an array in Kotlin.