Arrays the beginners and intermediate.pptx

ZSNQuest 4 views 22 slides Aug 02, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Arrays and programming


Slide Content

Arrays

outline Introduction Features Basic concepts Creating / Declaring & Initializing Reading/Accessing & Writing / Updating Array Traversing Array Methods

Array To store multiple items under a single variable name Multiple items has to be of the same data-type, such as an integer or string Used to organize data in programming so that a related set of values can be easily sorted or searched . 1 Normal Variable 50 60 70 55 67 Array Data Structure Index 1 2 3 4

Features of array in kotlin Stored in contiguous memory locations Accessed via index Mutable Fixed Size Not native data type to Kotlin, but a mutable collection using Array class 50 60 70 55 67 Array Data Structure Index 1 2 3 4

Creating / Declaring an Array Using functions arrayOf (), arrayOfNulls () Other functions such as : intArrayOf (), byteArrayOf () , charArrayOf (), shortArrayOf (), longArrayOf () Using the Array constructor

Creating / Declaring & Initializing an Array Using array functions arrayOf () arrayOfNulls () // Creates an array with values [1, 2, 3] val simpleArray = arrayOf (1, 2, 3) println ( simpleArray.joinToString ()) // 1, 2, 3 val num = arrayOf (1, 2, 3, 4) //implicit type declaration val num = arrayOf <Int>(1, 2, 3) //explicit type declaration // Declaring an array of integers val numbers: IntArray = intArrayOf (1, 2, 3, 4, 5) //faster // Creates an array with values [null, null, null] val nullArray : Array<Int?> = arrayOfNulls (3) println ( nullArray.joinToString ()) // null, null, null

Creating / Declaring & Initializing an Array Using Array Constructor Array< dataType >() The constructor takes two parameters 1) The Size of an Array 2) value /a function i.e. lambda expression to return values val my Array = Array<Int>(3) { 0 } println ( my Array.joinToString ()) // 0, 0, 0 fun main() { val arrayname = Array(5, { i -> i * 1 }) for ( i in 0..arrayname.size-1) { println ( arrayname [ i ]) } } Output: 1 2 3 4

Reading / Accessing & Modifying / Updating Arrays Using get() & set Methods/functions Using index [ ] operator

Reading / Accessing & Modifying / Updating Arrays Using get() & Set() Methods get() The get() method takes a single parameter — the index of the element and returns the value of the item at that index. Syntax : array_name.get(index_value) Example val x = myArray1.get(0) set() The set() method takes 2 parameters: the index of the element and the value to be inserted. Syntax: Array_name.set ( index_value , new_value ) Example myArray1 .set(1, 3) // sets the value of the second element in the array to 3

Reading / Accessing & Modifying / Updating Arrays Using index [ ] operator The [ ] operator can be used to access and modify arrays. To Access an array element: Syntax: Array_name [ index_value ] Example: val x = myArray1[1] This will assign the value of the second element in num to x. To modify an array element: Syntax: Array_Name [ index_value ] = new_value Example: num[2] = 5; This will change the value of the third element in the num array to 5.

Array Methods/Functions Array size/length Size To find out how many elements an array have, use the size property: Example val cars = arrayOf ("Volvo", "BMW", "Ford", "Mazda") println ( cars.size ) // Outputs 4

Traversing array elements One important property of an array is that it can be traversed programmatically, and each element in the array can be manipulated individually. Kotlin supports few powerful ways to traverse array. The simplest and most commonly used idiom when it comes to traversing an array is to use the for loop .

Traversing array elements Using For Loop // Traversing an array using fun main() { val num = arrayOf <Int>(1, 2, 3, 4, 5) num.set (0, 10) num.set (1, 6) for ( i in num.indices ){ println (num[ i ]) } } Output : 10 6 3 4 5 // Traversing an array using Loop Through fun main(){ val cars = arrayOf ("Volvo", "BMW", "Ford", "Mazda") for (x in cars) { println (x) } }

Traversing array elements Using For Loop & Range In Kotlin, a range is an interval between two values (start and end) and can be created using the (..) operator. Traversal through the range can be then done using the in keyword. // Traversing an array using Range fun main() { val arrayname = arrayOf <Int>(1, 2, 3, 4, 5) for ( i in 0..arrayname.size-1){ println ( arrayname [ i ]) } } Output : 1 2 3 4 5

Traversing array elements Using Foreach Loop // Traversing an array using foreach loop fun main() { val arrayname = arrayOf <Int>(1, 2, 3, 4, 5) arrayname.forEach ({ index -> println (index) }) } Output : 1 2 3 4 5

Array Methods/Functions plus() Adding an element to the end of the Array dropLast () Removing the last item from the Array Arrays in Kotlin have a fixed size , so you can't directly push or pop elements like in some other languages. However, you can achieve similar functionality by using specialized functions or by creating a custom data structure. fun main() { // declare an array using arrayOf () val num = arrayOf (1, 2, 3, 4, 5) num.set (0, 10) // set the first element equal to 10 num.set (1, 6) // set the secondelement equal to 6 println ( num.get (0)) // print the first element using get() println (num[1]) // print the second element using [ ] } Output: 10 6 fun main() { val numbers = intArrayOf (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val shortenedNumbers = numbers.dropLast (1) println ( shortenedNumbers.joinToString (", ")) } // output // 1, 2, 3, 4, 5, 6, 7, 8, 9

Array Methods/Functions Find an item in an array You can use the in operator to check if a value exists in an array Example: fun main(){ val cars = arrayOf ("Volvo", "BMW", "Ford", "Mazda") if ("Volvo" in cars) { println ("It exists!") } else { println ("It does not exist.") } }

Array Methods/Functions Sum of array items Kotlin has many useful functions to transform arrays. sum() : To return the sum of all elements in an array, use the .sum() function: val sumArray = arrayOf (1, 2, 3) // Sums array elements println ( sumArray.sum ()) // 6 Note: The .sum() function can only be used with arrays of numeric data types, such as Int. average() : Returns an average value of elements in the array. val my Array = arrayOf (1, 2, 3) // Averages array elements println ( my Array. average ()) // 2

Array Methods/Functions Distinct items of array distinct() : To return the sum of all elements in an array, use the .sum() function: fun main(){ val myArray = arrayOf (1, 2, 3, 2, 3) // Returns distinct elements println ( myArray.distinct ()) // [1,2,3] }

Array Methods/Functions Shuffle array items shuffle() : To randomly shuffle the elements in an array, use the .shuffle() function: val simpleArray = arrayOf (1, 2, 3) // Shuffles elements [3, 2, 1] simpleArray.shuffle () println ( simpleArray.joinToString ()) // Shuffles elements again [2, 3, 1] simpleArray.shuffle () println ( simpleArray.joinToString ())

Array Methods/Functions convert arrays to collections Convert to List or Set : To convert an array to a List or Set, use the . toList () and . toSet () functions. val simpleArray = arrayOf ("a", "b", "c", "c") // Converts to a Set println ( simpleArray.toSet ()) // [a, b, c] // Converts to a List println ( simpleArray.toList ()) // [a, b, c, c]

Review Introduction Features Basic concepts Creating / Declaring & Initializing Reading/Accessing & Writing / Updating Array Traversing Array Methods Sum, distinct, shuffle, size
Tags