【发布时间】:2022-02-01 23:50:19
【问题描述】:
我在使用基于文本的游戏的代码时遇到问题。我的类 Locations 旨在从“配置”文本文件加载参数以创建对象。 我目前的做法是:
- 我创建了一个带有构造函数的公共类,该构造函数将获取对象(位置)的参数。它将它们作为 this.xxx 分配给私有变量。
- 我还创建了一个解析文件的公共静态类,一旦它有必要数量的参数来创建一个对象,它就会通过将它们传递给构造函数来创建一个。接下来,它将该对象添加到 ArrayList 位置列表。生成所有位置对象后,该类返回 ArrayList locations_list
我的静态类解析文本文件OK。但是,当我运行一个遍历 locations_list 并为每个元素调用 getter 的测试时,对象的 ArrayList 参数不是个性化的。所有 ArrayList 元素都返回相同的位置字符。
所有对象的 location_characters 将具有相同的内容,这是一个包含所有位置的“字符”的列表。
例如,如果位置 1 有字符 2 和 3,位置 2 有 6 和 7,我的 location_characters 将打印 [2,3,6,7]。
如果我放一个 location_characters.clear();添加到 location_list 后,所有对象的 location_characters 都为空。
示例代码sn-ps:
带有构造函数的公共类:
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 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;
}
我会欣赏洞察力和解决方案的指针
【问题讨论】:
标签: java object arraylist constructor