![]() We can use for loop to loop through an array. We can also use count() member function to get the size of the array. Println( "Size of fruits array " + fruits.size ) Kotlin provides array property called size which returns the size i.e. Kotlin also provides get() and set() member functions to get and set the value at a particular index. When you run the above Kotlin program, it will generate the following output: So if you want to access 4th element of the array then you will need to give 3 as the index. ![]() We can access an array element by using the index number inside square brackets. Other factory methods available for creating arrays: For example, the factory method to create an integer array is: Kotlin also has some built-in factory methods to create arrays of primitive data types. Optionally we can provide a data type as follows:Īlternatively, the arrayOfNulls() function can be used to create an array of a given size filled with null elements. Val fruits = arrayOf("Apple", "Mango", "Banana", "Orange") To create an array in Kotlin, we use the arrayOf() function, and place the values in a comma-separated list inside it: Like any other modern programming languages, Kotlin also supports arrays and provide a wide range of array properties and support functions to manipulate arrays. Arrays are used to store multiple items of the same data-type in a single variable, such as an integer or string under a single variable name.įor example, if we need to store names of 1000 employees, then instead of creating 1000 different string variables, we can simply define an array of string whose capacity will be 1000.
0 Comments
Leave a Reply. |