【发布时间】:2018-05-02 05:16:18
【问题描述】:
我开始学习更多关于 Java 的知识并被困在这里: 想要制作包含值、索引和休息的对象,并在 main 方法中制作一个对象列表。 通过将新对象添加到列表中,之前的条目将被覆盖。
到目前为止,这是我的代码:
public class LNGHW {
public static int value;
public static int index;
public static int rest;
public LNGHW(int val, int index, int mod) {
this.value = val;
this.index = index;
this.rest = mod;
}
public static void main(String[] args) {
ArrayList<LNGHW> items = new ArrayList<LNGHW>();
items.add(new LNGHW(4, 0, 1));
items.add(new LNGHW(2, 1, 1));
System.out.println(items.get(0).getValue());
System.out.println(items.get(1).getValue());
}
public int getValue() {
return value;
}
public static void setValue(int value) {
LNGHW.value = value;
}
public static int getIndex() {
return index;
}
public static void setIndex(int index) {
LNGHW.index = index;
}
public static int getRest() {
return rest;
}
public static void setRest(int rest) {
LNGHW.rest = rest;
}
}
我愿意接受各种帮助!
【问题讨论】:
-
你的字段是 static 因此行为。从字段以及 getter/setter 中删除 static 修饰符,您应该一切顺利。
标签: java list object arraylist overwrite