
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Array - component1() Function
The Kotlin array component1() function retrieves the first element of the array object. If the array size is less than one, or empty, this function throw an IndexOutOfBoundsException except.
Syntax
Following is the syntax of Kotlin array component1() function −
operator fun <T> List<T>.component1(): T
Parameters
This function does not accepts any parameters
Return value
This function returns an array element of any data type.
Example 1
In the below example, lets see how to determine the value of the first element of the array using the component1() function.
fun main(args: Array<String>) { var array = Array(10) { i -> 1 } try { val value = array.component1() println("The value of the first element of the array is: $value") } catch (exception : Exception) { println("Array length is smaller than 1") println(exception.printStackTrace()) } }
Output
Following is the output −
The value of the first element of the array is: 1
Example 2
Now, let's see another example, here we creates an array that stores different type of data. We then use the component1() function to get the value of the zero index −
fun main(args: Array<String>) { var array = arrayOf(10, 23.4, 'c', "tutorialspoint.com") try { // use the component1() function val value = array.component1() println("The value of the first element of the array is: $value") } catch (exception : Exception) { println("An element passing the given predicate does not exist or the array is empty") println(exception.printStackTrace()) } }
Output
Following is the output −
The value of the first element of the array is: 10
Example 3
The below example creates an empty array and uses the component1() function to display the value of the first element of an array −
fun main(args: Array<String>) { var array = emptyArray<String>() try { val value = array.component1() println("The value of the first element of the array is: $value") } catch (exception : Exception) { println("An element passing the given predicate does not exist or the array is empty") println(exception.printStackTrace()) } }
Output
The above code generate the following output. If an exception occurs −
An element passing the given predicate does not exist or the array is empty