创建包含 ArrayList 作为参数的对象的 ArrayList - 我的代码似乎没有创建单独的 ArrayList?
Creating ArrayList of objects containing ArrayLists as parameters - my code does not seem to create individual ArrayLists?
我运行遇到基于文本游戏的代码问题。我的 class 位置旨在从“配置”文本文件加载参数以创建对象。
我目前的做法是:
- 我创建了一个 public class,它带有一个构造函数,该构造函数将采用对象(位置)的参数。它将它们作为 this.xxx 分配给私有变量。
- 我还创建了一个 public 静态 class 来解析文件,一旦它具有创建对象所需的参数量,它就会通过将它们传递给构造函数来创建一个对象。接下来,它将该对象添加到 ArrayList locations_list。生成所有位置对象后,class return 和 ArrayList locations_list
我的静态class解析文本文件OK。但是,当我 运行 一个遍历 locations_list 并为每个元素调用 getter 的测试时,对象的 ArrayList 参数没有个性化。所有 ArrayList 元素 return 相同 locations_characters.
所有对象的location_characters内容相同,是一个包含所有位置“字符”的列表。
例如,如果位置 1 有字符 2 和 3,位置 2 有字符 6 和 7,我的 location_characters 将打印 [2,3,6,7].
如果我放一个 location_characters.clear();添加到 location_list 后,所有对象的 location_characters 变为空。
示例代码片段:
Public class 构造函数:
public class Locations {
private final int location_id;
private final String location_name;
private final String location_description;
private ArrayList <Integer> location_characters;
private ArrayList <Integer> location_items;
private final ArrayList <Integer> location_enter_from;
private final ArrayList <Integer> location_exit_to;
private String location_stage_name;
private final int location_stages;
private final ArrayList <String> location_stage_descriptions;
public Locations(int location_id,
String location_name,
String location_description,
ArrayList <Integer> location_characters,
ArrayList <Integer> location_items,
ArrayList <Integer> location_enter_from,
ArrayList <Integer> location_exit_to,
String location_stage_name,
int location_stages,
ArrayList <String> location_stage_descriptions) {
this.location_id = location_id;
this.location_name = location_name;
this.location_description = location_description;
this.location_characters = location_characters;
this.location_items = location_items;
this.location_enter_from = location_enter_from;
this.location_exit_to = location_exit_to;
this.location_stages = location_stages;
this.location_stage_descriptions = location_stage_descriptions;
}
... below are getters/setters....
Public 加载程序的静态 class:
public static ArrayList <Locations> load_locations() {
//These are used for parsing the text file
String line;
ArrayList <String> lines = new ArrayList<>();
//These are used to initialize local variables
int location_id = 0;
String location_name = null;
String location_description = null;
ArrayList<Integer> location_characters = new ArrayList<>();
ArrayList<Integer> location_items = new ArrayList<>();
ArrayList<Integer> location_enter_from = new ArrayList<>();
ArrayList<Integer> location_exit_to = new ArrayList<>();
String location_stage_name = null;
int location_stages = 0;
ArrayList<String> location_stage_descriptions = new ArrayList<>();
//This is used to initialize ArrayList of objects
ArrayList <Locations> location_list = new ArrayList<>();
//here goes the code for parsing the text files....
//below is a sample portion that loads i.e 2,3,4 split into ints,
//into the ArrayList location_characters....
case "location_characters":
String[] characters = values[1].split(",");
for (String character : characters) {
location_characters.add(Integer.parseInt(character));
}
i++;
break;
//continued...
//below I am passing the parameters into constructor,
//then adding the object to ArrayList location_list
}
Locations location = new Locations(location_id,
location_name,
location_description,
location_characters,
location_items,
location_enter_from,
location_exit_to,
location_stage_name,
location_stages,
location_stage_descriptions);
location_list.add(location);
i = 0;
}
}
reader.close();
}
catch (IOException e){
System.out.println("Problem reading file.");
}
return location_list;
}
我将感谢洞察力和解决方案的建议
您的代码不完整,问题出在您未显示的代码中:您正在传递相同的列表对于所有构造函数调用,因此所有 Locations 对象都为其字段使用相同的列表。
这可以通过在其声明中初始化 Locations 字段来解决,例如:
private List<Integer> location_characters = new ArrayList<>();
// repeat this pattern for all List fields
和不通过构造函数传递列表。
如果您需要添加到列表中:
locationsInstance.getlocation_characters().add(foo);
我运行遇到基于文本游戏的代码问题。我的 class 位置旨在从“配置”文本文件加载参数以创建对象。 我目前的做法是:
- 我创建了一个 public class,它带有一个构造函数,该构造函数将采用对象(位置)的参数。它将它们作为 this.xxx 分配给私有变量。
- 我还创建了一个 public 静态 class 来解析文件,一旦它具有创建对象所需的参数量,它就会通过将它们传递给构造函数来创建一个对象。接下来,它将该对象添加到 ArrayList locations_list。生成所有位置对象后,class return 和 ArrayList locations_list
我的静态class解析文本文件OK。但是,当我 运行 一个遍历 locations_list 并为每个元素调用 getter 的测试时,对象的 ArrayList 参数没有个性化。所有 ArrayList 元素 return 相同 locations_characters.
所有对象的location_characters内容相同,是一个包含所有位置“字符”的列表。
例如,如果位置 1 有字符 2 和 3,位置 2 有字符 6 和 7,我的 location_characters 将打印 [2,3,6,7].
如果我放一个 location_characters.clear();添加到 location_list 后,所有对象的 location_characters 变为空。
示例代码片段:
Public class 构造函数:
public class Locations {
private final int location_id;
private final String location_name;
private final String location_description;
private ArrayList <Integer> location_characters;
private ArrayList <Integer> location_items;
private final ArrayList <Integer> location_enter_from;
private final ArrayList <Integer> location_exit_to;
private String location_stage_name;
private final int location_stages;
private final ArrayList <String> location_stage_descriptions;
public Locations(int location_id,
String location_name,
String location_description,
ArrayList <Integer> location_characters,
ArrayList <Integer> location_items,
ArrayList <Integer> location_enter_from,
ArrayList <Integer> location_exit_to,
String location_stage_name,
int location_stages,
ArrayList <String> location_stage_descriptions) {
this.location_id = location_id;
this.location_name = location_name;
this.location_description = location_description;
this.location_characters = location_characters;
this.location_items = location_items;
this.location_enter_from = location_enter_from;
this.location_exit_to = location_exit_to;
this.location_stages = location_stages;
this.location_stage_descriptions = location_stage_descriptions;
}
... below are getters/setters....
Public 加载程序的静态 class:
public static ArrayList <Locations> load_locations() {
//These are used for parsing the text file
String line;
ArrayList <String> lines = new ArrayList<>();
//These are used to initialize local variables
int location_id = 0;
String location_name = null;
String location_description = null;
ArrayList<Integer> location_characters = new ArrayList<>();
ArrayList<Integer> location_items = new ArrayList<>();
ArrayList<Integer> location_enter_from = new ArrayList<>();
ArrayList<Integer> location_exit_to = new ArrayList<>();
String location_stage_name = null;
int location_stages = 0;
ArrayList<String> location_stage_descriptions = new ArrayList<>();
//This is used to initialize ArrayList of objects
ArrayList <Locations> location_list = new ArrayList<>();
//here goes the code for parsing the text files....
//below is a sample portion that loads i.e 2,3,4 split into ints,
//into the ArrayList location_characters....
case "location_characters":
String[] characters = values[1].split(",");
for (String character : characters) {
location_characters.add(Integer.parseInt(character));
}
i++;
break;
//continued...
//below I am passing the parameters into constructor,
//then adding the object to ArrayList location_list
}
Locations location = new Locations(location_id,
location_name,
location_description,
location_characters,
location_items,
location_enter_from,
location_exit_to,
location_stage_name,
location_stages,
location_stage_descriptions);
location_list.add(location);
i = 0;
}
}
reader.close();
}
catch (IOException e){
System.out.println("Problem reading file.");
}
return location_list;
}
我将感谢洞察力和解决方案的建议
您的代码不完整,问题出在您未显示的代码中:您正在传递相同的列表对于所有构造函数调用,因此所有 Locations 对象都为其字段使用相同的列表。
这可以通过在其声明中初始化 Locations 字段来解决,例如:
private List<Integer> location_characters = new ArrayList<>();
// repeat this pattern for all List fields
和不通过构造函数传递列表。
如果您需要添加到列表中:
locationsInstance.getlocation_characters().add(foo);