question about how to manage a simple for loop and variable.
I am just learing java so please forgive how newbie this question must be.
I want to create a simple program that has 5 variables:
variable1, variable2, variable3, variable4, variable5;
Then I want to create a simple for loop that address each variable through the counter variable i. For example.
for (int i = 0; i <= 5; i++){
System.out.println( variable + i );
}
I would like to know how to make that (variable+i) statement correctly so the system prints all 5 variables and exits. Is there a way to do this in java?
Thank you for the help.
Use an arrayint variables[] = {0, 1, 2, 3, 4};System.out.println(variables[0]); // access the first location in array
>Why?
I guess the short answer is that I'm stubborn. The long answer is...
I am used to doing a little programming in Actionscript ( the scripting language for macromedia flash).
That language is built to resemble java closely.
In that language you can have say two variables:
variable1 = 20;
i = 1;
and you can address variable1 by something like print ( ["variable" + i] );
So I am just curious to see if it is possible in Java to do anything that resembles this.
> Good insight.
>
> Thank you for the answers.
>
> I will break down and use an array to accomplish my
> goal.
Just keep in mind that an array is immutable, meaning it cannot change size after you declare and initialize it.
Look into an ArrayList if you need to dynamically grow or shrink the size.
JJ