Difference between revisions of "KOTLIN: Array"
Onnowpurbo (talk | contribs) (Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_arrays.htm Arrays are used to store multiple items of the same data-type in a single variable, such as an integer or stri...") |
Onnowpurbo (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 7: | Line 7: | ||
Like any other modern programming languages, Kotlin also supports arrays and provide a wide range of array properties and support functions to manipulate arrays. | Like any other modern programming languages, Kotlin also supports arrays and provide a wide range of array properties and support functions to manipulate arrays. | ||
− | Creating Arrays in Kotlin | + | |
+ | |||
+ | Array digunakan untuk menyimpan beberapa item dari tipe data yang sama dalam satu variabel, seperti integer atau string di bawah nama variabel tunggal. | ||
+ | |||
+ | Misalnya, jika kita perlu menyimpan nama 1000 karyawan, maka alih-alih membuat 1000 variabel string yang berbeda, kita cukup mendefinisikan array string yang kapasitasnya menjadi 1000. | ||
+ | |||
+ | Seperti bahasa pemrograman modern lainnya, Kotlin juga mendukung array dan menyediakan berbagai properti array dan fungsi pendukung untuk memanipulasi array. | ||
+ | |||
+ | |||
+ | ==Creating Arrays in Kotlin== | ||
+ | |||
To create an array in Kotlin, we use the arrayOf() function, and place the values in a comma-separated list inside it: | To create an array in Kotlin, we use the arrayOf() function, and place the values in a comma-separated list inside it: | ||
− | val fruits = arrayOf("Apple", "Mango", "Banana", "Orange") | + | Untuk membuat array di Kotlin, kita menggunakan fungsi arrayOf(), dan menempatkan nilainya dalam daftar yang dipisahkan koma di dalamnya: |
+ | |||
+ | val fruits = arrayOf("Apple", "Mango", "Banana", "Orange") | ||
+ | |||
Optionally we can provide a data type as follows: | Optionally we can provide a data type as follows: | ||
− | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") | + | Secara opsional kami dapat menyediakan tipe data sebagai berikut: |
+ | |||
+ | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") | ||
+ | |||
+ | |||
Alternatively, the arrayOfNulls() function can be used to create an array of a given size filled with null elements. | Alternatively, the arrayOfNulls() function can be used to create an array of a given size filled with null elements. | ||
− | Primitive type Arrays | + | Atau, fungsi arrayOfNulls() dapat digunakan untuk membuat array dengan ukuran tertentu yang diisi dengan elemen null. |
+ | |||
+ | |||
+ | |||
+ | ==Primitive type Arrays== | ||
+ | |||
Kotlin also has some built-in factory methods to create arrays of primitive data types. For example, the factory method to create an integer array is: | Kotlin also has some built-in factory methods to create arrays of primitive data types. For example, the factory method to create an integer array is: | ||
− | val num = intArrayOf(1, 2, 3, 4) | + | Kotlin juga memiliki beberapa metode bawaan pabrik untuk membuat array tipe data primitif. Misalnya, metode pabrik untuk membuat array integer adalah: |
+ | |||
+ | val num = intArrayOf(1, 2, 3, 4) | ||
+ | |||
Other factory methods available for creating arrays: | Other factory methods available for creating arrays: | ||
− | byteArrayOf() | + | Metode pabrik lain yang tersedia untuk membuat array: |
+ | |||
+ | byteArrayOf() | ||
+ | charArrayOf() | ||
+ | shortArrayOf() | ||
+ | longArrayOf() | ||
+ | |||
+ | ==Get and Set the Elements of an Array== | ||
− | + | We can access an array element by using the index number inside square brackets. Kotlin array index starts with zero (0). So if you want to access 4th element of the array then you will need to give 3 as the index. | |
− | + | Kita dapat mengakses elemen array dengan menggunakan nomor indeks di dalam tanda kurung siku. Indeks array Kotlin dimulai dengan nol (0). Jadi jika Anda ingin mengakses elemen ke-4 dari array maka Anda harus memberikan 3 sebagai indeks. | |
− | |||
− | + | ===Example=== | |
− | |||
− | + | fun main(args: Array<String>) { | |
− | fun main(args: Array<String>) { | + | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") |
− | |||
− | |||
− | |||
− | |||
− | } | + | println( fruits [0]) |
+ | println( fruits [3]) | ||
+ | |||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Apple | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Orange | + | |
+ | Apple | ||
+ | Orange | ||
+ | |||
Kotlin also provides get() and set() member functions to get and set the value at a particular index. Check the following example: | Kotlin also provides get() and set() member functions to get and set the value at a particular index. Check the following example: | ||
− | Example | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | + | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") | |
− | + | println( fruits.get(0)) | |
− | + | println( fruits.get(3)) | |
− | + | ||
− | } | + | // Set the value at 3rd index |
+ | fruits.set(3, "Guava") | ||
+ | println( fruits.get(3)) | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Apple | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Orange | + | |
− | Guava | + | Apple |
− | Kotlin Array Length | + | Orange |
+ | Guava | ||
+ | |||
+ | ==Kotlin Array Length== | ||
+ | |||
Kotlin provides array property called size which returns the size i.e. length of the array. | Kotlin provides array property called size which returns the size i.e. length of the array. | ||
− | Example | + | Kotlin menyediakan properti array yang disebut size yang mengembalikan ukuran yaitu panjang array. |
− | fun main(args: Array<String>) { | + | |
− | + | ||
− | + | ===Example=== | |
− | + | ||
+ | fun main(args: Array<String>) { | ||
+ | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") | ||
+ | |||
+ | println( "Size of fruits array " + fruits.size ) | ||
+ | |||
+ | } | ||
− | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Size of fruits array 4 | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
+ | |||
+ | Size of fruits array 4 | ||
+ | |||
We can also use count() member function to get the size of the array. | We can also use count() member function to get the size of the array. | ||
− | Loop Through an Array | + | Kita juga bisa menggunakan fungsi anggota count() untuk mendapatkan ukuran array. |
+ | |||
+ | ==Loop Through an Array== | ||
+ | |||
We can use for loop to loop through an array. | We can use for loop to loop through an array. | ||
− | Example | + | Kita dapat menggunakan for loop untuk mengulang melalui array. |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | + | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") | |
− | |||
− | } | + | for( item in fruits ){ |
+ | println( item ) | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Apple | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Mango | + | |
− | Banana | + | Apple |
− | Orange | + | Mango |
− | Check if an Element Exists | + | Banana |
+ | Orange | ||
+ | |||
+ | ==Check if an Element Exists== | ||
+ | |||
We can use the in operator alongwith if...else to check if an element exists in an array. | We can use the in operator alongwith if...else to check if an element exists in an array. | ||
− | Example | + | Kita dapat menggunakan operator in bersama if...else untuk memeriksa apakah ada elemen dalam array. |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | + | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") | |
− | |||
− | |||
− | |||
− | } | + | if ("Mango" in fruits){ |
+ | println( "Mango exists in fruits" ) | ||
+ | }else{ | ||
+ | println( "Mango does not exist in fruits" ) | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Mango exists in fruits | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Distinct Values from Array | + | |
+ | Mango exists in fruits | ||
+ | |||
+ | ==Distinct Values from Array== | ||
+ | |||
Kotlin allows to store duplicate values in an array, but same time you can get a set of distinct values stored in the array using distinct() member function. | Kotlin allows to store duplicate values in an array, but same time you can get a set of distinct values stored in the array using distinct() member function. | ||
− | Example | + | Kotlin memungkinkan untuk menyimpan nilai duplikat dalam sebuah array, tetapi pada saat yang sama Anda bisa mendapatkan satu set nilai berbeda yang disimpan dalam array menggunakan fungsi anggota yang berbeda(). |
− | fun main(args: Array<String>) { | + | |
− | + | ||
− | + | ||
− | + | ===Example=== | |
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | + | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange", "Apple") | |
− | } | + | |
+ | val distinct = fruits.distinct() | ||
+ | for( item in distinct ){ | ||
+ | println( item ) | ||
+ | } | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Apple | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Mango | + | |
− | Banana | + | Apple |
− | Orange | + | Mango |
− | Dropping Elements from Array | + | Banana |
+ | Orange | ||
+ | |||
+ | ==Dropping Elements from Array== | ||
+ | |||
We can use drop() or dropLast() member functions to drop elements from the beginning or from the last respectively. | We can use drop() or dropLast() member functions to drop elements from the beginning or from the last respectively. | ||
− | Example | + | Kita dapat menggunakan fungsi anggota drop() atau dropLast() untuk menjatuhkan elemen dari awal atau dari yang terakhir. |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | + | val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange", "Apple") | |
− | + | ||
− | + | val result = fruits.drop(2) // drops first two elements. | |
− | } | + | for( item in result ){ |
+ | println( item ) | ||
+ | } | ||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | Banana | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Orange | + | |
− | Apple | + | Banana |
− | Checking an Empty Array | + | Orange |
+ | Apple | ||
+ | |||
+ | ==Checking an Empty Array== | ||
+ | |||
We can use isEmpty() member function to check if an array is empty or not. This function returns true if the array is empty. | We can use isEmpty() member function to check if an array is empty or not. This function returns true if the array is empty. | ||
− | Example | + | Kita dapat menggunakan fungsi anggota isEmpty() untuk memeriksa apakah array kosong atau tidak. Fungsi ini mengembalikan nilai true jika array kosong. |
− | fun main(args: Array<String>) { | + | |
− | + | ===Example=== | |
− | + | ||
− | + | fun main(args: Array<String>) { | |
− | } | + | val fruits = arrayOf<String>() |
+ | println( "Array is empty : " + fruits.isEmpty()) | ||
+ | |||
+ | } | ||
+ | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
− | "Array is empty : true | + | Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut: |
− | Quiz Time (Interview & Exams Preparation) | + | |
+ | |||
+ | "Array is empty : true | ||
+ | |||
+ | ==Quiz Time (Interview & Exams Preparation)== | ||
+ | |||
Q 1 - Which of the following is true about Kotlin Arrays? | Q 1 - Which of the following is true about Kotlin Arrays? | ||
Latest revision as of 10:38, 29 July 2022
Sumber: https://www.tutorialspoint.com/kotlin/kotlin_arrays.htm
Arrays are used to store multiple items of the same data-type in a single variable, such as an integer or string under a single variable name.
For example, if we need to store names of 1000 employees, then instead of creating 1000 different string variables, we can simply define an array of string whose capacity will be 1000.
Like any other modern programming languages, Kotlin also supports arrays and provide a wide range of array properties and support functions to manipulate arrays.
Array digunakan untuk menyimpan beberapa item dari tipe data yang sama dalam satu variabel, seperti integer atau string di bawah nama variabel tunggal.
Misalnya, jika kita perlu menyimpan nama 1000 karyawan, maka alih-alih membuat 1000 variabel string yang berbeda, kita cukup mendefinisikan array string yang kapasitasnya menjadi 1000.
Seperti bahasa pemrograman modern lainnya, Kotlin juga mendukung array dan menyediakan berbagai properti array dan fungsi pendukung untuk memanipulasi array.
Creating Arrays in Kotlin
To create an array in Kotlin, we use the arrayOf() function, and place the values in a comma-separated list inside it:
Untuk membuat array di Kotlin, kita menggunakan fungsi arrayOf(), dan menempatkan nilainya dalam daftar yang dipisahkan koma di dalamnya:
val fruits = arrayOf("Apple", "Mango", "Banana", "Orange")
Optionally we can provide a data type as follows:
Secara opsional kami dapat menyediakan tipe data sebagai berikut:
val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")
Alternatively, the arrayOfNulls() function can be used to create an array of a given size filled with null elements.
Atau, fungsi arrayOfNulls() dapat digunakan untuk membuat array dengan ukuran tertentu yang diisi dengan elemen null.
Primitive type Arrays
Kotlin also has some built-in factory methods to create arrays of primitive data types. For example, the factory method to create an integer array is:
Kotlin juga memiliki beberapa metode bawaan pabrik untuk membuat array tipe data primitif. Misalnya, metode pabrik untuk membuat array integer adalah:
val num = intArrayOf(1, 2, 3, 4)
Other factory methods available for creating arrays:
Metode pabrik lain yang tersedia untuk membuat array:
byteArrayOf() charArrayOf() shortArrayOf() longArrayOf()
Get and Set the Elements of an Array
We can access an array element by using the index number inside square brackets. Kotlin array index starts with zero (0). So if you want to access 4th element of the array then you will need to give 3 as the index.
Kita dapat mengakses elemen array dengan menggunakan nomor indeks di dalam tanda kurung siku. Indeks array Kotlin dimulai dengan nol (0). Jadi jika Anda ingin mengakses elemen ke-4 dari array maka Anda harus memberikan 3 sebagai indeks.
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") println( fruits [0]) println( fruits [3]) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Apple Orange
Kotlin also provides get() and set() member functions to get and set the value at a particular index. Check the following example:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") println( fruits.get(0)) println( fruits.get(3)) // Set the value at 3rd index fruits.set(3, "Guava") println( fruits.get(3)) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Apple Orange Guava
Kotlin Array Length
Kotlin provides array property called size which returns the size i.e. length of the array.
Kotlin menyediakan properti array yang disebut size yang mengembalikan ukuran yaitu panjang array.
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") println( "Size of fruits array " + fruits.size ) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Size of fruits array 4
We can also use count() member function to get the size of the array.
Kita juga bisa menggunakan fungsi anggota count() untuk mendapatkan ukuran array.
Loop Through an Array
We can use for loop to loop through an array.
Kita dapat menggunakan for loop untuk mengulang melalui array.
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") for( item in fruits ){ println( item ) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Apple Mango Banana Orange
Check if an Element Exists
We can use the in operator alongwith if...else to check if an element exists in an array.
Kita dapat menggunakan operator in bersama if...else untuk memeriksa apakah ada elemen dalam array.
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange") if ("Mango" in fruits){ println( "Mango exists in fruits" ) }else{ println( "Mango does not exist in fruits" ) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Mango exists in fruits
Distinct Values from Array
Kotlin allows to store duplicate values in an array, but same time you can get a set of distinct values stored in the array using distinct() member function.
Kotlin memungkinkan untuk menyimpan nilai duplikat dalam sebuah array, tetapi pada saat yang sama Anda bisa mendapatkan satu set nilai berbeda yang disimpan dalam array menggunakan fungsi anggota yang berbeda().
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange", "Apple") val distinct = fruits.distinct() for( item in distinct ){ println( item ) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Apple Mango Banana Orange
Dropping Elements from Array
We can use drop() or dropLast() member functions to drop elements from the beginning or from the last respectively.
Kita dapat menggunakan fungsi anggota drop() atau dropLast() untuk menjatuhkan elemen dari awal atau dari yang terakhir.
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange", "Apple") val result = fruits.drop(2) // drops first two elements. for( item in result ){ println( item ) } }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
Banana Orange Apple
Checking an Empty Array
We can use isEmpty() member function to check if an array is empty or not. This function returns true if the array is empty.
Kita dapat menggunakan fungsi anggota isEmpty() untuk memeriksa apakah array kosong atau tidak. Fungsi ini mengembalikan nilai true jika array kosong.
Example
fun main(args: Array<String>) { val fruits = arrayOf<String>() println( "Array is empty : " + fruits.isEmpty()) }
When you run the above Kotlin program, it will generate the following output:
Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:
"Array is empty : true
Quiz Time (Interview & Exams Preparation)
Q 1 - Which of the following is true about Kotlin Arrays?
A - Kotlin arrays are capable to store all data types.
B - Kotlin arrays can be created using either arrayOf() or arrayOfNulls() functions.
C - Kotlin provides a rich set of properties and functions to manipulate arrays.
D - All of the above
Q 2 - What will be the output of the following code segment?
fun main(args: Array<String>) {
val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")
println( fruits [2])
} A - Apple
B - Mango
C - Banana
D - None of the above
Q 3 - How to get the size of a Kotlin array?
A - Using length() function
B - Using size property
C - Using count() function
D - B and C both