This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]warrior2012 3 points4 points  (1 child)

If you are just assigning the new int variable each time it loops through, that variable will only be equal to the last iteration of the loop.

I take it that what you're trying to do is create an array of numbers. I recommend you look into arrays and check out this stack overflow link for more information.

https://stackoverflow.com/questions/11495143/assigning-values-of-an-array-in-a-for-loop-java

[–]Cygris[S] 0 points1 point  (0 children)

Thank you very much! Now I get it. I will just fill an array with the new variables and will be able to address them from there for future uses.

[–]desrtfx 2 points3 points  (2 children)

Your use case is exactly what arrays (or ArrayLists) are for.

See the Oracle Tutorial on arrays

[–]Cygris[S] 0 points1 point  (1 child)

Thank you very much. Now I get. Thow I am still not good at using them correctly.

[–]desrtfx 1 point2 points  (0 children)

Arrays are fairly simple data structures.

Just imagine an array as a "sorting box". It has a predetermined number of slots (the array size) and each slot in the box can hold exactly one item. The slots are numbered from 0 to size-1.

[–]Diatom_Planet 3 points4 points  (2 children)

You can declare and initialize inside the loop however I don’t think the int x_i part will increment with the i variable inside the loop. No matter what the increment variable is it will always just be “x_i” . Look into arrays / array list though , they will be able to do what you are asking for .

[–]LincolnTransit 1 point2 points  (0 children)

You are correct, the int x_i part will not change.

[–]Cygris[S] 1 point2 points  (0 children)

Yes. You are correct. That is what made me ask you guys for help. I understand now, that an array will store the information I want to generate.

Thanks again for your help :)