-1

Assume that I already have a 2D int value[i][j]. This works well and I can store a single int in each index. Now I want to add a second array int data[i][j] so I can store multiple int data in it. Am I approaching it correctly?

For example in a Sudoku situation:

value[0][0] = 0

But in an other grid I have all possible values in each index

data[0][0] = {1,2,3,4,5,6,7,8,9}

Is it possible to do that? If so what should I do with my data? I'm really confused about arrays, multi-dimensional arrays, ArrayLists, etc. I'm unsure which to use.

For example:

Values {1,2,3},{4,5,6},{7,8,9}

In an 3x3:

1,2,3
4,5,6
7,8,9

Data{1,2,3,4,5,6,7,8,9}

Which I want to store it now in each grid and will have a method to remove from that list in later steps because I'm cancelling those possibilities in that grid. And the data in data{} doesn't have to be shown to the user.

approxiblue
  • 6,624
  • 16
  • 47
  • 56
Tim Au
  • 3
  • 3

3 Answers3

1

I hope this will clear to some extent

Internally, Java stores 2 dimensional arrays as an array of arrays: Suppose you have

int [][] nums = new int[5][4];

The above is really equivalent to a 3-step process:

int [][] nums;

// create the array of references 
nums = new int[5][];

// this create the second level of arrays 

for (int i=0; i < nums.length ; i++)
    nums[i] = new int[4]; // create arrays of integers
shikjohari
  • 2,147
  • 8
  • 23
0

Yes, it is possible. For that I'd recommend a boolean[][][] where, for example, theArray[6][3][8] is a boolean indicating whether the number 8 is included in the cell at row 6, column 3.

tbodt
  • 15,199
  • 5
  • 52
  • 78
0

Simply use boolean[] for possible choices. You could use Set (http://docs.oracle.com/javase/7/docs/api/java/util/Set.html), but for a small pool of fixed size with boolean options only, Set is an overkill (mainly due to code overhead).

Next thing is, that the basic principle of OOP (and Java is, or at least should be OOP) is that you should group data into objects/classes based on their functionality. If you're trying to aggregate a cell in a grid, you should create a grid (2D array) of objects, not a couple of int or boolean (or whatever) arrays.

First create a class, e.g.

class Cell {
  final int POSSIBILITES = 9;
  int actual_value;
  boolean[] possible_values = new boolean[POSSIBILITES]; // for each number
  Cell( int actual_value ) {
    this.actual_value = actual_value;
    for( int i = 0; i < POSSIBILITES; i++ )
      possible_values[i] = true;
  }
}

and then initialize/instantiate an array of objects

//...
final int X_SIZE = 9, Y_SIZE = 9;
Cell[][] cells = new Cell[X_SIZE][Y_SIZE];
for( int i = 0; i < X_SIZE; i++ )
  for( int j = 0; j < Y_SIZE; j++ )
    cells[i][j] = new Cell( somevalue );
//...

And then access them by, for example

//note: it's more proper to use getter/setter pattern, so this is only a crude example
if ( cells[3][6].actual_value = 7 )
  do_something();
cells[1][2].possible_values[0] = false; // we're numbering from 0 to 8, so value 1=> index 0, 2=>1... 9=>8
cells[1][2].possible_values[4] = true;
  • I see what you mean, base on your code above , can your Cell hold different values in it? – Tim Au Oct 24 '13 at 17:36
  • Your Cell (class, or even structure in e.g. C/C++) can hold *anything* you want it to hold - you can use it to store *any* data to group it together. That's one of the basics of structurized programming (and also of OOP principles). So yeah, you can certainly store different values and properties in each of the Cell objects. –  Oct 24 '13 at 17:39
  • Whats the method that can add value in to Cell[x][y] ?Can I do Cell[x][y].add(5) ? I think I mess up with String ArrayList again... – Tim Au Oct 24 '13 at 17:42
  • first tell me *why* do you want to add a value to a Cell? what does this value signify to you? why do you want to *add* a value to a Cell object? programming is (or rather, *should* be) mainly about thinking *what* to do, not *how* to do it... –  Oct 24 '13 at 17:47
  • OK ,Im doing a Sudoku program and Im not using the traditional "Try each grid" method , I will first capture what value I got in each grid(only one value in each grid),then I will create/add all possible values (which is my data)in each grid(they are 1,2,3,4,5,6,7,8,9) ,and then cancel the particular data in that grid if that data is == that grid's value. – Tim Au Oct 24 '13 at 17:54
  • First, name your variables descriptively. If you want to have *possible_values*, for example name the array **possible_value** - don't call it *data*, it's like calling a human *an animal*. Next thing, I think you use the word "grid" in the wrong meaning here. Another thing, what do you mean by *Sudoku program*? Will it *generate* a Sudoku puzzle, *solve* a Sudoku puzzle or *allow a user to solve* a Sudoku puzzle? Next thing, to "cancel" you simply set the boolean to false, as I did in the above example. Possible values are the *index* of the boolean array (index+1, strictly). Read and learn. –  Oct 24 '13 at 17:59
  • https://en.wikipedia.org/wiki/Sudoku_solving_algorithms - once again, mate, read&learn. First you design an algorithm (or memorize an existing one), only then you start implementing it... best luck on your efforts, though. –  Oct 24 '13 at 18:20