Reputation: 37
I want to make a array in array and get one by index form in Kotlin.
for example, I make a this array [ (1, 12(real data is Bitmap)) , (2, 24(same)), (3, 36) ]
so I can get array(index) = 12
how can I make this form of array and get data by index like above?
Upvotes: 0
Views: 832
Reputation: 29844
If you just want an array of bytes, use byteArrayOf
:
val array = byteArrayOf(12, 24, 36)
println(array[0]) // 12
ByteArray
is the equivalent of Java's byte[]
.
Note: There is also intArrayOf
, floatArrayOf
, doubleArrayOf
etc.
Since you asked for an array in an array as well:
val arrayOfArrays = arrayOf(byteArrayOf(1, 2, 3), byteArrayOf(24), byteArrayOf(36))
println(arrayOfArrays[0][1]) // 2
In this case the type of arrayOfArrays
will be Array<ByteArray>
and you need arrayOf
to construct that.
Upvotes: 0
Reputation: 30605
Maybe Map
is what you need:
val map = mapOf(1 to 12, 2 to 24, 3 to 36)
val twelve = map[1]
It is a collection that holds pairs of objects (keys and values) and supports efficiently retrieving the value corresponding to each key.
To add data to a map we can use mutableMapOf
function:
val map = mutableMapOf<Int, Bitmap>()
val bitmap: Bitmap = ...
map[4] = bitmap
Upvotes: 1