英文:
How to access to the inner ArrayList<Any> binded by an initial ArrayList of type <Any>
问题
I am trying to access to the inner ArrayList<Any> in Kotlin... Please refer, herein. Is there a way? Many Thanks
fun main() {
val y = ArrayList<Any>()
for (i in 1..2){
y.add(readln().split(' ').map {
try {
it.toInt()
} catch (e: Exception) {
it
}
})
}
println(y[0]::class.simpleName)
println(y[1]::class.simpleName)
println(y)
y.forEach(::println)
}
I printed the class for y[0] and y[1] and the binding class y, all are of type ArrayList. However, accessing to the binding ArrayList is possible by way of lamda expression which shall retrieve the inner ArrayList. The inner list isn't accessible. Is there a way to do it?
Any assistance in this regard is appreciated
英文:
I am trying to access to the inner ArrayList<Any> in Kotlin... Please refer, herein. Is there a way? Many Thanks
fun main() {
val y = ArrayList<Any>()
for (i in 1..2){
y.add(readln().split(' ').map {
try {
it.toInt()
} catch (e: Exception) {
it
}
})
}
println(y[0]::class.simpleName)
println(y[1]::class.simpleName)
println(y)
y.forEach(::println)
}
I printed the class for y[0] and y[1] and the binding class y, all are of type ArrayList. However, accessing to the binding ArrayList is possible by way of lamda expression which shall retrieve the inner ArrayList. The inner list isn't accessible. Is there a way to do it?
Any assistance in this regard is appreciated
答案1
得分: 0
以下是翻译好的内容:
编译器无法知道列表实际上包含内部列表,因为您将ArrayList
声明为Any
类型的元素列表。
但是,您可以让编译器检查列表的元素是否实际上是内部列表。
y.forEach { it ->
// 请注意,您不能检查List<Int>,因为泛型类型在运行时被擦除
if (it is List<*>) {
println(it)
}
}
没有显式检查是不可能的原因是ArrayList<Any>
不是不变的,这意味着您不能保证类型为Any
的对象将具有从List
所需的功能,因为可以将任何对象添加到y
中。
英文:
It is not possible for compiler to know that list is actually containing inner lists, since you declared your ArrayList
as a list of elements of type Any
.
However, you can make compiler check if list's element is actually an inner list.
y.forEach { it ->
// Note that you cannot check for List<Int> since generic types are erased at runtime
if (it is List<*>) {
println(it)
}
}
The reason it is not possible without explicit check is that ArrayList<Any>
is not invariant, meaning you cannot guarantee that object of type Any
will have the functionality required from List
since any object can be added to y
.
答案2
得分: 0
Since you fill your list y
only with lists, the simplest way to make the inner lists accessible as List
would be to provide the type information that y
is a list of List
s and not just a list of Any
s:
val y = ArrayList<List<Any>>()
// now you can also iterate over the inner lists
y.forEach { it.forEach(::println) }
英文:
Since you fill your list y
only with lists, the simplest way to make the inner lists accessible as List
would be to provide the type information that y
is a list of List
s and not just a list of Any
s:
val y = ArrayList<List<Any>>()
// now you can also iterate over the inner lists
y.forEach { it.forEach(::println) }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论