我在使用自定义DataType时遇到问题。 ArrayList的行为是这样的吗?我根据我观察到的行为,使用类型CustomDataType的ArrayList实例化引用相同的内存。
例如, 我的自定义DataType是:
public class CustomDataType{
int id; String name;
public CustomDataType(int id, String name){...}
}
我使用此DataType存储一些值。
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
现在我做的时候:
ArrayList<CustomDataType> person = new ArrayList<CustomDataType>(); //2a
CustomDataType cdt = new CustomDataType(1231231,"Jane Doe");
methodThatDoesSomething(person.add(cdt));
在本申请中,我指的是1a和2a中的一个对象。当我将名字从John Doe改为Jane Doe时。我最初的人Arraylist也得到了&#34; Jane Doe&#34;而不是&#34; John Doe&#34;。所以,我有两个Jane Doe。知道这个引用类型发生了什么。我已经尝试使用Arraylist列表来解决这个问题,但无论我做什么引用都在替换旧值。
添加信息:
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();
}
更多细节
// 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
两个结果&#34; Jane Doe&#34;。
答案 0 :(得分:0)
我没有在您提供的代码中看到根本原因,但我怀疑您从解释的内容中修改了ArrayList
中包含的对象&#34; outside&#34;并且很惊讶它也在列表中进行了修改。
ArrayList<SubAction> subActions = new ArrayList<SubAction>();
SubAction subAction= new SubAction("Action name 1", "id1", true);
subActions.add(subAction);
如果您执行以下操作:
System.out.println(subActions.get(0).getName())
你得到:
行动名称1
现在,如果你继续:
subAction.setName("Brand new name");
System.out.println(subActions.get(0).getName());
你得到:
全新名称
为什么?
因为在Java中你只处理对象的引用,而subSections
列表的第一项和subSection
都指向同一个对象!
只是一些附加的评论:
action.clear();
完全没用,因为你刚刚实现了action
,因此已经空了。action
- &gt; actions
)命名集合很好。id_main
- &gt; idMain
)