Reputation: 177
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...
Upvotes: 0
Views: 3897
Reputation: 177
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.
Upvotes: 0
Reputation: 89
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>>
.
Upvotes: 1
Reputation: 6329
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 Room
s to a direction...
Upvotes: 1
Reputation: 153
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.
Upvotes: 1