I have a question concerning Java. I started up new to Java and my google search brought many results but non was the final help.
I created a class to track historical information. I have different values for different days and need to update them un a regular basis. I want to keep track of the last 30 days and created an array with 30 elements. When I call my 'shift' function I want to drop the last n elements and put zeros in front. Here is a minial example for 5 days:
public class Testclass {
private int[] histInfo;
public Element()
{
this.histInfo = new int[5];
}
public void shift_histInfo(long m)
{
//do magic
}
}
What I want shift to do is
INPUT:
histInfo = [50,21,1,45,901]
OPERATION:
shift_histInfo(2);
RESULT:
histInfo = [0,0,50,21,1]
I am thankfull for every kind of help you can support as well for thought-provoking impulses if you think that there is a way more elegant or efficient way.
Best :-)
Unless there are very tight performance constraints using the standard Collection classes will get the job done. Have a look at java.util.LinkedList.
As a programming exercise you might consider creating a ring buffer. The idea being to avoid copying the array on every insertion.
Keep a oldestIndex value.
When writing simply replace item[oldestIndex] and increment oldestIndex.
To iterate you start at oldestIndex and use an increment method to deal with wrapping round to the start of the array.
int nextIndex(int current) {
return (current + 1) % arrayLength;
}
Writing a nice encapsulating class to hide all this would be a good exercise.
You can try this :
public static void shift_histInfo(long m)
{
int[] myIntArray = {50,21,1,45,901};
int[] myIntArray2 = {50,21,1,45,901};
for (int j=0 ;j< myIntArray.length ; j++){
int temp = (int) (j+m);
if (temp >= myIntArray.length){
temp = temp - myIntArray.length;
myIntArray2[temp] = 0;
} else {
myIntArray2[temp] = myIntArray[j];
}
}
for (int j=0 ;j< myIntArray2.length ; j++){
System.out.println(myIntArray2[j]);
}
}
Output :
when shift_histInfo(2) ,
[0,0,50,21,1]
int[] array={1,2,3,4,5,6};
int removelength=2;
int e=1;
while(e<=removelength) {
for(int i=1;i<array.length;i++)
array[array.length-i]=array[array.length-i-1];
e++;
}
for(int i=0;i<removelength;i++) {
array[i]=0;
}
for(int g:array)
{
System.out.print(g);
}
For constraints that you wanted, although I did initialise the data in the same method instead of Element(). I don't know why the parameter is of type long so I left it and made an int local variable.
All it does is copy the index value over to the new array starting at m then increments/iterates until the end of the array.
You can also make the method return type int[] and then simply return changedInfo array. Instead of histInfo = changedInfo.clone();
private int[] histInfo;
public void shift_histInfo(long m) {
int n = (int) m;
this.histInfo = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15};
int length = this.histInfo.length;
int[] changedInfo = new int[length];
if (length - n >= 0) System.arraycopy(histInfo, 0, changedInfo, n + 0, length - n); //Edit: shortened to one line.
histInfo = changedInfo.clone();
System.out.println("Remove: " + n + " - " + Arrays.toString(changedInfo) + "\n");
}
public static void main(String[] args) {
Main main = new Main();
main.shift_histInfo(0);
main.shift_histInfo(30);
main.shift_histInfo(1);
main.shift_histInfo(15);
main.shift_histInfo(29);
}
println:
Remove: 0 - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Remove: 30 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Remove: 1 - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Remove: 15 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Remove: 29 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
Related
I am trying to implement a Sudoku solver using Java. This is the code I've written as of now. If I try to run it, it goes on to an endless loop that keeps on printing the first row of the Sudoku board, and that too with an incorrect solution. I guess I'm implementing backtracking the incorrect way over here. I think I am printing the final and wrong as well, as only the first row is printed every time. Can someone please help me fix my code and tell me as to where I am going wrong?
public static void display(int[][] board) {
for(int[] arr : board) {
System.out.println(Arrays.toString(arr));
return;
}
}
public static boolean isSafe(int[][] board, int row, int col, int i) {
//check row
for(int a=0; a<board.length; a++) {
if(board[a][col]==i) {
return false;
}
}
//check col
for(int b=0; b<board.length; b++) {
if(board[row][b]==i) {
return false;
}
}
//check cell
int strow = row-(row%3);
int stcol = col-(col%3);
for(int x=strow; x<strow+3; x++) {
for(int y=stcol; y<stcol+3; y++) {
if(board[x][y]==i) {
return false;
}
}
}
return true;
}
public static void sudoku(int[][] board, int row, int col) {
if(row==board.length) {
display(board);
System.out.println();
return; //modify this to print ans
}
if(col==board.length) {
sudoku(board, row+1, 0);
return;
}
if(board[row][col]==0) {
for(int i=1; i<=9; i++) {
if(isSafe(board, row, col, i)) {
board[row][col]=i;
sudoku(board, row, col+1);
board[row][col]=0;
}
}
}
sudoku(board, row, col+1);
}
public static void main(String args[]) {
int[][] board=
{ {3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0} };
sudoku(board, 0, 0);
}
A bactracking algorithm can be applied here and the problem happens in your sudoku method.
First of all we can just pass the board and we don't want to pass the row and col.
We can just pass the board and the just traverse through each and every cell.
Only consider those cells that are 0's.
We don't want to consider any other cells as 0"s are the cells we are interested in.
Now if we see a cell which is 0, we try to find all the different possibilites from 1 to 9 which can fit in that cell and apply the 'isSafe()` logic which will just do the same check.
And we backtrack and continue with our checking.
public static void display(int[][] board) {
for(int[] arr : board) {
System.out.println(Arrays.toString(arr));
}
}
public static boolean isSafe(int[][] board, int row, int col, int i) {
//check row
for(int a=0; a<board.length; a++) {
if(board[a][col]==i) {
return false;
}
}
//check col
for(int b=0; b<board.length; b++) {
if(board[row][b]==i) {
return false;
}
}
//check cell
int strow = row-(row%3);
int stcol = col-(col%3);
for(int x=strow; x<strow+3; x++) {
for(int y=stcol; y<stcol+3; y++) {
if(board[x][y]==i) {
return false;
}
}
}
return true;
}
public static boolean sudoku(int [][] board) {
for (int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
int current = board[i][j];
if (current == 0) {
for (int ch = 1; ch <= 9; ch++) {
if (isSafe(board, i, j, ch)) {
board[i][j] = ch;
if (sudoku(board)) {
return true;
}
board[i][j] = 0;
}
}
return false;
}
}
}
return true;
}
public static void main(String args[]) {
int[][] board=
{ {3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0} };
sudoku(board);
display(board);
}
Here is the output after the change
[3, 1, 6, 5, 7, 8, 4, 9, 2]
[5, 2, 9, 1, 3, 4, 7, 6, 8]
[4, 8, 7, 6, 2, 9, 5, 3, 1]
[2, 6, 3, 4, 1, 5, 9, 8, 7]
[9, 7, 4, 8, 6, 3, 1, 2, 5]
[8, 5, 1, 7, 9, 2, 6, 4, 3]
[1, 3, 8, 9, 4, 7, 2, 5, 6]
[6, 9, 2, 3, 5, 1, 8, 7, 4]
[7, 4, 5, 2, 8, 6, 3, 1, 9]
Note : I changed the return type to true or false inorder to make sure that a particular cell can be filled with a number say x. If its possible we return true and continue with the next cell which is 0, other wise we backtrack and check for other possibilities.
Updates :
The only change you are missing is an else block at the very end because even if the cell is 0 or any other number you are doing recursion sudoku(board, row, col+1);. So just enclose that statement in the else block and will give the desired output.
Code change without changing the return type:
public static void display(int[][] board) {
for(int[] arr : board) {
System.out.println(Arrays.toString(arr));
}
}
public static boolean isSafe(int[][] board, int row, int col, int i) {
//check row
for(int a=0; a<board.length; a++) {
if(board[a][col]==i) {
return false;
}
}
//check col
for(int b=0; b<board.length; b++) {
if(board[row][b]==i) {
return false;
}
}
//check cell
int strow = row-(row%3);
int stcol = col-(col%3);
for(int x=strow; x<strow+3; x++) {
for(int y=stcol; y<stcol+3; y++) {
if(board[x][y]==i) {
return false;
}
}
}
return true;
}
public static void sudoku(int[][] board, int row, int col) {
if(row==board.length) {
display(board);
System.out.println();
return; //modify this to print ans
}
if(col==board.length) {
sudoku(board, row+1, 0);
return;
}
if(board[row][col]==0) {
for(int i=1; i<=9; i++) {
if(isSafe(board, row, col, i)) {
board[row][col]=i;
sudoku(board, row, col+1);
board[row][col]=0;
}
}
}
else
sudoku(board, row, col+1);
}
public static void main(String args[]) {
int[][] board=
{ {3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0} };
sudoku(board, 0, 0);
}
Output :
[3, 1, 6, 5, 7, 8, 4, 9, 2]
[5, 2, 9, 1, 3, 4, 7, 6, 8]
[4, 8, 7, 6, 2, 9, 5, 3, 1]
[2, 6, 3, 4, 1, 5, 9, 8, 7]
[9, 7, 4, 8, 6, 3, 1, 2, 5]
[8, 5, 1, 7, 9, 2, 6, 4, 3]
[1, 3, 8, 9, 4, 7, 2, 5, 6]
[6, 9, 2, 3, 5, 1, 8, 7, 4]
[7, 4, 5, 2, 8, 6, 3, 1, 9]
I am working on a game in Java and I need to cache some position-based information. If I have the following array:
int[][] array = {
{1, 2, 3},
{1, 2, 3},
{1, 2, 3}
};
And then I have this other array:
int[][] otherArray = {
{4, 5, 6},
{4, 5, 6},
{4, 5, 6}
};
Now I want to combine them in a special way. I want to add otherArray to the left of array. So the result would look like this:
int[][] combinedArray = {
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3}
};
Then I have this other combined array:
int[][] otherCombinedArray = {
{30, 17, 139, 65, 335, 99},
{50, 43, 57, 53, 423, 534},
{90, 67, 78, 24, 99, 67}
};
Now I want to add it to the top of the original combined array. So the final result would look like this:
int[][] finalCombinedArray = {
{30, 17, 139, 65, 335, 99},
{50, 43, 57, 53, 423, 534},
{90, 67, 78, 24, 99, 67},
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3},
{4, 5, 6, 1, 2, 3}
};
Can someone point me to a good library or built in method for doing this? I also wanted to note that the method shouldn't be too computationally heavy (like looping through all the arrays multiple times, and it shouldn't use too much memory, like 80MB).
Thank you for the help!
On it's own, Java does not provide concatenation methods, but we can use System.arraycopy (as suggested by #user16320675 above)
With System.arraycopy you specify the array to copy + the destination array -> you have array A of size 10 and B of size 2, and you use the command to copy your B array into A.
int[] source = { 1,2,3,4,5 };
int[] destination = new int[10];
// You can play with the numbers below
int COPY_AT_INDEX = 0; // Start to copy at position destination[0]
int AMOUNT_TO_COPY = 5; // Copying from 0 to source.length
System.arraycopy(source, 0, destination, COPY_AT_INDEX, AMOUNT_TO_COPY);
System.out.println(Arrays.toString(source)); // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(destination)); // [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
Now, if we use arraycopy, seems you have to determine when to copy as rows, and when to copy as columns.
// Assuming a[][] and b[][] have the same size.
public int[][] mergeAsColumns(int[][] a, int[][] b) {
int rows = a.length;
int columns = a[0].length;
int[][] merged = new int[rows][2 * columns];
for (int i = 0; i < a.length; i++) {
System.arraycopy(a[i], 0, merged[i], 0, columns);
System.arraycopy(b[i], 0, merged[i], rows, columns);
}
return merged;
}
Merging as Rows is similar to the other one, but changes in which positions you want to affect and how you create the merged array.
// Assuming a[][] and b[][] have the same size.
public int[][] mergeAsRows(int[][] a, int[][] b) {
int rows = a.length;
int columns = a[0].length;
int[][] merged = new int[2 * rows][columns];
for (int i = 0; i < rows; i++) {
System.arraycopy(a[i], 0, merged[i], 0, columns);
System.arraycopy(b[i], 0, merged[rows + i], 0, columns);
}
return merged;
}
I've written this binary search algorithm based on recursion but I'm unable to understand the reason for the error.
// Only a sorted array must be entered for binary search to work
public int binarySearch(int searchFor, int[] inArray, int from, int to){
if (to >= from){
int mid = (to-from)/2 + from;
if (inArray[mid] == searchFor){
return inArray[mid];
} else if (inArray[mid] < searchFor){
binarySearch(searchFor, inArray, ++mid, to);
} else if (inArray[mid] > searchFor){
binarySearch(searchFor, inArray, from, ++mid);
}
}
return -1;
}
Exception in thread "main" java.lang.StackOverflowError
at search.binarySearch(search.java:24)
at search.binarySearch(search.java:24)
at search.binarySearch(search.java:24)
at search.binarySearch(search.java:24)
A few issues and remarks:
In the last case, the value ++mid is wrong as argument for binarySearch. That value should be one less than mid in that case.
I would also vote against the use of ++mid or --mid here, as that suggests that it is important that mid changes value, which is not needed. You should just pass mid+1 in the first case, and mid-1 in the second.
binarySearch returns an int, but when your code calls it recursively it doesn't do anything with that return value.
You should return that value. So:
} else if (inArray[mid] < searchFor){
return binarySearch(searchFor, inArray, mid+1, to);
// ^^^^^^ ^^^^^
} else if (inArray[mid] > searchFor){
return binarySearch(searchFor, inArray, from, mid-1);
// ^^^^^^ ^^^^^
}
The expression (to-from)/2 + from is a verbose way of doing (from+to)/2...
Apart from adding return to the recursive calls of binarySearch, there are a couple of flaws in the logic:
mid should be decremented to catch values in the left part:
return binarySearch(searchFor, inArray, from, --mid);
there should be a check for valid mid, from, to values to fit inside the input array
Thus, the method should look as:
public static int binarySearch(int searchFor, int[] inArray, int from, int to) {
if (to >= from && from > -1 && to <= inArray.length) {
int mid = (to-from)/2 + from;
if (mid >= inArray.length) {
return -1;
}
// System.out.printf("from=%d to=%d mid=%d val=%d%n", from, to, mid, inArray[mid]); // debug print
if (inArray[mid] == searchFor) {
return inArray[mid];
} else if (inArray[mid] < searchFor){
return binarySearch(searchFor, inArray, ++mid, to);
} else {
return binarySearch(searchFor, inArray, from, --mid);
}
}
return -1;
}
}
Tests:
public static int binarySearch(int searchFor, int... inArray) {
System.out.printf("Searching for %d in %s%n", searchFor, Arrays.toString(inArray));
return binarySearch(searchFor, inArray, 0, inArray.length);
}
System.out.println(binarySearch(10));
System.out.println(binarySearch(10, 10));
System.out.println(binarySearch(10, 1));
System.out.println(binarySearch(10, 0, 1, 5, 8, 10, 21));
System.out.println(binarySearch( 0, 0, 1, 5, 8, 10, 21));
System.out.println(binarySearch(21, 0, 1, 5, 8, 10, 21));
System.out.println(binarySearch(10, 0, 1, 3, 5, 8, 10, 21));
System.out.println(binarySearch( 0, 0, 1, 5, 8, 10, 15, 21));
System.out.println(binarySearch(21, 0, 1, 5, 8, 10, 16, 21));
System.out.println(binarySearch(30, 0, 1, 5, 8, 10, 21));
System.out.println(binarySearch(-1, 0, 1, 5, 8, 10, 21));
System.out.println(binarySearch(7, 0, 1, 5, 8, 10, 21));
Output:
Searching for 10 in []
-1
Searching for 10 in [10]
10
Searching for 10 in [1]
-1
Searching for 10 in [0, 1, 5, 8, 10, 21]
10
Searching for 0 in [0, 1, 5, 8, 10, 21]
0
Searching for 21 in [0, 1, 5, 8, 10, 21]
21
Searching for 10 in [0, 1, 3, 5, 8, 10, 21]
10
Searching for 0 in [0, 1, 5, 8, 10, 15, 21]
0
Searching for 21 in [0, 1, 5, 8, 10, 16, 21]
21
Searching for 30 in [0, 1, 5, 8, 10, 21]
-1
Searching for -1 in [0, 1, 5, 8, 10, 21]
-1
Searching for 7 in [0, 1, 5, 8, 10, 21]
-1
I am trying to write a sudoku "solver" and netbeans is giving me 3 error messages relating to StringBuilder. I had different files for each class but it wouldn't compile then either. Every site i have visited has suggested the way I am doing it. Please help.
public class Sudoku {
public static void main(String[] args) {
int[][] mainpuzzle =
{{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 81; i++) {
builder.append("09123456789"); // 11
}
for (int j = 0;j < 9; j++ ) {
for ( int k = 0;k < 9;k++ ) {
if ( mainpuzzle [j][k] != 0 ) {
String replace1 = String.valueOf(mainpuzzle[j][k]);
builder.replace((j*11) + (k*11), (j*11) + (k*11),
replace1);
}
}
}
GetSquareCandidates getSqCandid = new GetSquareCandidates();
StringBuilder builderMarkup = new StringBuilder();
builderMarkup = getSqCandid(builder);
}
}
class GetSquareCandidates {
public StringBuilder GetSquareCandidates(StringBuilder boxPassed)
StringBuilder boxRet = new StringBuilder();
boxRet = boxPassed;
return boxRet;
}
}
Looking at your code, there are a couple of things missing like below
GetSquareCandidates class's GetSquareCandidates method does not have an open curly brace.
getSqCandid(StringBuilder builder) method not defined in your GetSquareCandidates class.
Other than these, please share compilation error messages.
There are only a few errors please find below:
Case 1. If public StringBuilder GetSquareCandidates(StringBuilder boxPassed) is a constructor then it can't return.
Case 2. If it is method then kindly do below changes:
Call the method using builderMarkup = getSqCandid.GetSquareCandidates(builder);
Add { after public StringBuilder GetSquareCandidates(StringBuilder boxPassed).
It is recommended using method name like getSquareCandidates().
import java.util.Scanner;
public class SudokuPermuter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Sudoku Permuter.\n");
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
}
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 10; column++) {
System.out.print(sudoku[row] [column] + " ");
}
System.out.println();
}
}
}
I'm trying to enter a sudoku puzzle to be printed out, and it says there's a semicolon expected after the last line { 0, 9, 0, 7, 0, 1, 0, 4, 0 }
But when I put the semicolon in, it takes the bracket as ending the whole String args method. I don't know why.
You need the semicolon after the second closing bracket (to end the assignment statement):
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
} ;
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
};
No need for the constructor. It can be instantiated like this...
int[][] sudoku = {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
} ;
also your Array throwing Exception ArrayIndexOutOfBoundsException
you need to make your array bigger or change the loops to 9 inested of 10
I think you should altogether choose a more convenient way of representing your
Sudoku when filling a table by hand : something like an array of
string, that you split later, is for instance more convenient to fill-in.
After the (very simple) conversion, you have your array.
(Well i used Javascript notation here, conversion should not be hard.)
var oneSudokuStrings = [];
var sl=0;
oneSudokuStrings [sl++] = " O 8 0 4 0 2 0 6 0 ";
oneSudokuStrings [sl++] = " 0 3 4 0 0 0 9 1 0 ";
oneSudokuStrings [sl++] = " 9 6 0 0 0 0 0 8 4 ";
oneSudokuStrings [sl++] = " 0 0 0 2 1 6 0 0 0 ";
oneSudokuStrings [sl++] = " 2 0 0 0 0 9 6 0 0 ";
oneSudokuStrings [sl++] = " 0 1 0 3 5 7 0 0 8 ";
oneSudokuStrings [sl++] = " 8 4 0 0 0 0 0 7 5 ";
oneSudokuStrings [sl++] = " 0 2 6 0 0 0 1 3 0 ";
oneSudokuStrings [sl++] = " 0 9 0 7 0 1 0 4 0 ";
var oneSudoku = [];
for (var i=0; i<oneSudokuStrings.length; i++) {
oneSudoku[i]=oneSudokuStrings[i].match(/[^ ]+/g);
}