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.

[601 byte] By [klintonraya] at [2007-10-3 4:33:54]
# 1
Use an arrayint variables[] = {0, 1, 2, 3, 4};System.out.println(variables[0]); // access the first location in array
tymer99a at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 2
Use an array instead.int variable[] = new int[5];...for (int i = 0; i < variable.length; i++) {System.out.println(variable[ i ]);}Beat me to it.Message was edited by: warnerja
warnerjaa at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 3
> Beat me to it.That's a first
tymer99a at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 4
Thank you for the array idea :-). Is there any other way other than an array to do this?
klintonraya at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 5
[url http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html]java.util.List[/url]
tymer99a at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 6
> Is there any other way other than an array to do this?Why?
jfbrierea at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 7

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

klintonraya at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 8
Thats the difference between a scripting language and a programming language.IMHO.Just like JavaScript ≠ Java.JJ
Java_Jaya at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 9
Good insight.Thank you for the answers.I will break down and use an array to accomplish my goal.
klintonraya at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...
# 10

> 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

Java_Jaya at 2007-7-14 22:37:32 > top of Java-index,Java Essentials,New To Java...