Assigning variables with dynamic names in Java












78















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?










share|improve this question




















  • 1





    Could you please clarify your question?

    – Eng.Fouad
    Jul 18 '11 at 7:08








  • 3





    You need to do this for local variables? Why not array elements?

    – Ray Toal
    Jul 18 '11 at 7:08











  • @ Eng.Fouad : I want to access variables by their name dynamically.

    – Ashish Anand
    Jul 18 '11 at 7:18











  • @Ashish Anand are you meaning stackoverflow.com/questions/6629995/…

    – mKorbel
    Jul 18 '11 at 7:40
















78















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?










share|improve this question




















  • 1





    Could you please clarify your question?

    – Eng.Fouad
    Jul 18 '11 at 7:08








  • 3





    You need to do this for local variables? Why not array elements?

    – Ray Toal
    Jul 18 '11 at 7:08











  • @ Eng.Fouad : I want to access variables by their name dynamically.

    – Ashish Anand
    Jul 18 '11 at 7:18











  • @Ashish Anand are you meaning stackoverflow.com/questions/6629995/…

    – mKorbel
    Jul 18 '11 at 7:40














78












78








78


16






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?










share|improve this question
















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?







java variables dynamic-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 5 '14 at 20:41









user2864740

43.7k670148




43.7k670148










asked Jul 18 '11 at 7:06









Ashish AnandAshish Anand

1,98152540




1,98152540








  • 1





    Could you please clarify your question?

    – Eng.Fouad
    Jul 18 '11 at 7:08








  • 3





    You need to do this for local variables? Why not array elements?

    – Ray Toal
    Jul 18 '11 at 7:08











  • @ Eng.Fouad : I want to access variables by their name dynamically.

    – Ashish Anand
    Jul 18 '11 at 7:18











  • @Ashish Anand are you meaning stackoverflow.com/questions/6629995/…

    – mKorbel
    Jul 18 '11 at 7:40














  • 1





    Could you please clarify your question?

    – Eng.Fouad
    Jul 18 '11 at 7:08








  • 3





    You need to do this for local variables? Why not array elements?

    – Ray Toal
    Jul 18 '11 at 7:08











  • @ Eng.Fouad : I want to access variables by their name dynamically.

    – Ashish Anand
    Jul 18 '11 at 7:18











  • @Ashish Anand are you meaning stackoverflow.com/questions/6629995/…

    – mKorbel
    Jul 18 '11 at 7:40








1




1





Could you please clarify your question?

– Eng.Fouad
Jul 18 '11 at 7:08







Could you please clarify your question?

– Eng.Fouad
Jul 18 '11 at 7:08






3




3





You need to do this for local variables? Why not array elements?

– Ray Toal
Jul 18 '11 at 7:08





You need to do this for local variables? Why not array elements?

– Ray Toal
Jul 18 '11 at 7:08













@ Eng.Fouad : I want to access variables by their name dynamically.

– Ashish Anand
Jul 18 '11 at 7:18





@ Eng.Fouad : I want to access variables by their name dynamically.

– Ashish Anand
Jul 18 '11 at 7:18













@Ashish Anand are you meaning stackoverflow.com/questions/6629995/…

– mKorbel
Jul 18 '11 at 7:40





@Ashish Anand are you meaning stackoverflow.com/questions/6629995/…

– mKorbel
Jul 18 '11 at 7:40












7 Answers
7






active

oldest

votes


















97














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 code1.



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; i < 4; i++) {
n.add(5);
}

Map<String, Integer> n = new HashMap<String, Integer>();
for (int i = 1; i < 4; i++) {
n.put("n" + i, 5);
}




It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See @fyr's "quick and dirty" example.



However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.





1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!






share|improve this answer


























  • Thanx a lot, got what I was looking for. The last part(Map<String, integer> ).

    – Ashish Anand
    Jul 18 '11 at 7:35






  • 1





    It should be noted that even if it would be possible, it shouldn't be something you would actually do. You don't gain anything from it; you'll actually lose readability. If you want to link them, use a Map<String, T> instead, don't start messing with your actual code.

    – Jeroen Vannevel
    Dec 15 '13 at 2:37






  • 2





    @JeroenVannevel - That's what I meant by "madness" :-)

    – Stephen C
    Dec 15 '13 at 7:25



















29














