1

I have 10 array lists and a String variable. I have one function/method that allows the user to select one of the array lists to add to. Currently, that function changes the value of a string "chosen" to the value of the proper array list name.

Another function is started in which adding elements to the arraylist happens. However, I am struggling to come up with a solution on how to change which arraylist is being added to based upon what was selected by the user.

This code doesn't work but say it was:

chosen.add(whatUserEntered);

Normally you can put in the arrayList name and add .add(whateverTheUserIsEntering) and itll work fine.

So how can I have the name of an arrayList equal that of a String variable's value?

2
  • 3
    Use a Map. An old name for a Map is "symbol table," used to bind names in your program to memory addresses. That's basically what you're doing here. Commented Jan 9, 2016 at 5:50
  • Simple.. Keep a map of <whatUserEntered, actualArrayListReference> . So, when user selects an arraylist name, you can internally get out your reference that represents it and add the values :) Commented Jan 9, 2016 at 5:51

2 Answers 2

1

Here is an example using a Map to bind string names to lists. When you need to put things together at run time (as opposed to compile time, as you show with your example choosen.add(userEntered)), you usually need to use a Map.

This works with strings read from a file (say to bind XML names to objects) or things like interpreters where you have to keep track of user variables by name.

public class SymbolTableExample
{
   public static void main(String[] args) {
      // Some array lists
      ArrayList<String> cats = new ArrayList<>();
      ArrayList<String> dogs = new ArrayList<>();
      ArrayList<String> beetles = new ArrayList<>();
      ArrayList<String> cows = new ArrayList<>();

      // Bind names to them
      Map<String,List<String>> binding = new HashMap<>();
      binding.put( "cats", cats );
      binding.put( "dogs", dogs );
      binding.put( "beetles", beetles );
      binding.put( "cows", cows );

      // Pretend the user enters some input here
      String userChoice = "cats";
      String userEntered = "flufy";
      binding.get( userChoice ).add( userEntered );

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

2 Comments

What are the advantages to using Map<String, List<String>> instead of Map<String, ArrayList<String>>?
@Arc676 I guess "Program to an interface." Since I didn't need any special methods from ArrayList here, I just selected List without really thinking about it. I suppose there could be good reasons to use ArrayList too.
0

Use a Map.

public interface Map<K,V>

You can use the method

V put(K key, V value)

to assign a value to a key, so you can set ArrayLists for different keys or add new ones..

Since you are using ArrayLists, you could make your make your Map like so:

Map<String, ArrayList<String>)> myArrays

Then, to add a value to an array with a given key, use

myArrays.get(userChosenArrayKey).add(whatUserEntered)

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.