From the course: Kotlin Essential Training: Functions, Collections, and I/O
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Arrays - Kotlin Tutorial
From the course: Kotlin Essential Training: Functions, Collections, and I/O
Arrays
- [Instructor] Let's discuss how we store collections of data in Kotlin. The most basic data structure for this is an array, which behaves very similarly to arrays in other programming languages. Let's take a look at how to create and work with arrays in Kotlin. An array is a collection of values, typically of the same type. Let's say we wanted to store five integer values, we could do so using the arrayOf function. If we passed in five comma separated values here, this will create an array of size five with the elements 1 through 5, the type of the array will be inferred to be array of non-null int. If we replace the last value 5 and instead use null, then the inferred type of the array will be nullable int. The array of function is smart enough to recognize whether we are mixing nullable and non-null values. If we want to create an array of some given size, and initialize it with all null values, we can…