문제

So i have created a Square class, created a 2D array in my "NormalMode" class like this:

Square[][] square = new Square[4][4]

and initialized it with the 2 nested for loops. Now that i have this, how can i run a method from my Square class for all the square objects in the array at the same time?

도움이 되었습니까?

해결책 2

Use another pair of loops, or use a static method if the method is independent of the Square instances (i.e. if the method doesn't modify or access any instance fields).

If you don't want a static method and you only want the method to be executed once you should put the code into the constructor, or call it from there.

다른 팁

You have to do it one by one:

for (Square[] arr: square)
    for (Square sq : square)
        sq.someMethod();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top