set int = to for loop var
so, i need to set something to the current for loop iteration and i can find no way to do this effectively.
for example
Code:
int j;
for(int i = 0; i < 10; i++) {
if(whatever) {
j = i;
}
}
that requires i to be final, and i need to use this.
i myself am not new to java, ive been using it years, but ive never had to do this, and i really wouldnt feel right posting this in advanced help
Re: set int = to for loop var
Try defining j outside the for loop.
Re: set int = to for loop var
oh, i shouldve done that anyway, in my code the variable is outside.
it actually works if its defined inside, ill change the OP
Re: set int = to for loop var
The code in the OP should work... Unless the loop is in a class in a class and j is declared outside the inner class.
Re: set int = to for loop var
actually, looking at it, it is in an inner class, because its in a mouse listener, so i assume theres no way of doing it how i want to?
Re: set int = to for loop var
I generally make the whole class into a mouselistener by implementing the MouseListener interface (or ActionListener, or WindowListener, etc). It means a few more methods that do nothing at all, but at least you can reach all your variables without a problem.
Re: set int = to for loop var
If you absolutely want an inner class though, I guess one way would be to throw the main class into it via the constructor and using a set/get method for the variable you want to reach, or just access them directly.
Re: set int = to for loop var
yeah, i do need the inner class, because inside the for loop, i create panels, then place them and add things to them all based on the for loop iteration value. then i add a MouseListener to the panel, all from inside the for loop.
i just thought id give you this more info and see if it helps more :)
Re: set int = to for loop var
No ideas beyond the ones I posted in the previous post, sorry.