PeakGen
PeakGen

Reputation: 22995

How to create 2 dimensional List

This is my situation. I have 8 zones. Consider them as rooms in a building. each zone got number of cameras which i don't know. for an example, zone1 might have 6 cams, zone2 might have 3 cams etc. now i need to track these camera objects which is inside these zones. i thought of adding 2 dimensional list because them the first dimension can contain the zone while the second dimension can contain the camera. how can i do this?

anyway, if you feel there is a better way, please let me know

Upvotes: 1

Views: 144

Answers (6)

Vishal K
Vishal K

Reputation: 13066

If Zone and Camera are itself class then You could use HashMap as follows:

Map<Zone,ArrayList<Camera>> map = new HashMap<Zone,ArrayList<Camera>>();
ArrayList<Camera> list1 = new ArrayList<Camera>();
list1.add(camera1);list1.add(camera2);
map.put(zone1,list1);
ArrayList<Camera> list2 = new ArrayList<Camera>();
 list2.add(camera1);list2.add(camera2);list2.add(camera3);
map.put(zone2,list2);
....
...

Where camera1,camera2 ..so on are objects of Camera. And zone1, zone2 .. so on are objects of Zone
And if you want to add more camera say for zone1 then you can proceed as follows:

ArrayList<Camera> list = map.get(zone1);
list.add(camera3);list.add(camera4);
map.put(zone1,list);

Upvotes: 1

crstamps2
crstamps2

Reputation: 614

How about create a Zone object (especially helpful if a zone will ever have something else in it). Then in the constructor just pass the number of cameras it has.

public class Zone {

    private int numOfCameras;
    private int zoneNumber;

    public Zone(_numOfCameras, _zoneNumber) {
        numOfCameras = _numOfCameras;
        zoneNumber = _zoneNumber;
    }

    //getters and setters
    ...
}

If you need to keep a list of zones you can just instantiate an array of zones and add it to the array:

private Zone[] zones;

To find out how many cameras in a zone you do something like this (this assumes zones will numbered 1-8 in order):

public int getNumberOfCamerasByZone(i){ //pass in the zone number you are wanting
    return zones[i].getNumberofCameras();
}

Upvotes: 0

jedwards
jedwards

Reputation: 30200

Well, one approach would be to use Maps and Lists:

HashMap<Zone, ArrayList<Camera>> building = new HashMap<>();

ArrayList<Camera> zone1cameras = new ArrayList<>();
zone1cameras.add(camera1);
zone1cameras.add(camera2);
...

building.put(zone, zone1cameras);

But if you already have Zone objects, why not something like

public class Zone
{
    // Members
    private ArrayList<Camera> cameras = new ArrayList<>();

    // Methods 
    public void registerCamera(Camera c){ cameras.add(c); }
    public List<Camera> getCameras(){ return cameras; }
}

And then you could store the Zones in another List, eg:

ArrayList<Zone> zones = new ArrayList<>();
zones.add(zone1);

If you wanted to get crazy, you could chain the registerCamera methods like:

public class Zone
{
    // Members
    private ArrayList<Camera> cameras = new ArrayList<>();

    // Methods 
    public Zone registerCamera(Camera c){ cameras.add(c); return this; }
    public List<Camera> getCameras(){ return cameras; }
}

Which would allow you to do something like

ArrayList<Zone> zones = new ArrayList<>();
zones.add(new Zone().registerCamera(cam1).registerCamera(cam2).registerCamera(cam3));

Upvotes: 1

giorashc
giorashc

Reputation: 13713

Why not having a class Zone with number of cameras as a member ?

public class Zone {

    private int numOfCameras;

    public Zone(int numOfCameras) {
        this.numOfCameras = numOfCameras;
    }  
}

And then you can easily create a list of zones :

List<Zone> zonesList = new ArrayList<Zone>();
zoneList.add(new Zone(6);
zoneList.add(new Zone(8);
.
.

etc..

I prefer this than a multi list since you can easily add more properties to a zone object and access them from your list.

You can also have a name/id property to a zone object and map zones by id in a map;

Upvotes: 1

Jean Waghetti
Jean Waghetti

Reputation: 4727

You can create a HashMap of Lists, with the key being Zone.

HashMap<Zone, List<Camera>>

Upvotes: 1

matt
matt

Reputation: 1984

Use a HashMap where the keys are the Zones and the values are a List of the Cameras.

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

Upvotes: 1

Related Questions