在Kotlin中创建Arraylist的Arraylist时索引超出范围异常

问题描述 投票:0回答:1

我想做什么:

  • 我有3个学生的清单
  • 我正在尝试在其中创建一个组(因此第一个组将有2个学生和第二组将有一名学生)

这里是代码:

FragDispGroupBlocksVM.kt

class FragDispGroupBlocksVM @Inject constructor() : ViewModel() {

    private var mStudentList: MutableList<StudentModel> = ArrayList()


    constructor(studentList: MutableList<StudentModel>) : this(){
        mStudentList = studentList
        initStudentGroups()
    }

    fun initStudentGroups() {

        var mStudLstPartn : ArrayList<ArrayList<StudentModel>> = ArrayList()


        var partitionedList = GroupingUtils.partitionIntoSubGroups(mStudentList,2)

        val mList = partitionedList.filterIsInstance<ArrayList<ArrayList<StudentModel>>>()

        mList.forEachIndexed{i,element ->
            mStudLstPartn.add(element[i])
        }



        // mPartitionedList.postValue(mList);
        Timber.i("")
    }

}

GroupingUtils.kt

class GroupingUtils {
    companion object {
        fun <T> partitionIntoSubGroups(
            members: Collection<T>,
            maxSize: Int
        ): MutableList<List<T>> {
            val res: MutableList<List<T>> = ArrayList()
            var internal: MutableList<T> = ArrayList()
            for (member in members) {
                internal.add(member)
                if (internal.size == maxSize) {
                    res.add(internal)
                    internal = ArrayList()
                }
            }
            if (internal.isNotEmpty()) {
                res.add(internal)
            }
            return res
        }
    }
}

StudentModel.kt

data class StudentModel(
    @SerializedName("role")
    val role: String,
    @SerializedName("_id")
    val id: String,
    @SerializedName("firstName")
    val firstName: String,
    @SerializedName("lastName")
    val lastName: String,
    @SerializedName("kneuraId")
    val kneuraId: String,
    @SerializedName("email")
    val email: String,
    var isPresent: Boolean = true
): Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readByte() != 0.toByte()
    )

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest!!.writeString(role)
        dest.writeString(id)
        dest.writeString(firstName)
        dest.writeString(lastName)
        dest.writeString(kneuraId)
        dest.writeString(email)
        dest.writeByte(if (isPresent) 1 else 0)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<StudentModel> {
        override fun createFromParcel(parcel: Parcel): StudentModel {
            return StudentModel(parcel)
        }

        override fun newArray(size: Int): Array<StudentModel?> {
            return arrayOfNulls(size)
        }
    }
}

发生了什么::

下面的行有3个元素

private var mStudentList: MutableList<StudentModel> = ArrayList()

正在运行直到行

val mList = partitionedList.filterIsInstance<ArrayList<ArrayList<StudentModel>>>()

它在第二次迭代中在循环下面的行中崩溃

 mList.forEachIndexed{i,element ->
            mStudLstPartn.add(element[i])
        }

Log:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
        at java.util.ArrayList.get(ArrayList.java:437)
        at com.cnx.project.grouping.viewModels.FragDispGroupBlocksVM.initStudentGroups(FragDispGroupBlocksVM.kt:30)

问题:

  • 为什么存在索引超出范围异常
  • 如何制作类似于java的Arraylist的数组列表
  • 如何解决错误
android generics kotlin
1个回答
0
投票

我认为这是发生错误的地方:

© www.soinside.com 2019 - 2024. All rights reserved.