dynamic-variables

Use dynamic variable names in JavaScript

为君一笑 提交于 2019-11-25 21:37:20
问题 In PHP you can do amazing/horrendous things like this: $a = 1; $b = 2; $c = 3; $name = \'a\'; echo $$name; // prints 1 Is there any way of doing something like this with Javascript? E.g. if I have a var name = \'the name of the variable\'; can I get a reference to the variable with name name ? 回答1: Since ECMA-/Javascript is all about Objects and Contexts (which, are also somekind of Object), every variable is stored in a such called Variable- (or in case of a Function, Activation Object ). So

Assigning variables with dynamic names in Java

人盡茶涼 提交于 2019-11-25 21:36:40
问题 I\'d like to assign a set of variables in java as follows: int n1,n2,n3; for(int i=1;i<4;i++) { n<i> = 5; } How can I achieve this in Java? 回答1: This is not how you do things in Java. There are no dynamic variables in Java. Java variables have to be declared in the source code 1 . Depending on what you are trying to achieve, you should use an array, a List or a Map ; e.g. int n[] = new int[3]; for (int i = 0; i < 3; i++) { n[i] = 5; } List<Integer> n = new ArrayList<Integer>(); for (int i = 1