1

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

1
  • you simply can't do that in Java Commented Sep 24, 2012 at 8:59

1 Answer 1

6

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 your ArrayLists to the Map indexed on the String "names".

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

6 Comments

I don't think I should need to do all that, since we have not come close to covering that in my schooling. What if I simply had a String variable: String aString = "TheName" and I want to create a new ArrayList called "TheName". Is there any simpler way to get thename out of that String and name my new object with it?
@ChrisKekaha Nope, you can't use the contents of a variable to name another variable. As I stated in my first paragraph, one must be known at compile-time and the other is only known at run time.
@ChrisKekaha If my answer is more complicated than you expected, then I think that you are trying to solve the wrong problem. If you can provide more details (perhaps in a new question), we will gladly give you some guidance as to how to go about solving the problem.
Well, it's for my homework, of course, so I don't want you to just GIVE me the code, but any hints and/or pointing in the right direction would be appreciated....I want to allow the user to enter the names of students(which will be stored in an ArrayList), then hit 'C' to stop entering the names...Then I want to create separate ArrayLists to hold their test scores (which I will later sort and average and so on), so I thought I would like to give the ArrayList for student Smith the same name as the student, or maybe "smithScores"..I guess there may be an easier way to hold the data
@ChrisKekaha Please post a new question with this information. If you want to bring it to my attention, add a comment that begins with "@Code-Guru". I will be happy to post an answer with some suggestions. Others will likely have additional suggestions as well.
|

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.