Skip to content

Commit 4d66b21

Browse files
committed
Arrays lesson & examples added
1 parent 84d8ffa commit 4d66b21

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/Arrays.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
fun main() {
2+
3+
/*
4+
Array is to store multiple items of the same data-type,such as an integer or string under a single variable name.
5+
6+
Arrays are used to organize data in programming so that a related set of values can be easily sorted or searched.
7+
8+
- They're stored in contiguous memory locations.
9+
- They can be accessed programmatically through their indexes (array[1], array[0], etc.)
10+
- They're mutable.
11+
- Their size is fixed.
12+
13+
*/
14+
15+
16+
val numbers = arrayOf(1, 9, 8, 3) // -> implicit type declaration
17+
// val numbers= arrayOf<Int>(1,9,8,3) -> Remove explicit type arguments
18+
19+
println("Numbers[0] = ${numbers[0]}")
20+
21+
// numbers[4] = 0 -> The size of the array is determined at first, so no new elements are added.
22+
23+
24+
/*
25+
26+
Kotlin also has some built-in factory methods to create arrays of primitive data types,
27+
such as byteArray, intArray, shortArray, etc.
28+
These classes do not extend the Array class; however, they implement the same methods and properties.
29+
30+
*/
31+
32+
33+
// charArray
34+
val chars = charArrayOf('a','b','c')
35+
36+
37+
// Using the for loop to show values on the screen
38+
for (i in chars.indices){
39+
println(chars[i])
40+
}
41+
42+
43+
}

0 commit comments

Comments
 (0)