I have the following method in one of my classes called Board. Board has an array of 120 Squares, which is another class from my program.
public class Board{
private Square[] square = new Square[120];
...
Each Square has an int row and an int column.
public class Square extends JButton{
public int row;
public int column;
...
The method itself is supposed to figure out what the row and column is for every Square inside
void setSquares() {
int ones;
int tenths;
Square s = new Square();
Insets squareMargin = new Insets(5, 5, 5, 5);
s.setMargin(squareMargin);
for (int i = 0; i < square.length; i++){
ones = getNdigit(i, 1);
tenths = getNdigit(i, 2);
//set row && set column
if ((tenths >= 2 && tenths <= 9) && (ones >= 1 && ones <= 8)){
s.row = tenths - 1;
s.column = ones;
} else{
s.row = 0;
s.column = 0;
}
square[i] = s;
System.out.println(square[0].toString());
}
So at the end of the method, I expect that square[34] has a row of 2 and a column of 4. However, the actual result is always the same as where the for loop ended (square[34] has a row and column of 0). If the for loop was changed to
for (int i = 0; i < 55; i++){
then square[34] has a row of 4 and a column of 4.