简体   繁体   English

如何从 ArrayList 中获取已移除项目的 position?

[英]How to get the position of removed item from ArrayList?

I have an ArrayList type of ImagesLoaderItem .我有一个 ArrayList 类型的ImagesLoaderItem I removed the item from ArrayList using the element imagesLoaderItem my question is that I want to get the position of an element that is removed.我使用元素imagesLoaderItem从 ArrayList 中删除了该项目我的问题是我想获取已删除元素的 position。

Following is my code以下是我的代码

var loadingImagesList: ArrayList<ImagesLoaderItem> = getAllImages()
val imagesLoaderItem:ImagesLoaderItem = ...
selectedImagesList.remove(imagesLoaderItem)

You can get the index of the item by indexOf method for example例如,您可以通过indexOf方法获取项目的索引

val index = selectedImagesList.indexof(imagesLoaderItem)

And you can create extension to remove the item and return the index of it您可以创建扩展来删除该项目并返回它的索引

fun <T: Comparable<T>> ArrayList<T>.removeWithIndex(element : T) : Int {
    val index = this.indexOf(element)
    this.remove(element)
    return index
}

And use it like this并像这样使用它

val index = selectedImagesList.removeWithIndex(imagesLoaderItem)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM