为什么扩展函数不适用于 ArrayList<ArrayList<Double>>

我对 Kotlin 的 Android 开发非常陌生。我有一个 ArrayList,由两个 Double 类型的 ArrayList 组成。我想丢弃/切片 Big ArrayList 中第一个元素之后的所有内容。在遇到Kotlin 页面上描述的属性时,我发现了一些函数,例如dropLasttake但是,它们在实现上不执行,也没有错误。我仍然得到与具有相同长度的输入相同的输出。add尽管, getunder column等功能Functions运行良好。我肯定在这里遗漏了一些东西。实现这一目标的方法是什么?

下面是一个虚拟代码:-

fun padding(tokenizedinput : ArrayList<ArrayList<Double>>) : ArrayList<ArrayList<Double>> {

    var temp_storage = tokenizedinput // of size 2

    temp_storage.take(1) // OPTION 1. Only want to have first element in this ArrayList

    temp_storage.dropLast(1) // OPTION 2. Only want to drop 1 element from the end

    println("FInal size: "+ temp_storage.size) //still size 2. Why not 1!?

    return temp_storage

}


眼眸繁星
浏览 62回答 3
3回答

湖上湖

temp_storage.take(1)这将返回修改后的List.&nbsp;它不会修改List您调用它的位置。您忽略了返回值。temp_storage.dropLast(1)同样的——你忽略了该函数正在做的工作。println("FInal&nbsp;size:&nbsp;"+&nbsp;temp_storage.size)&nbsp;//still&nbsp;size&nbsp;2.&nbsp;Why&nbsp;not&nbsp;1!?它的大小相同,因为您没有对它进行任何修改。实现这一目标的方法是什么?如果我明白你想要什么,请使用:fun&nbsp;padding(tokenizedinput&nbsp;:&nbsp;ArrayList<ArrayList<Double>>)&nbsp;=&nbsp;arrayListOf(tokenizedinput[0])在这里,我们:获取 tokenizedinput 的第一个元素将其包含在 中ArrayList,因为您想要ArrayList<ArrayList<Double>>回复

一只萌萌小番薯

List.take(n)或者List.dropLast(n)将return使用该操作创建一个新列表。它不会修改现有列表。尝试以这种方式记录或打印:-println(temp_storage.take(1).size)&nbsp;//&nbsp;would&nbsp;be&nbsp;1 println(temp_storage.dropLast(1).size)&nbsp;//&nbsp;would&nbsp;be&nbsp;1上面的输出将是1,当且仅当列表的大小是2要转换为现有列表,请使用:-temp_storage&nbsp;=&nbsp;ArrayList(temp_storage.dropLast(1))&nbsp;//&nbsp;need&nbsp;to&nbsp;cast&nbsp;it&nbsp;to&nbsp;ArrayList<T>&nbsp;before&nbsp;assigning

紫衣仙女

要添加其他答案已经说过的内容,请从包含此方法的实际类中添加:采取方法:/**&nbsp;* Returns a list containing first [n] elements.&nbsp;*&nbsp;&nbsp;* @throws IllegalArgumentException if [n] is negative.&nbsp;*&nbsp;&nbsp;* @sample samples.collections.Collections.Transformations.take&nbsp;*/public fun <T> Iterable<T>.take(n: Int): List<T> {&nbsp; &nbsp; require(n >= 0) { "Requested element count $n is less than zero." }&nbsp; &nbsp; if (n == 0) return emptyList()&nbsp; &nbsp; if (this is Collection<T>) {&nbsp; &nbsp; &nbsp; &nbsp; if (n >= size) return toList()&nbsp; &nbsp; &nbsp; &nbsp; if (n == 1) return listOf(first())&nbsp; &nbsp; }&nbsp; &nbsp; var count = 0&nbsp; &nbsp; val list = ArrayList<T>(n)&nbsp; &nbsp; for (item in this) {&nbsp; &nbsp; &nbsp; &nbsp; if (count++ == n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; list.add(item)&nbsp; &nbsp; }&nbsp; &nbsp; return list.optimizeReadOnlyList()}&nbsp;还有dropLast:/**&nbsp;* Returns a list containing all elements except last [n] elements.&nbsp;*&nbsp;&nbsp;* @throws IllegalArgumentException if [n] is negative.&nbsp;*&nbsp;&nbsp;* @sample samples.collections.Collections.Transformations.drop&nbsp;*/public fun <T> List<T>.dropLast(n: Int): List<T> {&nbsp; &nbsp; require(n >= 0) { "Requested element count $n is less than zero." }&nbsp; &nbsp; return take((size - n).coerceAtLeast(0))}可以在以下位置找到:_Collections.kt这意味着它返回一个列表,它不会修改原始集合
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java