0

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer, can i add bitmap data to mapOf array dynamically?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.