An array is a collection of similar data types either of Int, String, etc. Array in Kotlin is mutable in nature with fixed size which means we can perform both read and write operations, on the elements of an array. Arrays in Kotlin are able to store multiple values of different data types.
AndroidMobile DevelopmentApps/ApplicationsKotlin. In Kotlin, "!!" is an operator that is known as the double-bang operator. This operator is also known as "not-null assertion operator". This operator is used to convert any value to a non-NULL type value and it throws an exception if the corresponding value is NULL.
To convert a string to byte array in Kotlin, use String. toByteArray() method. String. toByteArray() method returns a Byte Array created using the characters of the calling string.
There's no special case for String
, because String
is an ordinary referential type on JVM, in contrast with Java primitives (int
, double
, ...) -- storing them in a reference Array<T>
requires boxing them into objects like Integer
and Double
. The purpose of specialized arrays like IntArray
in Kotlin is to store non-boxed primitives, getting rid of boxing and unboxing overhead (the same as Java int[]
instead of Integer[]
).
You can use Array<String>
(and Array<String?>
for nullables), which is equivalent to String[]
in Java:
val stringsOrNulls = arrayOfNulls<String>(10) // returns Array<String?>
val someStrings = Array<String>(5) { "it = $it" }
val otherStrings = arrayOf("a", "b", "c")
See also: Arrays in the language reference
use arrayOf, arrayOfNulls, emptyArray
var colors_1: Array<String> = arrayOf("green", "red", "blue")
var colors_2: Array<String?> = arrayOfNulls(3)
var colors_3: Array<String> = emptyArray()
To create an empty Array of Strings in Kotlin you should use one of the following six approaches:
First approach:
val empty = arrayOf<String>()
Second approach:
val empty = arrayOf("","","")
Third approach:
val empty = Array<String?>(3) { null }
Fourth approach:
val empty = arrayOfNulls<String>(3)
Fifth approach:
val empty = Array<String>(3) { "it = $it" }
Sixth approach:
val empty = Array<String>(0, { _ -> "" })
Those types are there so that you can create arrays of the primitives, and not the boxed types. Since String isn't a primitive in Java, you can just use Array<String>
in Kotlin as the equivalent of a Java String[]
.
For Strings
and other types, you just use Array<*>
.
The reason IntArray
and others exist is to prevent autoboxing.
So int[]
relates to IntArray
where Integer[]
relates to Array<Int>
.
Some of the common ways to create a String array are
This will create an array of 5 strings with initial values to be empty string.
<String?>
(5)This will create an array of size 5 with initial values to be null. You can use String data to modify the array.
When you know the contents of array already then you can initialise the array directly.
There is an easy way for creating an multi dimensional array of strings as well.
var matrix = Array(5){Array(6) {""}}
This is how you can create a matrix with 5 rows and 6 columns with initial values of empty string.
you can use too:
val frases = arrayOf("texto01","texto02 ","anotherText","and ")
for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With