我正在尝试从可变列表中进行打印,但一直越界越多。我将成员添加到底部的菜单中,然后每次尝试从列表成员中打印某些内容时,都会出现错误。这些成员是存储为列表的类的对象。我认为这与我编写列表的方式有关,但我无法弄清楚。
我尝试以不同的方式将成员添加到列表中,但这会导致相同的错误。
class membership ()
{
var number: Int? = null
var name: String? = null
var address: String? = null
var zip: String? = null
var phone: String? = null
var memberSince: String? = null
var memberType: Char? = null
}
fun main(args: Array<String>) {
var Members: MutableList<membership> = mutableListOf()
var member1 = membership()
member1.number = 1
member1.name = "George Jetson"
member1.address ="123 Main St."
member1.zip = "99207"
member1.memberSince = "12/01/1997"
member1.memberType = 'L'
Members[0] = member1
println(Members[0].name)
}
// I have more members.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck (ArrayList.java:653)
at java.util.ArrayList.set (ArrayList.java:444)
at FileKt.main (File.kt:45)
答案 0 :(得分:1)
如果您之前未设置项目0,我会说您不能分配它。 member1需要添加
Members.add(member1)