Home > other >  How to print an arrayList using a variable containing the arrayList name
How to print an arrayList using a variable containing the arrayList name

Time:11-24

I know this sounds a bit stupid but I am extending a text-based game and I'm trying to figure out a way to print out the items that are in the room. I have made an arrayList for the room and added and "Item" into it by using a separate class called Item.

import java.util.ArrayList;
import java.util.List;
public class Item
{
    private String itemName;
    private Boolean pickUp;
    private Integer itemWeight;

    /**
     * Constructor for objects of class Items
     */
    public Item(String itemName, Boolean pickUp, Integer itemWeight)
    {
        this.itemName = itemName;
        this.pickUp = pickUp;
        this.itemWeight = itemWeight;
    }

    /**
     * Returns the name of the item
     */
    public String getItemName()
    {
        return itemName;
    }
    
    /**
     * Returns the pickUp ability of the item
     */
    public Boolean getPickUp()
    {
        return pickUp;
    }
    
    /**
     * Returns the weight of the item
     */
    public Integer getItemWeight()
    {
        return itemWeight;
    }
}

The part I'm stuck on now is printing those elements out when entering the room. Theres a function in the code that keeps track of the current room and I am trying to use that function to then print out the elements of that corresponding rooms arrayList. Each room in the game is an object from the "Room" class.

public class Room  {
    private String description;
    private String listName;
    private HashMap<String, Room> exits;        // stores exits of this room.

    public Room(String description, String listName) 
    {
        this.description = description;
        this.listName = listName;
        exits = new HashMap<>();
    }

    public String getListName()
    {
        return listName;
    }
    private void createRooms()
    {
        Room playground, assemblyHall, groundHallway, lunchHall, ITRoom, groundToilets, headteacherOffice, footballCage, stairs, groundClassroom, firstFloorClassroom, exitGate;
      
        // create the rooms
        assemblyHall = new Room("in the assembly hall", "assemblyHallList");
        playground = new Room("in the main playground of the school", "playgroundList");
        .......
        currentRoom = assemblyHall;
        ArrayList<Item> assemblyHallList = new ArrayList<Item>();
        assemblyHallList.add(new Item("Piece of paper", true, 1));

These last 2 lines are an example of what I will be doing for every room with more items. I feel like I am going about this the wrong way but this was the only idea I had. The problem I have is printing out the elements when I enter a room as I would need to call on the method to get the name of the arrayList then use that name to then access the list and print out the elements in it but I have no idea how to use the string to accesss the array as I would have to use

currentRoom.getListName()

to get the name of the list but when I try to print it out it prints the name itself, not the elements. Any help is appreciated and I can send more parts of the class if needed. Didn't include it all as its very big and a lot of it is irrelevant to my problem

CodePudding user response:

You create a ArrayList<Item> assemblyHallList, then you create

Room assemblyHall = new Room("in the assembly hall", "assemblyHallList");

And now you expect that room to have some reference to the List.

That's not how this works. The Room now has the String value "assemblyHallList" in the listName field. But there is no reference to the List. And since assemblyHallList is a local variable in your createRooms method, there's no way of resolving it.

The Solution is this: Don't assign the name of the List variable to the Room (why would that work?), instead assign the List itself:

List<Item> assemblyHallList = ...;
Room assemblyHall = new Room("in the assembly hall", assemblyHallList);

Of course you need to change the type of the constructor's parameter and Room's listName field to List<Item>.

CodePudding user response:

To answer your question, you can't just directly access a variable from a string containing its name. Technically, you could make a static Hashmap<String, ArrayList<Item>> to keep track of all the lists and access them by their name.

However, I don't see why you couldn't just make the list itself part of the constructor and then store the list as an instance variable of the Room.

Something like this would work better:

ArrayList<Item> items = new ArrayList<>();
items.add(new Item("Piece of paper", true, 1));
Room room = new Room("best room", items);
public class Room  {
    private String description;
    private ArrayList<Item> items;
    private HashMap<String, Room> exits;

    public Room(String description, ArrayList<Item> items) 
    {
        this.description = description;
        this.items = items;
        exits = new HashMap<>();
    }

    // Other members not shown
}
  • Related