I have a string, refid = "asdkjdndkdkkd"
, and another refid = "jsdjnfrejnfkn"
. How can I use the refid
as part of the name of a list?
For example, something like:
list<String> list_+refid
I have a string, refid = "asdkjdndkdkkd"
, and another refid = "jsdjnfrejnfkn"
. How can I use the refid
as part of the name of a list?
For example, something like:
list<String> list_+refid
The only way to achieve this is by writing code that emits new bytecode at run-time, or other metaprogramming approaches, as explained in this thread.
Unless you have a very good reason to do it, you might want to stick to a HashMap (or other Map):
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
// ...
List<String> list;
if (map.containsKey(refId))
list = map.get(refId);
else
map.put(refid, list = new ArrayList<String>());
// work with list here
It is possible to access a certain field by using string concatenation and reflection, but not to create one. However, you typically have multiple fields you want to create like this, so a map mapping from a string to a list seems like what you would use here. Example:
Map<String,List<String>> map = new HashMap<>();
map.put(refid, new ArrayList());
map.get(refid)