Kotlin Array declaration access changeAn array is a homogeneous data structure, that stores a sequence of consecutively numbered values--allocated in contiguous memory. Each object of the array can be accessed by using its number (i.e., index).
In Kotlin to create an array we use the built in function
arrayOf()and we place values inside parentheses with comma separated format.
In this section you will learn:
- How to declare an Array in Kotlin.
- How to Access an array element in Kotlin.
- How to modify and change an Array element in Kotlin.
- How to find the size of An array in Kotlin.
- How to find if an element exists in An array in Kotlin.
Kotlin Array declaration syntax
Access the Elements of an ArrayTo access an element in an Array we will need to use the index of that element for example:
array_namein this example we access element at index 1 for array arra_name, we use braces to surround the index.
Change an Array Element in Kotlinto change an array element we just need to use the existing index and set a new value. See below example :
Kotlin Array Length / SizeIn Kotlin we use function
sizeto get the length and number of elements in an array. See example below:
Kotlin Check if an Element Existsin Kotlin the operator
inis used to identify if an element exist in array and it will return true if exist and false if not. See example below:
In this page (written and validated by A. Gawali) you learned about Kotlin Array declare access change size find . What's Next? If you are interested in completing Kotlin tutorial, your next topic will be learning about: Kotlin Array Sorting .
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: email@example.com.