‫Welcome back.
‫In this video we're going to talk about arrays.
‫An array is a collection of elements of the same data type, organized in a specific order and accessed
‫by an index.
‫Arrays provide a convenient way to store and manipulate multiple values of the same type.
‫You can declare an array using the array of function or the array constructor.
‫For example, val o s names equals array of.
‫And this array contains four elements windows, Android, macOS, and Linux.
‫You can access individual elements of an array using square brackets and an index.
‫Arrays in Kotlin are zero based, so the first element has an index of zero.
‫For example, I'll create val first element equals to os names array, and here we specify the index
‫of zero.
‫Here I am accessing the OS name array and getting the first element and store it inside first element
‫a variable.
‫Let me test it so println.
‫The first element run the program and here we go.
‫We accessed the first element of this array at index zero.
‫Moreover, you can modify elements of an array by assigning a new value to a specific index.
‫For example, OS names at index one.
‫So I'm referring for the second element again guys this is index zero.
‫This is one, two and three.
‫So if I need to access the second element I need to specify an index one.
‫Inside these square brackets I'll modify it to I os if I print.
‫Now the second element os names at index one run the program iOS, so Android is replaced by iOS.
‫Here you can obtain the size and the number of elements of an array using the size property.
‫For example, val size equals two OS names.
‫This is the array dot size.
‫This is the attribute size.
‫Let me print the size.
‫Run the program.
‫And here we go.
‫We have four elements in the array.
‫You can iterate through the elements of an array using a for loop or a for each loop or other loop constructs.
‫For example, for every name in those names, I need to print this name.
‫Let me run and here we go.
‫We get all the elements of the array windows, iOS, macOS and Linux.
‫So this is how we iterate through an array.
‫Iterate through all the elements of the array.
‫Another way I can use a for each loop.
‫It's similar to this for loop.
‫So here names dot for each name.
‫Print a Len this name run again.
‫And you can see we looped through this array using the for each loop.
‫And we get all the elements again.
‫Again guys name is a variable that represents the current element being processed in the loop.
‫In each iteration of the loop, the name takes on the value of the current element in the OS names,
‫and between the curly braces, the code inside them is executed for each element in the array, and
‫in this case, I'm printing all the elements, and here this code is the statement inside the loop that
‫prints the current value of name to the console.
‫Moving to the for each loop.
‫Here is the OS names array.
‫This is the array that you want to iterate through for each name and this arrow.
‫This is the start of a lambda expression that takes an element of the array and assign it to the variable
‫name.
‫This lambda expression is a parameter to the for each loop function, and it defines what to do with
‫each element of the array.
‫Println.
‫The name is the action performed on each element, and in this case it prints the name to the console.
‫Later on, when we discuss lambda expressions, we're going to learn about this, but I want from you
‫to understand that we can iterate through the array using the for loop and the for each loop.
‫Also, Kotlin provides several useful functions and operations for working with arrays such as sum,
‫sum function, average function, filter, map, and more.
‫These functions allow you to manipulate and process array elements efficiently.