andodeki
03/24/2022, 9:59 PMfun main() {
var newList1 = listOf(listOf(1, "a", "b"), listOf(1, "a", "b"), listOf(1, "a", "b"))
var newList = listOf(1, "b", "C" to newList1)
var defaultList = listOf(1, "a", 'B' to newList)
var a = defaultList[2]
print(a::class.java.typeName) //kotlin.Pair
}
I need to access "newList1" but i cant since "defaultList" is a Pair
how can i access via index or how can i construct to get this behaviour
nkiesel
03/24/2022, 10:41 PM(a as Pair<Char, List<Any>>).second
defaultList
is not a Pair: it is a List<Any>
(in you concrete example, a list consists of an Int, a String, and a Pair of Char and List) . Thus the "static" type of the list entries is Any
(the supertype of all your item types), but individual items have different runtime types (which is why the cast is necessary)andodeki
03/24/2022, 11:06 PMfun main() {
data class Quote(
var int: Int,
var item: String,
var ItemList: Pair<String, List<Any>>
)
var newList1 = listOf(
Quote(1, "asds dd", "sd dsdb" to emptyList()),
Quote(1, "adsd sdssdsds", "bsdds sdd" to emptyList()),
Quote(1, "asdsd sds", "bdsd sd" to emptyList())
)
var newList = listOf(
Quote(1, "bsds sdsds", "Cdfdd fsd" to newList1)
)
var defaultList = listOf(
Quote(1, "aasds ss", "Bdsf wsd" to newList)
)
var a = defaultList[0] //as Pair<Char, List<Any>>).second;
// print(a::class.java.typeName)
print(a.ItemList::class.java.typeName) // kotlin.Pair
}
Richard Gomez
03/24/2022, 11:23 PMkotlin.Pair
because you've defined it as such:
var ItemList: Pair<String, List<Any>>.
andodeki
03/24/2022, 11:35 PMRichard Gomez
03/25/2022, 12:35 AMMap<String, List<Any>>
?Matteo Mirk
03/25/2022, 9:06 AM