0

So this is something that has me a little stumped. I'm trying to make an array list that holds objects, and each object will have a string associated with it.

For an example lets say I have this array list of adjacent rooms...

ArrayList<Object> adjacentRooms = new ArrayList<Object>();

I could add Room objects to that array list that are adjacent to whichever subject.. however, when I add to the array list with adjacentRooms.add(x); (where x could be an object Room type).. I would also like to add a string to that position. For example adjacentRooms.add(x, "north");.. <- now I know that that is not possible unless I do something like a 2D array list possibly?

So after some time researching I am at a loss. I just can't quite figure out how to add an object with an associated string in a 2D array list...

1
  • Create your own class. Commented Oct 13, 2017 at 1:29

4 Answers 4

1

Instead of a List use a Map: That can "map" a value to another, and store it in a collection.
Something like this:

Map<String, Room> adjacentRooms = new HashmMap<>();

adjacentRooms.put("north", room);
adjacentRooms.get("east");

You may want to use constants, to make sure the values are "discrete".

It has a drawback, tho: it cannot assign more than 1 value to a key, that is more than 1 Rooms to a direction...

Sign up to request clarification or add additional context in comments.

1 Comment

Coming back to this question after sometime and I implemented this is a similar situation. Even with the drawback, I prefer doing this over making an entire separate class like I did in my answer ( link ). This is a quick simple easy to use solution to the question that I overlooked. Thanks.
1

An ArrayList can only hold one data type. But I'm curious as to why you cant associate a string as a member in the Object you're talking about. Since you want a 2d arraylist, I'm assuming the string and "room" are related

Object foo = new Object();
foo.data = "your string"
adjacentRooms.add(foo);

access by

adjacentRooms.get(index).data

However, if you must, you can do a 2d ArrayList, but they get annoying ArrayList<ArrayList<String> > list = new ArrayList();

access would be something like list.get(i).get(k) with 'i' referring to the index of ArrayList of Strings, and k referring to the index of a String in that 'i' ArrayList.

However, that structure does not store the "Object" you're talking about...

Hope that helps.

3 Comments

This was helpful, thank you. However my reasoning is because each room is being manipulated by many different objects. I was thinking it could be difficult for each room to have a method that says the direction it is in relative to each subject.
yea that is a head scratcher, maybe you'll have to assign x,y coordinates to each room as data members. Then you could just compare the coords of the two rooms in question.
Yes, each room has an x and a y. Each entity as well. They are then compared in the entity class. If there is a room found adjacent to the entity.. well thats where I was having problems structuring.. If interested you can view my answer link
1

The "array" in ArrayList describes the way the list is implemented.

A List is always one-dimensional. When you need more dimensions, use objects in your list that can store additional information.

So either create a new class that holds your data (e.g. DetailedRoom with members Room and a String) or use an existing collection class. The latter would be a poor design, but still... it could be List for instance, so that you end up with List<List<Object>> .

2 Comments

link This is the class I ended up creating.
yep. thats the way to go.
0

If you are to create your own class.. this is what I did...

public class AdjacentRoom {
    private Room room;
    private String direction;
    public AdjacentRoom(Room room, String direction) {
        this.room = room;
        this.direction = direction;
    }
    public Room getRoom(){
        return room;
    }
    public String getDirection(){
        return direction;
    }
}

Also for sake of example here is a bare bones room class...

public class Room {
    private String name;
    public Room(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}

What this all allows me to do is ...

//Firstly i can create a room...
Room testRoom = new Room("Test Room");
//If i change my array list in the question to an AdjacentRoom list...
ArrayList<AdjacentRoom> adjacentRooms = new ArrayList<AdjacentRoom>();
//I can add rooms and strings to it like this..
adjacentRooms.add(new AdjacentRoom(testRoom, "north"));

Now I can still access each of the rooms methods while also printing each string or 'direction' associated to each room.. For example..

for(AdjacentRoom room : adjacentRooms){
        System.out.println(room.getRoom().getName() + " " + room.getDirection());
}

While this is a cool and customization solution, using a map like in Usagi Miyamoto's answer ( link ) is great for this situation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.