简体   繁体   中英

2D Array List of different types in Java

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...

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.

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...

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 (eg 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>> .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM