You want to use Java variables repeatedly by numbering them. What should I do?

Asked 2 years ago, Updated 2 years ago, 106 views

int Mnum0,Mnum1,Mnum2,Mnum3,Mnum4,Mnum5,Mnum6,,,,,,,,,,,,,,,,,,,

   for(int h=0;h<Mnum0.length;h++)

   for(int h=0;h<Mnum1.length;h++)

   for(int h=0;h<Mnum2.length;h++)

   for(int h=0;h<Mnum3.length;h++).

   for(int h=0;h<Mnum4.length;h++)

Repeat statement above

for(int i=0; i<100; i++)
   {

    for(int h=0;h<Mnumi.length;h++) /// Replace the number part of Mnum with the variable i (Mnumi)

    }

How do I respond to the number of iterations of a variable like this?

Question about generating dynamic variables.

java for

2022-09-21 22:59

2 Answers

Study reflection.

You can access the field as a string to obtain a value.

http://tutorials.jenkov.com/java-reflection/fields.html


2022-09-21 22:59

List<String> list = new ArrayList<>(Arrays.asList("a","bb","ccc","dddd"));
for (int i = 0; i < list.size(); i++) {
    for (int j = 0; j < list.get(i).length(); j++) {
        System.out.print(list.get(j));
    }
    System.out.println();
}

Put it in an array or list and look at the index


2022-09-21 22:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.