Reputation: 21
I am currently in a Java course this semester (completely new to programming and struggling a bit with multiple classes and arrays). The current program I am working on is a Sudoku type game using a 2D array. The instructors have given us a class for Constant numbers in this array (the initial values in the Sudoku puzzle). Here is the code for it.
public class Constants {
public static int game[][] = new int[][] {
{ 1, 2, -1, -1, -1, -1, -1, -1, 8 },
{ -1, -1, 4, -1, 8, -1, 7, 1, -1 },
{ -1, -1, -1, -1, 1, -1, 5, 3, -1 },
{ 8, -1, -1, -1, -1, 4, -1, -1, -1 },
{ -1, 4, -1, -1, -1, -1, 6, 5, -1 },
{ 7, 5, -1, 1, -1, -1, -1, -1, -1 },
{ -1, 7, -1, -1, -1, -1, -1, 9, -1 },
{ 3, -1, 1, 8, 5, -1, -1, -1, 6 },
{ 5, 6, -1, 9, -1, 7, -1, -1, -1 }
};
The values of -1 refer to blank spaces in the puzzle.
Basically all I need to know is.. how would I access this array or these values in my other Class and main method? Once again I apologize for being completely new to java, thank you in advance.
Upvotes: 2
Views: 1333
Reputation: 129497
You would access the actual game
variable with
Constants.game
and you can access individual members of the array with
Constants.game[i][j]
where i
is the row and j
is the column of the element you are trying to access.
Upvotes: 5