【发布时间】:2021-11-12 01:17:24
【问题描述】:
我有一个从 api 提取的数据列表。但是,我需要对此列表(movieList)进行更改。我需要将索引 0 处的元素与索引 1 处的元素交换。例如:
列表[0] = 电影A,
列表[1] = 电影B
然后
列表[0] = 电影 B,
列表[1] = 电影A
我打算做这些操作的类如下:
data class MovieListDto(
val docs: List<Movie>,
val limit: Int,
val offset: Int,
val page: Int,
val pages: Int,
val total: Int
)
fun MovieListDto.MovieListDtoToMovieList(): List<Movie> {
val movieList = mutableListOf<Movie>()
for (movie in docs) {
if (movie._id == "5cd95395de30eff6ebccde5c" ||
movie._id == "5cd95395de30eff6ebccde5b" ||
movie._id == "5cd95395de30eff6ebccde5d"
) {
movieList.add(movie)
}
}
return movieList
}
我该怎么做?
【问题讨论】:
标签: list kotlin mutablelist