0

I was hoping someone could tell me if I'm even going about this the right way, or if what I'm doing is even possible.

My end goal is to create a program where you can create cabins, create campers, and assign them to a cabin.

What I thought of was allowing the user to create a new ArrayList by having a method called that does such. I would create a Camper class where it has the variables such as name, age, gender, etc...

Then I would write a method that looked something like this....

      public static void userCreatesList(){


        ArrayList<Camper> cabin = new ArrayList<Camper>();


        }

The problem is, the method works when I add items to the list and have it print out as each time it prints a different list when calling the method, however, what I cannot figure out how to do is to call a previous list again as every list here will have the same variable or object name.

So, if I created say three cabin lists all together by calling this method three times, how would I assign a person to a particular cabin if they all end up having the same variable name?

It has to be a program where the user can create the list and I didn't have to declare a number of lists with a different variable name, because each camp may have a different number of cabins, and they have to be able to make their own list?

If what I'm doing is not possible and I need to be using another type of object besides a list to do this, please just tell me and I'll research how to use that object.

I've searched all over how to create a new group, a new set, etc.. and I cannot find anything relevant to what I'm trying to do that can explain this.

I want them to be able to give that group a name, such as "Red Cabin", "Blue Cabin", "Cabin 1", "Cabin 12", etc....

Nathan777
  • 79
  • 8

1 Answers1

1

For this I would recommend to use Map, with group name as a key and ArrayList as value.

private static Map<String, ArrayList<Camper>> cabinMap = new  HashMap<String, ArrayList<Camper>>();

public static void userCreatesList(String groupName){
    ArrayList<Camper> cabin = new ArrayList<Camper>();
    cabinMap.put(groupName, cabin);
}

Later you can access to the certain group from this map by name and add members to it.

Andry
  • 339
  • 7
  • 23
  • If I do this, won't it put every Camper on the list in that Cabin then? I only want some campers in each cabin, but to start out the list will be empty, and campers will be added to it. Does this still work for that? – Nathan777 Apr 02 '18 at 13:16
  • This just creates an empty ArrayList and puts it intto the map with certain groupName reference to it. Later when you add members you should do it like this: `public static void addCamper(Camper camper, String groupName) { cabinMap.get(groupName).add(camper); }` – Andry Apr 02 '18 at 13:22
  • So, if I have an ArrayList of the Camper class, and I add names to it, then I make a Map>..... it just add all the campers to that map?? I'm just trying to make sure I get what you're saying before I practice this. – Nathan777 Apr 02 '18 at 13:28
  • Each time you want to create a new cabin you call a `userCreatesList(String groupName)` method with certain groupName. Then each time you want to add a camper to some cabin you call `addCamper(Camper camper, String groupName)` method, where groupName is the name of the group, where you want the camper to add. – Andry Apr 02 '18 at 13:41
  • 1
    Also the map is created just once, at the beginning of the class. This map would later contain all the cabins. – Andry Apr 02 '18 at 13:48
  • Okay, I'm still just a little confused. Let me clarify what I'm trying to do... The campers will already be on a master list before any cabins are created. Then, I'm going to take the campers and place them in a cabin. If I have a camper list with all my campers already on it, then I make a HashMap that references that list, won't it put all the campers that I already put on the master list in that cabin already? I'm not starting out with an empty list. I'm having a full list, an then copying from that list and putting them in cabins. – Nathan777 Apr 03 '18 at 07:56
  • 1
    Oh, I see. In your case you could just iterate over this master list of yours (see [this](https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java)) and call the `addCamper(Camper camper, String groupName)` on each iteration according to which group you want to add the camper in. Of course you need to make these groups first by calling `userCreatesList(String groupName)`. – Andry Apr 03 '18 at 19:29