for my new project I want my code to create a new rectangle for each one existing in a list.
Given is the List "particles". I want to do something like
foreach(Rectangle rec in particles){
Rectangle r2 = new Rectangle(10, 10, 10, 10);
particles.add(r2);
}
The problem is, that I need to define a name. If I don't know, how many rectangles will be in list particles when the code executes, I can't just do it like that (I hope you get what I mean). What I need is something like a function that creates a new rectangle with the name r3 or r4 or r5 (...) as long as there are new rectangles. I really don't know how. Can you help me?
You don't have to increment the variable name, you just need to add the rectangles as you are to the collection and then the index becomes the reference. i.e particles.get(0) will be your r0 rectangle and so on.
If you absolutely need a 'plain english' reference, use a map and set the key as a String such as "r"+indexNumber i.e r0, r1, r2..etc. Then the value for that key is the actual rectangle object you created in that loop.