-1

How can I send a string to a method that receives a string as a parameter, and use that parameter being received to name a new ArrayList?

example:

public void new(String name){


        ArrayList name = new ArrayList();

}

Why doesnt it works, and what can I do to fix it?

Daniel Rodríguez
  • 684
  • 3
  • 7
  • 15
  • You want to name an `ArrayList` after a variable? I think I would rather create a `HashMap`, and map the variable to an `ArrayList`. – Clark Kent Nov 25 '15 at 14:12
  • It doesn't work because variable names are assigned at compile time, not runtime. As @Saviour suggests, a common alternative is having a `HashMap` key-value pair like so: `{nameStr, nameArrayListObj}` – CubeJockey Nov 25 '15 at 14:14
  • For what purpose? You won't have any advantage from this, accept you're trying something dirty with reflection. – Tom Nov 25 '15 at 14:14
  • What did you mean by "to name a new ArrayList" ? Why you are declaring `name` again while you already passed it ? – Rahman Nov 25 '15 at 14:14
  • I think you are trying to solve the wrong problem, read about [XY problem](http://mywiki.wooledge.org/XyProblem) and then ask about the thing that you really want to do. – user3707125 Nov 25 '15 at 14:16

1 Answers1

0

You can't do this. Local variable names is just for you, choose what you want.

But if you need a different ArrayList from a parameter, you should use an Map<String,ArrayList>. By doing that, you can put into the map an arraylist as value and use the name as the key for this value.

Prim
  • 2,880
  • 2
  • 15
  • 29