如何访问由初始类型为<Any>的ArrayList绑定的内部ArrayList?

huangapple go评论67阅读模式
英文:

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&lt;Any&gt;()
    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&#39;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&lt;Any&gt;()
	for (i in 1..2){
		y.add(readln().split(&#39; &#39;).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 -&gt;
    // Note that you cannot check for List&lt;Int&gt; since generic types are erased at runtime
    if (it is List&lt;*&gt;) { 
        println(it)
    }
}

The reason it is not possible without explicit check is that ArrayList&lt;Any&gt; 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 Lists and not just a list of Anys:

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 Lists and not just a list of Anys:

val y = ArrayList&lt;List&lt;Any&gt;&gt;()
// now you can also iterate over the inner lists
y.forEach { it.forEach(::println) }

huangapple
  • 本文由 发表于 2023年5月18日 01:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定