Updated April 6, 2023
Introduction to Kotlin array
The kotlin array is one of the collection types, and that it is similar to the other types like Int, String, char, etc. and also the array is the mutable one, and it is fixed-size arrays are not a native data type and which is represented by the Array class its also invariant one kotlin provides the built-in array method that can be provided to the enumerated values into the array methods like arrayOf() with primitive values that can be mapped to the corresponding wrapper object classes the array values may be of null to restrict and hold the array values of the specific data type.
Syntax
The kotlin language used many data types, variables, keywords, and other packages like; that array is one of the important packages of creating, storing, and retrieving the datas from both the front end and back end. It has different methods for implementing the application, which has array-based.
fun main(args: Array<String>)
{
val first=arrayOf(values)
val second=arrayOf<data type>(values)
----some coding logics depends on the requirement---
}
The above code is the basic syntax for creating and initializing the arrays on the memory. By using some default methods, we can call and operate the input datas.
How does array work in Kotlin?
The array is one of the main features in the programming language. We can store and retrieve the data by using these array formats; it has many types like primitive arrays etc. Here in kotlin, we can define the array with two different approaches like one is using the arrayOf() method, and another thing is Array is one of the classes, so we can call the constructor to create the array. This constructor has passing two different parameters like the size of the array and the function or method, which accepts the specific index of the given elements, and returns the initial values of those elements in the memory.
Kotlin also statically typed languages like java, and the type of the variable is mainly identified with the compile-time mechanism. The array values are stored with the index format; it can be started with the 0-based index. For each value, the unique index will be used to retrieve the accurate data, and also we can overwrite the values with the built-in methods. The index value will not be changed; it will Overwrite the values on the memory location. We also restrict the value access in the array of specific data types.
Examples of Kotlin array
Below are the different examples of Kotlin array:
Example #1
Code:
package one;
fun main(args : Array<String>){
println("Welcome To My Domain its a First Example Related to the Kotlin Array concepts")
var new = arrayOf(1, "Siva", "Chennai", 'I')
println(new[0])
new[0] = 210
println(new[0])
var new1 = arrayOf(2, "Raman", "Tiruppur", 'N')
println(new1[1])
new1[1] = "Address"
println(new1[1])
var new2 = arrayOf(3, "Sivaraman", "Chennai", 'D')
println(new2[2])
new2[2] = 12753
println(new2[2])
var new3 = arrayOf(4, "Arun", "Madurai", 'I')
println(new3[3])
new3[3] = "city"
println(new3[3])
var new4 = arrayOf(5, "Kumar", "Chennai", 'A')
println(new4[0])
new4[0] = 't'
println(new4[0])
var new5=arrayOf("first", "second", "third", "four", "five")
println(new5[2])
new5[2]="six"
println(new5[2])
var new6=arrayOf(123, 456789, 10, 11, 1233456)
println(new6[4])
new6[4]=72356
println(new6[4])
println("Thank you users for spenting your time on our application have a nice day users please keep on try again with our apps")
}
Output:
In the above example, we used some basic array declaration for the input data types like both string and integer.
Example #2
Code:
package one;
import java.util.Arrays
fun main(args: Array<String>) {
val eg1 = doubleArrayOf(12.34, -53264.2, 32.3, 34.5, 35.5, 33.7, 36.8, -39.9, 40.1, -41.2)
var bignum = eg1[0]
for (demo in eg1) {
if (bignum < demo)
bignum = demo
}
println("Thank you for spenting the time with our application please find the biggest number of your inputs is = %.2f".format(bignum))
val first = arrayOf(23.34, 35.46, 32.7, 39.89, 43.8, 74.3, 82.1, 62.3, 71.5, 83.2, 101.9,31.3)
val second = first.max()
println("Thank you for spenting the time with our application please find the maximum number of your inputs is:" +second)
val str1 = arrayOf("ID", "Name Siva", "Raman Chennai", "TamilNadu India", "Your fifth input")
val result = "Name"
val output = Arrays.stream(str1).anyMatch { st -> st == result }
if (output)
println("Please find your input strings $result is found.")
else
println("Please try again your matched input strings $result is not found.")
}
Output:
In the second example, we calculated the maximum and largest numbers by using the built-in method called max(). We can calculate the string also contains; if the input string is matched with the output string, it shows found else its shows not found on the screen.
Example #3
Code:
package one;
import java.util.Arrays
fun demo(a: IntArray, arryindx: Int): IntArray {
if (arryindx < 0 || arryindx >= a.size) {
return a
}
val out = a.toMutableList()
out.removeAt(arryindx)
return out.toIntArray()
val outp = IntArray(a.size - 1)
System.arraycopy(a, 0, outp, 0, arryindx)
System.arraycopy(a, arryindx + 1, outp, arryindx, a.size - arryindx - 1)
return outp
}
fun main() {
println("Welcome To My Domain its a third example for regarding kotlin array concept")
var b: IntArray = intArrayOf(123, 456, 789, 1112, 1213, 1415, 1617,1819, 2021)
val arryindx = 4
b = demo(b, arryindx)
println(b.contentToString())
val dem = arrayOf("your first input","your second input",3,"your fourth input", "your fifth input", 6, "your seventh input", 8, "your ninth input", "your tenth input")
val dem1 = arrayOf<String>("Your eleventh input","Your twelth input","Your thirteen input","Your fourteen input", "Your fifteen input")
println(dem.get(2))
println(dem[1])
println("Thank you users for spenting time with our application kindly keep and sepnt more time for our application and support it for further feature")
println(dem1.get(2))
println(dem1[3])
}
Output:
In the final example, we used the concept called removing the specific array elements on the list. It removes only the value the index will be kept on the same memory location. The input arrays are denoted using the toMutableList() method because it cant change the value.
Conclusion
The array is mutable, and it is one of the important concepts for the application. It is one of the ways to store and retrieve the elements on the user screen. It has n number of types, and each type with different recursive in the application based on the requirements.
Recommended Articles
This is a guide to Kotlin array. Here we discuss the introduction, syntax, and working of an array in Kotlin along with different examples and its code implementation. You may also have a look at the following articles to learn more –