简体   繁体   English

有没有办法循环变量名称?

[英]Is there any way to loop though variable names?

So for example I have the following variables: Var1, Var2, Var3, Var4, Var5 - a total of 5 variables. 例如,我有以下变量:Var1,Var2,Var3,Var4,Var5 - 共5个变量。 All with unique data and I want to loop though them using a for loop. 所有都有独特的数据,我想使用for循环循环它们。

//String Var1 = something, Var2 = something etc..
for (int i = 1; i <= 5; i++)
{
Var(i) = "something else";
}
//i.e I change Var(1), Var(2) etc.. to something else respectively.

To clarify further, ultimately I want to apply this method to iterate through multiple components in my program. 为了进一步澄清,最终我想应用此方法来迭代我的程序中的多个组件。 I have a large number of components with styled names(eg label1, label2, label3 etc..) and want to change the value of these components without having to individually set their value. 我有大量具有样式名称的组件(例如label1,label2,label3等),并且想要更改这些组件的值而无需单独设置它们的值。

You can do it with reflection, if the variables are defined as members of a class. 如果将变量定义为类的成员,则可以使用反射执行此操作。 For method parameters or local variables it is not possible. 对于方法参数或局部变量,它是不可能的。 Something similar to this: 与此类似的东西:

Class currentClass = getClass();
Field[] fields = currentClass.getFields();
for (Field f : fields) {
  System.out.println(f.getName());
}

If you intend to change the value it becomes a bit more complicated as you also have to consider the type of the variable. 如果您打算更改值,它会变得有点复杂,因为您还必须考虑变量的类型。 Eg you can assign a String to a variable of type Object but not the other way around. 例如,您可以将String分配给Object类型的变量,但不能反过来。

I would suggest to go for an array if data type of variables are same. 如果变量的数据类型相同,我建议去一个数组。 You can try something like that 你可以试试这样的东西

        String[] Var = {"something","something2","something else"};
        for (String var : Var)
        {
        System.out.println(var);
        }

You can't loop through (local) variables. 你不能循环(本地)变量。 You can use an array or a List and then loop through its elements: 您可以使用数组List ,然后遍历其元素:

for (Object item : myListOfObjects) {
    // do the processing
}

Using Java 8 Arrays it is as simple as: 使用Java 8 Arrays就像这样简单:

Arrays.stream(varArray).forEach(System.out::println);

Usage: 用法:

public class LoopVariables {
    public static void main(String[] args) {
        String[] varArray = new String[]{"Var1", "Var2", "Var3", "Var4", "Var5"};
        Arrays.stream(varArray).forEach(System.out::println);
    }
}

As long as all the variables use the same type, you can use an Array to store all of them. 只要所有变量使用相同的类型,就可以使用Array来存储所有变量。 Then you can use a for loop to iterate through the array. 然后,您可以使用for循环遍历数组。 Something like this: 像这样的东西:

     String[] V = {"var1","var2","var3","var4","var5"};
    int arraylength = V.length;

    for(int i = 0; i<arraylength; i++){
        System.out.println(V[i]);
    }

Try This piece of Code. 试试这段代码。

public class Main {

    public static void main(String[] args) {
        Insan i1[]=new Insan[5];
        i1[0].name="Ali";
        i1[0].age=19;
        i1[1].name="Pakize";
        i1[1].age=29;
        i1[2].name="Kojiro Hyuga";
        i1[2].age=30;
        i1[3].name="Optimus Prime";
        i1[3].age=40;
        for (int ib=0; ib < 4; ib++) {
            System.out.println("Name: " + i1[ib].name + " Age: "+i1[ib].age);
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM