<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=KOTLIN%3A_Array</id>
	<title>KOTLIN: Array - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=KOTLIN%3A_Array"/>
	<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;action=history"/>
	<updated>2026-04-10T08:20:29Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.4</generator>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;diff=65949&amp;oldid=prev</id>
		<title>Onnowpurbo at 03:38, 29 July 2022</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;diff=65949&amp;oldid=prev"/>
		<updated>2022-07-29T03:38:43Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;amp;diff=65949&amp;amp;oldid=65887&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;diff=65887&amp;oldid=prev</id>
		<title>Onnowpurbo at 23:31, 22 July 2022</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;diff=65887&amp;oldid=prev"/>
		<updated>2022-07-22T23:31:28Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;amp;diff=65887&amp;amp;oldid=65792&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;diff=65792&amp;oldid=prev</id>
		<title>Onnowpurbo: Created page with &quot;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...&quot;</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=KOTLIN:_Array&amp;diff=65792&amp;oldid=prev"/>
		<updated>2022-07-18T01:25:59Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;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...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Sumber: https://www.tutorialspoint.com/kotlin/kotlin_arrays.htm&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Like any other modern programming languages, Kotlin also supports arrays and provide a wide range of array properties and support functions to manipulate arrays.&lt;br /&gt;
&lt;br /&gt;
Creating Arrays in Kotlin&lt;br /&gt;
To create an array in Kotlin, we use the arrayOf() function, and place the values in a comma-separated list inside it:&lt;br /&gt;
&lt;br /&gt;
val fruits = arrayOf(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
Optionally we can provide a data type as follows:&lt;br /&gt;
&lt;br /&gt;
val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
Alternatively, the arrayOfNulls() function can be used to create an array of a given size filled with null elements.&lt;br /&gt;
&lt;br /&gt;
Primitive type Arrays&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
val num = intArrayOf(1, 2, 3, 4)&lt;br /&gt;
Other factory methods available for creating arrays:&lt;br /&gt;
&lt;br /&gt;
byteArrayOf()&lt;br /&gt;
&lt;br /&gt;
charArrayOf()&lt;br /&gt;
&lt;br /&gt;
shortArrayOf()&lt;br /&gt;
&lt;br /&gt;
longArrayOf()&lt;br /&gt;
&lt;br /&gt;
Get and Set the Elements of an Array&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
   println( fruits [0])&lt;br /&gt;
   println( fruits [3])&lt;br /&gt;
   &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Apple&lt;br /&gt;
Orange&lt;br /&gt;
Kotlin also provides get() and set() member functions to get and set the value at a particular index. Check the following example:&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
   println( fruits.get(0))&lt;br /&gt;
   println( fruits.get(3))&lt;br /&gt;
   &lt;br /&gt;
   // Set the value at 3rd index&lt;br /&gt;
   fruits.set(3, &amp;quot;Guava&amp;quot;)&lt;br /&gt;
   println( fruits.get(3)) &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Apple&lt;br /&gt;
Orange&lt;br /&gt;
Guava&lt;br /&gt;
Kotlin Array Length&lt;br /&gt;
Kotlin provides array property called size which returns the size i.e. length of the array.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
   println( &amp;quot;Size of fruits array &amp;quot; + fruits.size )&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Size of fruits array 4&lt;br /&gt;
We can also use count() member function to get the size of the array.&lt;br /&gt;
&lt;br /&gt;
Loop Through an Array&lt;br /&gt;
We can use for loop to loop through an array.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
   for( item in fruits ){&lt;br /&gt;
      println( item )&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Apple&lt;br /&gt;
Mango&lt;br /&gt;
Banana&lt;br /&gt;
Orange&lt;br /&gt;
Check if an Element Exists&lt;br /&gt;
We can use the in operator alongwith if...else to check if an element exists in an array.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
   if (&amp;quot;Mango&amp;quot; in fruits){&lt;br /&gt;
      println( &amp;quot;Mango exists in fruits&amp;quot; )&lt;br /&gt;
   }else{&lt;br /&gt;
      println( &amp;quot;Mango does not exist in fruits&amp;quot; )&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Mango exists in fruits&lt;br /&gt;
Distinct Values from Array&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;, &amp;quot;Apple&amp;quot;)&lt;br /&gt;
   &lt;br /&gt;
   val distinct = fruits.distinct()&lt;br /&gt;
   for( item in distinct ){&lt;br /&gt;
      println( item )&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Apple&lt;br /&gt;
Mango&lt;br /&gt;
Banana&lt;br /&gt;
Orange&lt;br /&gt;
Dropping Elements from Array&lt;br /&gt;
We can use drop() or dropLast() member functions to drop elements from the beginning or from the last respectively.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;, &amp;quot;Apple&amp;quot;)&lt;br /&gt;
   &lt;br /&gt;
   val result = fruits.drop(2) // drops first two elements.&lt;br /&gt;
   for( item in result ){&lt;br /&gt;
      println( item )&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Banana&lt;br /&gt;
Orange&lt;br /&gt;
Apple&lt;br /&gt;
Checking an Empty Array&lt;br /&gt;
We can use isEmpty() member function to check if an array is empty or not. This function returns true if the array is empty.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;()&lt;br /&gt;
   println( &amp;quot;Array is empty : &amp;quot; + fruits.isEmpty())&lt;br /&gt;
   &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Array is empty : true&lt;br /&gt;
Quiz Time (Interview &amp;amp; Exams Preparation)&lt;br /&gt;
Q 1 - Which of the following is true about Kotlin Arrays?&lt;br /&gt;
&lt;br /&gt;
A - Kotlin arrays are capable to store all data types.&lt;br /&gt;
&lt;br /&gt;
B - Kotlin arrays can be created using either arrayOf() or arrayOfNulls() functions.&lt;br /&gt;
&lt;br /&gt;
C - Kotlin provides a rich set of properties and functions to manipulate arrays.&lt;br /&gt;
&lt;br /&gt;
D - All of the above&lt;br /&gt;
&lt;br /&gt;
Q 2 - What will be the output of the following code segment?&lt;br /&gt;
&lt;br /&gt;
fun main(args: Array&amp;lt;String&amp;gt;) {&lt;br /&gt;
   val fruits = arrayOf&amp;lt;String&amp;gt;(&amp;quot;Apple&amp;quot;, &amp;quot;Mango&amp;quot;, &amp;quot;Banana&amp;quot;, &amp;quot;Orange&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
   println( fruits [2])&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
A - Apple&lt;br /&gt;
&lt;br /&gt;
B - Mango&lt;br /&gt;
&lt;br /&gt;
C - Banana&lt;br /&gt;
&lt;br /&gt;
D - None of the above&lt;br /&gt;
&lt;br /&gt;
Q 3 - How to get the size of a Kotlin array?&lt;br /&gt;
&lt;br /&gt;
A - Using length() function&lt;br /&gt;
&lt;br /&gt;
B - Using size property&lt;br /&gt;
&lt;br /&gt;
C - Using count() function&lt;br /&gt;
&lt;br /&gt;
D - B and C both&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Referensi==&lt;br /&gt;
&lt;br /&gt;
* https://www.tutorialspoint.com/kotlin/kotlin_arrays.htm&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>