I want to make a method that will create ArrayLists, and name each one after an element in a existing ArrayList.
Like, ArrayList original is (abc, def, ghi, jkl)
and I want the method to create four ArrayLists, named abc, def, ghi, and jkl.
How to pull those strings (abc, def) out and use them to name the new ArrayLists?
I tried:
ArrayList original.get(0) = new ArrayList();
But Eclipse says misplaced construct
I tried:
String newName = original.get(count);
ArrayList (newName) = new ArrayList();
But it says the left hand side of of an assignment must be a variable
If I take the newName out of parenthesis:
String newName = original.get(count);
ArrayList newName = new ArrayList();
It says duplicate local variable newName
In Java, you cannot name a variable based on the value of another variable. The compiler must know the names of all variables at compile-time. However, the return value of a method such as
original.get(0)and values of variables are only known at run-time.With that said, you can use the
Map<String, List<String>>class to do what you are trying to do with dynamic variable names. You will just add yourArrayLists to theMapindexed on theString“names”.