6

So for example I have the following variables: Var1, Var2, Var3, Var4, Var5 - a total of 5 variables. All with unique data and I want to loop though them using a for loop.

//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(e.g. label1, label2, label3 etc..) and want to change the value of these components without having to individually set their value.

4
  • Why does no one in [java] maintain canonical duplicates? Surely this is asked hundred times before. Commented Oct 17, 2015 at 11:32
  • @BalusC Canonical Duplicates? Commented Oct 17, 2015 at 11:36
  • Sounds like you are looking for a Map datastructure. Commented Oct 17, 2015 at 11:37
  • @DaneBrouwer Can you just use array? Commented Oct 17, 2015 at 12:26

6 Answers 6

11

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. E.g. you can assign a String to a variable of type Object but not the other way around.

Sign up to request clarification or add additional context in comments.

3 Comments

I wonder why this is downvoted, since it's the only way to truly iterate through variables in Java.
Gotta laugh at the -1's for the only proper solution.
Can I set variables to those class fields? IE, class has String point1, point2, point3; can I iterate through fields and set string?
5

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);
        }

Comments

2

As long as all the variables use the same type, you can use an Array to store all of them. Then you can use a for loop to iterate through the array. 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]);
    }

Comments

1

Using Java 8 Arrays it is as simple as:

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);
    }
}

Comments

1

You can't loop through (local) variables. You can use an array or a List and then loop through its elements:

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

Comments

-3

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);
        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.