【问题标题】:Creating ArrayList of objects containing ArrayLists as parameters - my code does not seem to create individual ArrayLists?创建包含 ArrayLists 作为参数的对象的 ArrayList - 我的代码似乎没有创建单独的 ArrayLists?
【发布时间】: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


    【解决方案1】:

    您的代码不完整,问题出在您显示的代码中:您将相同的列表传递给所有构造函数调用,因此所有位置对象对其字段使用相同的列表。

    这可以通过在声明中初始化 Locations 字段来解决,例如:

    private List<Integer> location_characters = new ArrayList<>();
    // repeat this pattern for all List fields
    

    并且通过构造函数传递它们的列表。

    如果需要添加到列表中:

    locationsInstance.getlocation_characters().add(foo);
    

    【讨论】:

    • 我刚刚尝试了一些东西,我在加载它的参数之前添加了一行来创建新的 ArrayList。例如:location_characters = new ArrayList(); for (String character : characters) { 这似乎在做伎俩,但我认为这不是正确的方法。我现在正在尝试您的建议并在静态类“加载器”中使用 getter/setter。
    • 我摆脱了传递参数、初始化私有变量和更改代码以使用 getter/setter。它工作得很好,但是我仍然必须添加 1 行代码来初始化新的 ArrayList,到 ArrayList 类型的每个参数(在解析文本文件的部分中)。所以看起来这两种方法都有效。我的允许通过传递参数在类之外创建对象,而你的需要 getter/setter。我已将您的标记为解决方案!
    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多