r/javahelp • u/[deleted] • Aug 17 '19
Easier way to do this? (Working with array)
How can I call rotate() on my orbs objects so that I do not have to type out every index of the array? Is this even possible?
And no I cannot use a for loop (I believe) because I need all indexes to be calling rotate() at the same time.
public static void rotateAll() {
while (true) {
orbs[0].rotate();
orbs[1].rotate();
orbs[2].rotate();
orbs[3].rotate();
orbs[4].rotate();
orbs[5].rotate();
orbs[6].rotate();
orbs[7].rotate();
orbs[8].rotate();
orbs[9].rotate();
orbs[10].rotate();
orbs[11].rotate();
orbs[12].rotate();
orbs[13].rotate();
orbs[14].rotate();
orbs[15].rotate();
orbs[16].rotate();
orbs[17].rotate();
orbs[18].rotate();
orbs[19].rotate();
orbs[20].rotate();
orbs[21].rotate();
orbs[22].rotate();
orbs[23].rotate();
orbs[24].rotate();
orbs[25].rotate();
}
}2
u/jocularamity Aug 17 '19
In your example code, rotate() is called one orb at a time, one after the other in sequence. Then after the last orb rotates, because of the while(true), it will repeat again starting back at the first orb again. Can you elaborate on what you're trying to do, i.e. why do you think they need to be called at the same time?
To iterate through all of the globes one time, you could use a for loop.
// this code rotates all orbs once, one at a time.
for(int i = 0; i < orbs.length; ++i){
orbs[i].rotate();
} If you want to have it rotate the orbs repeatedly like your example code does, you could put that whole thing inside an enclosing while(true) loop.
while(true){ // this repeats forever because true is always true.
// 1. Maybe you want to do some other stuff before rotating the orbs? Put that here.
// 2. rotate all the orbs once here
// 3. do some other stuff after rotating the orbs? That goes here.
}2
Aug 17 '19
The reason I want the orbs to rotate simultaneously is because I am trying to simulate planets rotating around a sun, for example.
I am trying to simulate that imagery. So I need everything moving at once, not iteratively.
2
u/jocularamity Aug 17 '19
I think we would need more complete code to help you figure that out. My recommendation would be to get it working like this first, with rotate() called on one orb at a time, and see how it looks. It may appear virtually instantaneous because computers are fast so all of the orbs more or less continuously move. Depending on the code, there might be a single rendering step that happens after all of the orbs finish rotating. Depends what's happening inside the rotate method and in your other code.
Generally speaking, if you're reading java code, the lines will execute in sequence. You can step through them one at a time and know they will execute one after another, not at the same time. In order for things to really happen at the same exact time, you would need them to run in two different threads which is a little more advanced.
3
Aug 17 '19
I appreciate the answer. I think my code is working now where the computer is so fast that I am not noticing each line operating separately. Instead I am put under the illusion that everything is running simultaneously.
Anyways, I am glad my code is working easily now. Multi-threading is a future topic I am looking forward to.
8
Aug 17 '19 edited Aug 17 '19
In your while loop, all orbs rotate sequentially *not at the same time*. Adding a for loop (with index) inside while would work the same as the while loop in your code.
while (true)
for (i=0;i<orbs.length;i++)
orbs[i].rotate();If you want real parallel execution you have to introduce a thread per orb.
For example:
while(true)
Stream.of(orbs).parallelStream().forEach(it->it.rotate());7
u/desrtfx Out of Coffee error - System halted Aug 17 '19
You have a severe misunderstanding of code flow.
Code flows sequentially from top to bottom, so each .rotate() gets executed only after the previous one has completed.
A for loop would be in no way different.
Your only chance would be to multithread the calls.
11
u/Gorfoo Aug 17 '19
How would using a for loop work any differently than how you have it now? As it is, it won't do it all at the same time; a for-loop wouldn't change that.