If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.



A rough quick and dirty example is this:



public class T {
public Integer n1;
public Integer n2;
public Integer n3;

public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
NoSuchFieldException {

for (int i = 1; i < 4; i++) {
T.class.getField("n" + i).set(this, 5);
}
}
}


You need to improve this code in various ways it is only an example. This is also not considered to be good code.






share|improve this answer



















  • 2





    Excellent when you need to convert Android's sensor event.values into a set of variables. event.values could have a length from 1 to 6 and it's handy to have it convert, in my case for an array-less json marshaling.

    – Farshid T
    Jun 21 '15 at 22:30



















11














What you need is named array. I wanted to write the following code:



int n = new int[4];

for(int i=1;i<4;i++)
{
n[i] = 5;
}





share|improve this answer



















  • 1





    Don't want to use arrays. I want to dynamically access the variables (n1,n2,n3) depending on some condition.

    – Ashish Anand
    Jul 18 '11 at 7:19



















10














You should use List or array instead



List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);


Or



int arr  = new int[10];
arr[0]=1;
arr[1]=2;


Or even better



Map<String, Integer> map = new HashMap<String, Integer>();
map.put("n1", 1);
map.put("n2", 2);

//conditionally get
map.get("n1");





share|improve this answer

































    5














    Dynamic Variable Names in Java

    There is no such thing.



    In your case you can use array:



    int n = new int[3];
    for() {
    n[i] = 5;
    }


    For more general (name, value) pairs, use Map<>






    share|improve this answer































      3














      Try this way:



          HashMap<String, Integer> hashMap = new HashMap();

      for (int i=1; i<=3; i++) {
      hashMap.put("n" + i, 5);
      }





      share|improve this answer































        2














        You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.






        share|improve this answer






















          protected by Stephen C Jan 15 '18 at 3:13



          Thank you for your interest in this question.
          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



          Would you like to answer one of these unanswered questions instead?














          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          97














          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 code1.



          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; i < 4; i++) {
          n.add(5);
          }

          Map<String, Integer> n = new HashMap<String, Integer>();
          for (int i = 1; i < 4; i++) {
          n.put("n" + i, 5);
          }




          It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See @fyr's "quick and dirty" example.



          However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.





          1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!






          share|improve this answer


























          • Thanx a lot, got what I was looking for. The last part(Map<String, integer> ).

            – Ashish Anand
            Jul 18 '11 at 7:35






          • 1





            It should be noted that even if it would be possible, it shouldn't be something you would actually do. You don't gain anything from it; you'll actually lose readability. If you want to link them, use a Map<String, T> instead, don't start messing with your actual code.

            – Jeroen Vannevel
            Dec 15 '13 at 2:37






          • 2





            @JeroenVannevel - That's what I meant by "madness" :-)

            – Stephen C
            Dec 15 '13 at 7:25
















          97














          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 code1.



          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; i < 4; i++) {
          n.add(5);
          }

          Map<String, Integer> n = new HashMap<String, Integer>();
          for (int i = 1; i < 4; i++) {
          n.put("n" + i, 5);
          }




          It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See @fyr's "quick and dirty" example.



          However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.





          1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!






          share|improve this answer


























          • Thanx a lot, got what I was looking for. The last part(Map<String, integer> ).

            – Ashish Anand
            Jul 18 '11 at 7:35






          • 1





            It should be noted that even if it would be possible, it shouldn't be something you would actually do. You don't gain anything from it; you'll actually lose readability. If you want to link them, use a Map<String, T> instead, don't start messing with your actual code.

            – Jeroen Vannevel
            Dec 15 '13 at 2:37






          • 2





            @JeroenVannevel - That's what I meant by "madness" :-)

            – Stephen C
            Dec 15 '13 at 7:25














          97












          97








          97







          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 code1.



          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; i < 4; i++) {
          n.add(5);
          }

          Map<String, Integer> n = new HashMap<String, Integer>();
          for (int i = 1; i < 4; i++) {
          n.put("n" + i, 5);
          }




          It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See @fyr's "quick and dirty" example.



          However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.





          1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!






          share|improve this answer















          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 code1.



          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; i < 4; i++) {
          n.add(5);
          }

          Map<String, Integer> n = new HashMap<String, Integer>();
          for (int i = 1; i < 4; i++) {
          n.put("n" + i, 5);
          }




          It is possible to use reflection to dynamically refer to variables that have been declared in the source code. However, this only works for variables that are class members (i.e. static and instance fields). It doesn't work for local variables. See @fyr's "quick and dirty" example.



          However doing this kind of thing unnecessarily in Java is a bad idea. It is inefficient, the code is more complicated, and since you are relying on runtime checking it is more fragile. And this is not "variables with dynamic names". It is better described as dynamic access to variables with static names.





          1 - That statement is slightly inaccurate. If you use BCEL or ASM, you can "declare" the variables in the bytecode file. But don't do it! That way lies madness!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 7 at 8:41

























          answered Jul 18 '11 at 7:10









          Stephen CStephen C

          516k69564921




          516k69564921













          • Thanx a lot, got what I was looking for. The last part(Map<String, integer> ).

            – Ashish Anand
            Jul 18 '11 at 7:35






          • 1





            It should be noted that even if it would be possible, it shouldn't be something you would actually do. You don't gain anything from it; you'll actually lose readability. If you want to link them, use a Map<String, T> instead, don't start messing with your actual code.

            – Jeroen Vannevel
            Dec 15 '13 at 2:37






          • 2





            @JeroenVannevel - That's what I meant by "madness" :-)

            – Stephen C
            Dec 15 '13 at 7:25



















          • Thanx a lot, got what I was looking for. The last part(Map<String, integer> ).

            – Ashish Anand
            Jul 18 '11 at 7:35






          • 1





            It should be noted that even if it would be possible, it shouldn't be something you would actually do. You don't gain anything from it; you'll actually lose readability. If you want to link them, use a Map<String, T> instead, don't start messing with your actual code.

            – Jeroen Vannevel
            Dec 15 '13 at 2:37






          • 2





            @JeroenVannevel - That's what I meant by "madness" :-)

            – Stephen C
            Dec 15 '13 at 7:25

















          Thanx a lot, got what I was looking for. The last part(Map<String, integer> ).

          – Ashish Anand
          Jul 18 '11 at 7:35





          Thanx a lot, got what I was looking for. The last part(Map<String, integer> ).

          – Ashish Anand
          Jul 18 '11 at 7:35




          1




          1





          It should be noted that even if it would be possible, it shouldn't be something you would actually do. You don't gain anything from it; you'll actually lose readability. If you want to link them, use a Map<String, T> instead, don't start messing with your actual code.

          – Jeroen Vannevel
          Dec 15 '13 at 2:37





          It should be noted that even if it would be possible, it shouldn't be something you would actually do. You don't gain anything from it; you'll actually lose readability. If you want to link them, use a Map<String, T> instead, don't start messing with your actual code.

          – Jeroen Vannevel
          Dec 15 '13 at 2:37




          2




          2





          @JeroenVannevel - That's what I meant by "madness" :-)

          – Stephen C
          Dec 15 '13 at 7:25





          @JeroenVannevel - That's what I meant by "madness" :-)

          – Stephen C
          Dec 15 '13 at 7:25













          29














          If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.



          A rough quick and dirty example is this:



          public class T {
          public Integer n1;
          public Integer n2;
          public Integer n3;

          public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
          NoSuchFieldException {

          for (int i = 1; i < 4; i++) {
          T.class.getField("n" + i).set(this, 5);
          }
          }
          }


          You need to improve this code in various ways it is only an example. This is also not considered to be good code.






          share|improve this answer



















          • 2





            Excellent when you need to convert Android's sensor event.values into a set of variables. event.values could have a length from 1 to 6 and it's handy to have it convert, in my case for an array-less json marshaling.

            – Farshid T
            Jun 21 '15 at 22:30
















          29














          If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.



          A rough quick and dirty example is this:



          public class T {
          public Integer n1;
          public Integer n2;
          public Integer n3;

          public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
          NoSuchFieldException {

          for (int i = 1; i < 4; i++) {
          T.class.getField("n" + i).set(this, 5);
          }
          }
          }


          You need to improve this code in various ways it is only an example. This is also not considered to be good code.






          share|improve this answer



















          • 2





            Excellent when you need to convert Android's sensor event.values into a set of variables. event.values could have a length from 1 to 6 and it's handy to have it convert, in my case for an array-less json marshaling.

            – Farshid T
            Jun 21 '15 at 22:30














          29












          29








          29







          If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.



          A rough quick and dirty example is this:



          public class T {
          public Integer n1;
          public Integer n2;
          public Integer n3;

          public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
          NoSuchFieldException {

          for (int i = 1; i < 4; i++) {
          T.class.getField("n" + i).set(this, 5);
          }
          }
          }


          You need to improve this code in various ways it is only an example. This is also not considered to be good code.






          share|improve this answer













          If you want to access the variables some sort of dynamic you may use reflection. However Reflection works not for local variables. It is only applyable for class attributes.



          A rough quick and dirty example is this:



          public class T {
          public Integer n1;
          public Integer n2;
          public Integer n3;

          public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException,
          NoSuchFieldException {

          for (int i = 1; i < 4; i++) {
          T.class.getField("n" + i).set(this, 5);
          }
          }
          }


          You need to improve this code in various ways it is only an example. This is also not considered to be good code.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 18 '11 at 7:30









          fyrfyr

          15.4k52948




          15.4k52948








          • 2





            Excellent when you need to convert Android's sensor event.values into a set of variables. event.values could have a length from 1 to 6 and it's handy to have it convert, in my case for an array-less json marshaling.

            – Farshid T
            Jun 21 '15 at 22:30














          • 2





            Excellent when you need to convert Android's sensor event.values into a set of variables. event.values could have a length from 1 to 6 and it's handy to have it convert, in my case for an array-less json marshaling.

            – Farshid T
            Jun 21 '15 at 22:30








          2




          2





          Excellent when you need to convert Android's sensor event.values into a set of variables. event.values could have a length from 1 to 6 and it's handy to have it convert, in my case for an array-less json marshaling.

          – Farshid T
          Jun 21 '15 at 22:30





          Excellent when you need to convert Android's sensor event.values into a set of variables. event.values could have a length from 1 to 6 and it's handy to have it convert, in my case for an array-less json marshaling.

          – Farshid T
          Jun 21 '15 at 22:30











          11














          What you need is named array. I wanted to write the following code:



          int n = new int[4];

          for(int i=1;i<4;i++)
          {
          n[i] = 5;
          }





          share|improve this answer



















          • 1





            Don't want to use arrays. I want to dynamically access the variables (n1,n2,n3) depending on some condition.

            – Ashish Anand
            Jul 18 '11 at 7:19
















          11














          What you need is named array. I wanted to write the following code:



          int n = new int[4];

          for(int i=1;i<4;i++)
          {
          n[i] = 5;
          }





          share|improve this answer



















          • 1





            Don't want to use arrays. I want to dynamically access the variables (n1,n2,n3) depending on some condition.

            – Ashish Anand
            Jul 18 '11 at 7:19














          11












          11








          11







          What you need is named array. I wanted to write the following code:



          int n = new int[4];

          for(int i=1;i<4;i++)
          {
          n[i] = 5;
          }





          share|improve this answer













          What you need is named array. I wanted to write the following code:



          int n = new int[4];

          for(int i=1;i<4;i++)
          {
          n[i] = 5;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 18 '11 at 7:09









          AlexRAlexR

          97.9k898169




          97.9k898169








          • 1





            Don't want to use arrays. I want to dynamically access the variables (n1,n2,n3) depending on some condition.

            – Ashish Anand
            Jul 18 '11 at 7:19














          • 1





            Don't want to use arrays. I want to dynamically access the variables (n1,n2,n3) depending on some condition.

            – Ashish Anand
            Jul 18 '11 at 7:19








          1




          1





          Don't want to use arrays. I want to dynamically access the variables (n1,n2,n3) depending on some condition.

          – Ashish Anand
          Jul 18 '11 at 7:19





          Don't want to use arrays. I want to dynamically access the variables (n1,n2,n3) depending on some condition.

          – Ashish Anand
          Jul 18 '11 at 7:19











          10














          You should use List or array instead



          List<Integer> list = new ArrayList<Integer>();
          list.add(1);
          list.add(2);
          list.add(3);


          Or



          int arr  = new int[10];
          arr[0]=1;
          arr[1]=2;


          Or even better



          Map<String, Integer> map = new HashMap<String, Integer>();
          map.put("n1", 1);
          map.put("n2", 2);

          //conditionally get
          map.get("n1");





          share|improve this answer






























            10














            You should use List or array instead



            List<Integer> list = new ArrayList<Integer>();
            list.add(1);
            list.add(2);
            list.add(3);


            Or



            int arr  = new int[10];
            arr[0]=1;
            arr[1]=2;


            Or even better



            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("n1", 1);
            map.put("n2", 2);

            //conditionally get
            map.get("n1");





            share|improve this answer




























              10












              10








              10







              You should use List or array instead



              List<Integer> list = new ArrayList<Integer>();
              list.add(1);
              list.add(2);
              list.add(3);


              Or



              int arr  = new int[10];
              arr[0]=1;
              arr[1]=2;


              Or even better



              Map<String, Integer> map = new HashMap<String, Integer>();
              map.put("n1", 1);
              map.put("n2", 2);

              //conditionally get
              map.get("n1");





              share|improve this answer















              You should use List or array instead



              List<Integer> list = new ArrayList<Integer>();
              list.add(1);
              list.add(2);
              list.add(3);


              Or



              int arr  = new int[10];
              arr[0]=1;
              arr[1]=2;


              Or even better



              Map<String, Integer> map = new HashMap<String, Integer>();
              map.put("n1", 1);
              map.put("n2", 2);

              //conditionally get
              map.get("n1");






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 18 '11 at 7:33

























              answered Jul 18 '11 at 7:08









              Jigar JoshiJigar Joshi

              199k35339390




              199k35339390























                  5














                  Dynamic Variable Names in Java

                  There is no such thing.



                  In your case you can use array:



                  int n = new int[3];
                  for() {
                  n[i] = 5;
                  }


                  For more general (name, value) pairs, use Map<>






                  share|improve this answer




























                    5














                    Dynamic Variable Names in Java

                    There is no such thing.



                    In your case you can use array:



                    int n = new int[3];
                    for() {
                    n[i] = 5;
                    }


                    For more general (name, value) pairs, use Map<>






                    share|improve this answer


























                      5












                      5








                      5







                      Dynamic Variable Names in Java

                      There is no such thing.



                      In your case you can use array:



                      int n = new int[3];
                      for() {
                      n[i] = 5;
                      }


                      For more general (name, value) pairs, use Map<>






                      share|improve this answer













                      Dynamic Variable Names in Java

                      There is no such thing.



                      In your case you can use array:



                      int n = new int[3];
                      for() {
                      n[i] = 5;
                      }


                      For more general (name, value) pairs, use Map<>







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 18 '11 at 7:10









                      Op De CirkelOp De Cirkel

                      21.9k63043




                      21.9k63043























                          3














                          Try this way:



                              HashMap<String, Integer> hashMap = new HashMap();

                          for (int i=1; i<=3; i++) {
                          hashMap.put("n" + i, 5);
                          }





                          share|improve this answer




























                            3














                            Try this way:



                                HashMap<String, Integer> hashMap = new HashMap();

                            for (int i=1; i<=3; i++) {
                            hashMap.put("n" + i, 5);
                            }





                            share|improve this answer


























                              3












                              3








                              3







                              Try this way:



                                  HashMap<String, Integer> hashMap = new HashMap();

                              for (int i=1; i<=3; i++) {
                              hashMap.put("n" + i, 5);
                              }





                              share|improve this answer













                              Try this way:



                                  HashMap<String, Integer> hashMap = new HashMap();

                              for (int i=1; i<=3; i++) {
                              hashMap.put("n" + i, 5);
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 18 '11 at 7:16







                              user784540






























                                  2














                                  You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.






                                  share|improve this answer




























                                    2














                                    You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.






                                    share|improve this answer


























                                      2












                                      2








                                      2







                                      You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.






                                      share|improve this answer













                                      You don't. The closest thing you can do is working with Maps to simulate it, or defining your own Objects to deal with.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 18 '11 at 12:36









                                      evertoneverton

                                      5,66722137




                                      5,66722137

















                                          protected by Stephen C Jan 15 '18 at 3:13



                                          Thank you for your interest in this question.
                                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                          Would you like to answer one of these unanswered questions instead?



                                          Popular posts from this blog

                                          Ottavio Pratesi

                                          Error adding annotation colours to pheatmap in R: “more elements supplied than there are to replace”

                                          15 giugno