功能Add(添加到列表中)做了一些奇怪的事情

时间:2018-07-13 16:12:31

标签: java jsf

我有一个看似愚蠢的问题,但已经停留了3个小时。当我在arrayList中添加元素时,我不会添加我应该添加的内容。

基本上在Sysout上,您可以看到我应该具有的值,在B上的值上,您可以看到我仅添加了最后一个值...

private List<ContextSchemeValue> ConvertCodeListIntoContextSchemeValue (List<CodeListValue> A){
   ContextSchemeValue C= new ContextSchemeValue();
   List<ContextSchemeValue> B = new ArrayList<>();

   for (int i = 0; i < A.size(); i=i+1) {
         C.setContextScheme(contextScheme);
         C.setMeaning(A.get(i).getName());
         C.setValue(A.get(i).getValue());

         System.out.println("   C"+i+" = "+C);
         B.add(C);
     }
    System.out.println("            B =" + B);
    return B;
}

C0 = ContextSchemeValue {ctxSchemeValueId = 0,guid ='null',value ='fefe',意思是'fefe',contextScheme = ContextScheme {ctxSchemeId = 0,guid ='null',schemeId ='null',schemeName ='null',description ='null',schemeAgencyId ='2',schemeVersionId ='99999',contextCategory = null,createdBy = 0,lastUpdatedBy = 0,creationTimestamp = null,lastUpdateTimestamp = null}}

C1 = ContextSchemeValue {ctxSchemeValueId = 0,guid ='null',value ='efefewfeasf',含义='efewfw',contextScheme = ContextScheme {ctxSchemeId = 0,guid ='null',schemeId ='null',schemeName ='null',description ='null',schemeAgencyId ='2',schemeVersionId ='99999',contextCategory = null,createdBy = 0,lastUpdatedBy = 0,creationTimestamp = null,lastUpdateTimestamp = null}}

C2 = ContextSchemeValue {ctxSchemeValueId = 0,guid ='null',value ='fef',意味着='fwefefef',contextScheme = ContextScheme {ctxSchemeId = 0,guid ='null',schemeId ='null',schemeName ='null',description ='null',schemeAgencyId ='2',schemeVersionId ='99999',contextCategory = null,createdBy = 0,lastUpdatedBy = 0,creationTimestamp = null,lastUpdateTimestamp = null}}

B = [ContextSchemeValue {ctxSchemeValueId = 0,guid ='null',value ='fef',意味着='fwefefef',contextScheme = ContextScheme {ctxSchemeId = 0,guid ='null',schemeId ='null', schemeName ='null',description ='null',schemeAgencyId ='2',schemeVersionId ='99999',contextCategory = null,createdBy = 0,lastUpdatedBy = 0,creationTimestamp = null,lastUpdateTimestamp = null}},

ContextSchemeValue {ctxSchemeValueId = 0,guid ='null',value ='fef',意味着='fwefefef',contextScheme = ContextScheme {ctxSchemeId = 0,guid ='null',schemeId ='null',schemeName =' null',description ='null',schemaAgencyId ='2',schemeVersionId ='99999',contextCategory = null,createdBy = 0,lastUpdatedBy = 0,creationTimestamp = null,lastUpdateTimestamp = null}},

ContextSchemeValue {ctxSchemeValueId = 0,guid ='null',value ='fef',意味着='fwefefef',contextScheme = ContextScheme {ctxSchemeId = 0,guid ='null',schemeId ='null',schemeName =' null',description ='null',schemeAgencyId ='2',schemeVersionId ='99999',contextCategory = null,createdBy = 0,lastUpdatedBy = 0,creatingTimestamp = null,lastUpdateTimestamp = null}}]]

我真的不明白为什么add函数会继续添加最后一个值而不是另一个值,我在每个i上打印了该值,非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

问题是,您要将相同的C对象添加到列表中三次,然后在循环中将同一对象更改三次。该列表不包含三个不同的对象,它包含对同一对象的三个引用。

写时:

C.setValue(A.get(i).getValue())

您要更改C的值,而不是创建新的C。如果您已经在列表中引用了C,则它们也会被更改。

我对ContextSchemeValue并不熟悉,但是您每次都需要创建一个新对象,而不是重复使用同一对象3次,然后将对同一对象的三个引用添加到列表中。