2

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.

4
  • You instructor doesn't know what constants or conventions are. public static final int GAME[][] Commented Oct 11, 2012 at 2:10
  • @LanguagesNamedAfterCofee I'd say the OP doesn't know what constants are. I believe that the program is going to want to modify the values in game Commented Oct 11, 2012 at 2:22
  • You should still be able to modify the values in game even if it is declared as static final. Commented Oct 11, 2012 at 2:24
  • Yeah, I mean constants as in... just a number they'll give us. This constants file will be replaced with a new one with the same variable names and all, just different numbers in different positions. Commented Oct 11, 2012 at 2:39

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.