我的ArrayList仅包含阵列中的最后一项

我的ArrayList仅包含阵列中的最后一项,第1张

我的ArrayList仅包含阵列中的最后一项

问题是您在

Student
课程中的字段
static
。您尚未显示代码,但可以这样猜测:

public class Student {    private static int id;    //other fields...    //constructor...    //getters and setters...}

只需

static
从此类的字段中删除标记即可。

public class Student {    //field must not be static    private int id;    //other non-static fields...    //constructor...    //getters and setters...}

请注意,删除class

static
字段中的标记 不会解决问题*
Converter
*

简而言之,

static
字段属于类,而非
static
字段属于类对象引用。如果
static
您的
Student
类中包含字段,则所有对象引用将共享该值。当具有非
static
字段时,由于它们属于该类的每个实例,因此每个对象引用_将_ 具有不同的值。

更多信息:了解实例和类成员



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5500167.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存