我想做什么:
这里是代码:
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)
问题:
我认为这是发生错误的地方: