[英]Why Extension Functions are not working on ArrayList<ArrayList<Double>>
I am very new to Android development in Kotlin.我对 Kotlin 中的 Android 开发非常陌生。 I have an ArrayList consisting two ArrayLists of Double type.我有一个 ArrayList 包含两个 Double 类型的 ArrayList。 I want to discard/ slice everything after the first element inside the Big ArrayList.我想在 Big ArrayList 内的第一个元素之后丢弃/切片所有内容。 On coming across the properties described here on Kotlin's page, I found some functions like dropLast
, take
etc. However, they don't perform on implementation also there is no error.在遇到 Kotlin 页面上描述的属性时,我发现了一些函数,如dropLast
、 take
等。但是,它们在实现时没有执行,也没有错误。 I am still getting the same output as input having the same length.我仍然得到相同的 output 作为具有相同长度的输入。 Although the functions like add
, get
under Functions
column are working fine.尽管在Functions
列下的add
、 get
等函数运行良好。 I am surely missing something here.我肯定在这里遗漏了一些东西。 What would be the way for achieving this?实现这一目标的方法是什么?
Below is a dummy code:-下面是一个虚拟代码:-
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
}
temp_storage.take(1)
This returns a revised List
.这将返回一个修改后的List
。 It does not modify the List
on which you call it.它不会修改您调用它的List
。 You are ignoring the returned value.您忽略了返回的值。
temp_storage.dropLast(1)
Same — you are ignoring the work that the function is doing.同样——你忽略了 function 正在做的工作。
println("FInal size: "+ temp_storage.size) //still size 2. Why not 1!?
It is the same size because nothing that you did modified it.它的大小相同,因为您没有修改它。
What is the way of achieving this?实现这一目标的方法是什么?
If I am understanding what you want, use:如果我理解你想要什么,请使用:
fun padding(tokenizedinput : ArrayList<ArrayList<Double>>) = arrayListOf(tokenizedinput[0])
Here, we:在这里,我们:
Get the first element of tokenizedinput获取tokenizedinput的第一个元素
Wrap that in an ArrayList
, since you wanted an ArrayList<ArrayList<Double>>
response将其包装在ArrayList
中,因为您需要ArrayList<ArrayList<Double>>
响应
List.take(n)
or List.dropLast(n)
would return
a new list with the operation. List.take(n)
或List.dropLast(n)
将return
一个包含操作的新列表。 It would NOT MODIFY the existing list.它不会修改现有列表。 Try logging or printing this way:-尝试以这种方式记录或打印:-
println(temp_storage.take(1).size) // would be 1
println(temp_storage.dropLast(1).size) // would be 1
The above outputs would be
1
, iff the size of the List is2
如果列表的大小为2
,则上述输出将为1
To convert to the existing list use:-要转换为现有列表,请使用:-
temp_storage = ArrayList(temp_storage.dropLast(1)) // need to cast it to ArrayList<T> before assigning
To add to what the other answers have already said, from the actual class which contains this method:添加到其他答案已经说过的内容,从包含此方法的实际 class :
The take method:取法:
/**
* Returns a list containing first [n] elements.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.collections.Collections.Transformations.take
*/
public fun <T> Iterable<T>.take(n: Int): List<T> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
if (this is Collection<T>) {
if (n >= size) return toList()
if (n == 1) return listOf(first())
}
var count = 0
val list = ArrayList<T>(n)
for (item in this) {
if (count++ == n)
break
list.add(item)
}
return list.optimizeReadOnlyList()
}
and also the dropLast :还有dropLast :
/**
* Returns a list containing all elements except last [n] elements.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.collections.Collections.Transformations.drop
*/
public fun <T> List<T>.dropLast(n: Int): List<T> {
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
which can be found in _Collections.kt
可以在_Collections.kt
中找到
This means that it returns a list, it doesn't modify the original collection这意味着它返回一个列表,它不会修改原始集合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.