首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用自定义DataType在ArrayList中添加新数据

使用自定义DataType在ArrayList中添加新数据
EN

Stack Overflow用户
提问于 2018-06-04 23:52:21
回答 1查看 602关注 0票数 0

我在使用自定义DataType时遇到了问题。ArrayList的行为是这样的吗?我用CustomDataType类型实例化的ArrayList根据我观察到的行为引用了相同的内存。

例如,我的自定义DataType是:

代码语言:javascript
复制
public class CustomDataType{
     int id; String name; 
     public CustomDataType(int id, String name){...}
} 

我使用这个DataType来存储一些值。

代码语言:javascript
复制
ArrayList<CustomDataType> person = new ArrayList<CustomDataType>(); //1a
CustomDataType cdt = new CustomDataType(12333333,"John Doe");
methodThatDoesSomething(person.add(cdt)); // method that takes ArrayList<E>
person.clear(); // 1b

现在,当我这样做的时候:

代码语言:javascript
复制
ArrayList<CustomDataType> person = new ArrayList<CustomDataType>(); //2a
CustomDataType cdt = new CustomDataType(1231231,"Jane Doe");
methodThatDoesSomething(person.add(cdt));

在本应用程序中,我指的是1a和2a中的对象。当我把名字从无名氏改成无名氏时。我最初的person Arraylist也得到了"Jane Doe“而不是"John Doe”。所以我有两个无名女尸。知道这个引用类型发生了什么吗?我尝试过使用list of Arraylist来解决这个问题,但无论我做什么,引用的都是替换较旧的值。

添加信息:

代码语言:javascript
复制
ArrayList<ActionModel> data = new ArrayList<ActionModel>();
int i = 0;
for (i = 0; i < my_action_items.length(); i++) {
    // parsing the data 
    JSONObject obj = my_action_items.getJSONObject(i);
    String id_main = obj.getString("id");
    int numb_of_step = obj.getInt("numb_of_step");
    String title_main = obj.getString("name");
    JSONObject sub_obj = apiResult.getJSONObject(id_main);
    int j = 0;

    // problem in action: action is alway the same for all cases. 
    ArrayList<SubAction> action = new ArrayList<SubAction>(); // HERE
    action.clear();

    for (j = 0; j < numb_of_step; j++) {
        // data parsing
        String name = sub_obj.getString("" + j);
        Boolean isComplete = sub_obj.getBoolean("completed" + j);
        String id = sub_obj.getString("step_id" + j);
        SubAction subAction = new SubAction(name, id, isComplete);
        action.add(subAction); // adding new data
    }
    // number of action is dedicated by 'my_action_items.length()' but all the data
    // inside the action is copy of last element.
    ActionModel ad = new ActionModel(action, id_main, numb_of_step, title_main, false, false);
    data.add(ad); // instance to data added 
    Boolean flag = action.isEmpty();
}

更多详细信息

代码语言:javascript
复制
// Underlying flow of the code    
ArrayList<ActionModel> data = new ArrayList<ActionModel>();

//------ [1]---
SubAction[] st = null;
SubAction subAction = new SubAction("John Doe", "123123123", true);
st = new SubAction[1];
st[0] = subAction;
ActionModel ad = new ActionModel((st == null) ? st : st, "300", 2, "This is a test"+0, false, false);
data.add(ad);

//-------[2]---

SubAction[] st1 = null;
SubAction subAction1 = new SubAction("Jane Doe", "123123123", true);
st1 = new SubAction[1];
st1[0] = subAction1;
ActionModel ad1 = new ActionModel((st == null) ? st1 : st1, "2", 2, "This is a test" + 1, false, false);
data.add(ad1);

//// SAME ADDRESS //////
SubAction[] actions = data.get(0).getSubAction(); // same address 
SubAction[] anotherActions = data.get(1).getSubAction(); // same address

两个结果都是“无名氏”。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 06:50:50

我在你的代码中看不到根本原因,但我从你的解释中怀疑你从“外部”修改了ArrayList中包含的对象,并惊讶地发现它也在列表中被修改了。

代码语言:javascript
复制
ArrayList<SubAction> subActions = new ArrayList<SubAction>(); 
SubAction subAction= new SubAction("Action name 1", "id1", true);
subActions.add(subAction);

如果您执行以下操作:

代码语言:javascript
复制
System.out.println(subActions.get(0).getName())

你会得到:

操作名称%1

现在,如果你继续:

代码语言:javascript
复制
subAction.setName("Brand new name");
System.out.println(subActions.get(0).getName());

你会得到:

品牌新名称

为什么?

因为在Java语言中,您只处理对object的引用,而subSections列表的第一项和subSection都指向同一个对象!

只有一些额外的附注:

actions).

  • Java命名约定完全没有用,因为您只是实例化了action.clear();,因此action已经是空的。

  • 使用复数(action -> case

  • action.clear(); (id_main -> case)来命名集合是很好的
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50684455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档