简体   繁体   English

java add函数,添加后全部成为最后一组数据

[英]java add function,all become one last set of data when added

there are only 3 sets of data in turn1.length,when temp added,there should have three different sets of data.But,after i add,even there is 3 group objects,the date in it upexpectedly is the last group of date,all. 依次只有1组3个数据。长度,在添加temp时,应该有3组不同的数据。但是,在我添加后,即使有3个组对象,其中的日期也可能是最后一组日期,所有。 I see three different data in debug mode,current attribute will cover the prev data in temp each cycle. 我在调试模式下看到三个不同的数据,current属性将覆盖每个周期中temp的上一个数据。

how can i fix it? 我该如何解决?

  String[] turn1 = idList.split(","); 
  String[] turn2 = labelList.split(","); 
  Attribute attribute = new Attribute(); 
  List<Attribute> Temp = new ArrayList<Attribute>(); 

for(int i=0;i<turn1.length;i++){ 
    long getId; 
    getId = Integer.parseInt(turn1[i]); 
    attribute.setId(getId); 
    attribute.setLabel(turn2[i]); 
    Temp.add(attribute); 
   } 
for(int i=0;i<3;i++) 
System.out.println(Temp.get(i)); 

You are changing the same object in the loop, when you add an object to list, this won't copy the object, just reference that object, so every element in the list will be pointed to the same original object. 您正在循环中更改同一对象,将对象添加到列表时,这不会复制该对象,仅引用该对象,因此列表中的每个元素都将指向同一原始对象。

What you should do is new the Attribute inside the loop. 您应该做的是在循环内添加新的Attribute

String[] turn1 = idList.split(","); 
String[] turn2 = labelList.split(","); 
List<Attribute> Temp = new ArrayList<Attribute>(); 

for(int i=0;i<turn1.length;i++){ 
    long getId;
    Attribute attribute = new Attribute(); 
    getId = Integer.parseInt(turn1[i]); 
    attribute.setId(getId); 
    attribute.setLabel(turn2[i]); 
    Temp.add(attribute); 
} 

for(int i=0;i<3;i++) 
System.out.println(Temp.get(i)); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加第一个元素或删除最后一个元素时触发事件的设置 - Set that fires event when first element is added, or last one is removed 使用for循环将对象添加到数组时,最后添加的对象用于所有实例 - When using a for loop to add objects to an array, the last object that is added is used for all instances JTable从下往上添加行-一个接一个添加的行应出现在最后一个的底部,但所有行应在底部对齐 - JTable add rows bottom up - rows added one after the other should appear at the bottom of the last one but all should be bottom aligned (Java)使用for循环获取所有组合并添加到ArrayList中,但是ArrayList仅具有最后一个添加一个 - (Java) Use for loop to get all combinations and add to ArrayList, but ArrayList only have the last add one 从Java中的一个文件夹读取所有文本文件(数据集) - Reading all text files(data set) from one folder in java 最后添加的元素将覆盖列表中的所有对象。 爪哇 - The last element added overwrites all the objects in the List. Java 将值从一个Arraylist复制到另一个时,所有值均设置为在第一个Arraylist中输入的最后一个值 - When copying the values from one Arraylist to another, all values get set as the last value entered in the first Arraylist Java 数组列表覆盖了添加的数据并仅放置最后添加的项目 - Java Array List is overriding the data added and put the last items added only 当订阅者在Mqtt主题Java中处于非活动状态时,如何获取生产者发送的所有数据(未保留或最后一条消息) - How to get all the data sent by producer when subscriber is inactive in Mqtt topic Java(not retained or last message) 设置其中一个属性成为URL(超链接) - Set one of the attribute become an URL (hyperlink)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM