如何创建数据类成员变量的数组列表

How to create an array list of a member variable of a dataclass

我有一个数据类:

data class MoviesInSeries(
    val originalMovieName: String,

// If there's no value of parameter, assign it as null
    val movieInSeries1Name: String? = null,
    val movieInSeries1Date: String? = null,
    val movieInSeries1Rating: String? = null,
    val movieInSeries1Pic: Int? = null,

    val movieInSeries2Name: String? = null,
    val movieInSeries2Date: String? = null,
    val movieInSeries2Rating: String? = null,
    val movieInSeries2Pic: Int? = null,
    )

我已经为它创建了两个对象。

fun getRestOfSeriesMovies(): ArrayList<MoviesInSeries> {
    val movieList = ArrayList<MoviesInSeries>()

    val s_gi_joe = MoviesInSeries("G.I. Joe: Retaliation", "G.I. Joe: The Rise of Cobra",
        "2009","Pg-13",  R.drawable.gijtsofmp)
    movieList.add(s_gi_joe)

    val s_gi_joe2 = MoviesInSeries("G.I. Joe: Retaliation2", "G.I. Joe: The Rise of Cobra",
        "2009","Pg-13",  R.drawable.gijtsofmp)
    movieList.add(s_gi_joe)


    return movieList
}

现在我想创建一个 originalMovieName 成员的数组列表。我稍后会用它来检查某个字符串是否在其中。我该怎么做?

你的模型中发生了很多奇怪的事情class :

1-

checkIFMovieNameExist(getRestOfSeriesMovies())
fun checkIFMovieNameExist(list: List<MoviesInSeries>,value:String): MoviesInSeries? {
    return list.find { it.originalMovieName == value }
}

2-从创建时间开始:

data class MoviesInSeries(
    val originalMovieName: String,

// If there's no value of parameter, assign it as null
    val movieInSeries1Name: String? = null,
    val movieInSeries1Date: String? = null,
    val movieInSeries1Rating: String? = null,
    val movieInSeries1Pic: Int? = null,

    val movieInSeries2Name: String? = null,
    val movieInSeries2Date: String? = null,
    val movieInSeries2Rating: String? = null,
    val movieInSeries2Pic: Int? = null,
){
    init {
        names.add(originalMovieName)
    }
companion object {
    private val names: MutableList<String> = mutableListOf()
    fun getMoviesNames():List<String>{
        return names
    }
}
}

然后你可以在代码的任何地方检查名称列表中是否存在名称

MoviesInSeries("sd1")
MoviesInSeries("sd2")
MoviesInSeries("sd3")
MoviesInSeries("sd4")
println(MoviesInSeries. getMoviesNames().contains("sd1"))