Home > other >  Creating ArrayList of objects containing ArrayLists as parameters - my code does not seem to create
Creating ArrayList of objects containing ArrayLists as parameters - my code does not seem to create

Time:02-02

I am running into problem with a code for a text-based game. My class Locations is meant to load parameters from a "config" text file to create objects. My current approach is:

  • I have created a public class with a constructor that will take the parameters of the object (location). It assigns them as this.xxx to private variables.
  • I have also created a public static class that parses the file, and once it has the necessary amount of parameters to create an object, it creates one by passing them to the constructor. Next, it adds that object to an ArrayList locations_list. Once all location objects were generated, the class returns the ArrayList locations_list

My static class parses the text file OK. However, when I run a test which iterates through locations_list and calls the getters for each element, the ArrayList parameters of objects are not individualized. All of the ArrayList elements return the same locations_characters.

The location_characters of all objects will have the same content, which is a list containing "characters" of all locations.
For example, if location 1 has characters 2 and 3 and location 2 has 6 and 7, my location_characters will print [2,3,6,7].
If I put a location_characters.clear(); after adding to location_list, the location_characters becomes empty for all objects.

Sample code snippets:

Public class with a constructor:

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 class for the loader:

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;
    }

I will appreciate insight and pointers to solution

CodePudding user response:

Your code is incomplete, and the problem is in the code you're not showing: You're passing the same lists to all constructor calls, so all Locations objects are using the same lists for their fields.

This can be fixed by initializing Locations fields at their declarations, eg:

private List<Integer> location_characters = new ArrayList<>(); // etc

and not passing them lists through the constructor.

If you need to add to the list:

locationsInstance.getlocation_characters().add(foo);
  •  Tags:  
  • Related