java - In search of some guidance regarding HashMap -
i'm making sudoku solver program in java, uses backtracking , bruteforce solving algorithm. problem seem having when insert correct values in simple container, wrong values showing when try access class.
hashmap has correct values in correct locations when print out inside insert method in sudokucontainer
, (with for(int = 0 < dim; i++) + for(int j = 0; j < dim; j++)
) followed square[][] tmp = solutionshash.get(count)
, system.out.println(tmp[i][j].getvalue());
. it's successful when use same for-loops right after solutions.insert
statement in square
class' fillinnremainingofboard()
(followed square[][] tmp = solutions.solutionshash.get(count)
, same system.out.println
statement.
the problem occurs when try same board
class, can see in test() method in class. prints out predefined values(or nothing if board empty). difference use i.e. square[][] tmp = allsquares[0][0].solutionshash.get(wantednumber)
, same before. mentioned, don't see why should make difference, when solutionshash
declared static in sudokucontainer
-class.
any appriciated. thanks!
note: indentation got screwed somehow, sorry that. haven't included gui part because haven't written , possibly don't have permission put on here. problem occurs before that(possibly in board
class), same values printed out there board
class.
edit: removed unnecessary code.
public class square { static sudokucontainer solutions; protected char value; protected int valueint; protected static square allsquares[][]; public int count = 0; protected boolean predefined = false; protected boolean lastsquare = false; protected square next = null; public void setnext() { if(col.getnr() < col.dim) { next = allsquares[row.getnr()-1][col.getnr()]; }else if(col.getnr() == col.dim) { if(row.getnr() < row.dim) { next = allsquares[row.getnr()][0]; }else { next = null; lastsquare = true; } } if(next != null) { if(next.col.getnr() == col.dim && next.row.getnr() == col.dim && next.predefined == true) lastsquare = true; } } public void fillinnremainingofboard(square[][] allsquares, int hd, int br) { this.allsquares = allsquares; int highestnumber = col.dim; setnext(); if(value == '\u0020') { for(int = 1; <= highestnumber; i++) { if(row.islegal[i-1] && col.islegal[i-1] && box.islegal[i-1]) { string s1 = integer.tostring(i); value = s1.charat(0); insert(value); insertint(i); box.islegal[i-1] = false; col.islegal[i-1] = false; row.islegal[i-1] = false; if(!lastsquare) { next.fillinnremainingofboard(this.allsquares, hd, br); }else{ system.out.println("solution found: " + count); solutions.insert(allsquares, count, col.dim); ++count; } // slutt paa else box.islegal[valueint-1] = true; col.islegal[valueint-1] = true; row.islegal[valueint-1] = true; value = '\u0020'; } // slutt paa if(row.islegal[i-1] && col.islegal[i-1] && box.islegal[i-1]) } // slutt paa if (value == '\u0020' || !predefined) }else { if(!lastsquare) { next.fillinnremainingofboard(this.allsquares, hd, br); } else { system.out.println("solution found"); solutions.insert(allsquares, count, col.dim); count++; } } // slutt paa else if(predefined) // slutt paa for(int = 1; <= highestnumber; i++) }// returnerer vi til forrige metode public void insert(char value) { if(value != '.') { this.value = value; }else{ this.value = '\u0020'; // in other words; space(in unicode) } } public void insertint(int value) { valueint = value; } public char getvalue() { return value; } } public class sudokucontainer { int dim = 0; private int count = 0; static hashmap<integer, square[][]> solutionshash = new hashmap<integer, square[][]>(); public void insert(square[][] allsquares, int count, int dim) { this.count = count; this.dim = dim; if(count <= 499) { solutionshash.put(count,allsquares); } } public square[][] get(int nr) { square[][] tmp = solutionshash.get(nr); return tmp; } public int getsolutioncount() { return solutionshash.size(); } } public class board { int dim, br, hd; char[][] chararray; char[] chararray1; static square [][] allsquares; row [] rows; column[] columns; box[] boxes; public board(int dim, int br, int hd, char[][] chararray) { this.dim = dim; this.br = br; this.hd = hd; this.chararray = chararray; chararray1 = new char[dim]; allsquares = new square[dim][dim]; rows = new row[dim]; columns = new column[dim]; boxes = new box[dim/br * dim/hd]; setrows(); setcolumns(); setboxes(); setsquares(); insertboxinsquares(); fillinlegalvalues(); welcome(); solve(); test(); system.out.println("found solutions. please refer graphical interface."); showgui(); } public void test() { for(int = 0; < dim; i++) { for(int j = 0; j < dim; j++) { //square[][] tmp = allsquares[0][0].solutions.solutionshash.get(0); //system.out.println(tmp[i][j].getvalue()); } } } public void solve() { allsquares[0][0].fillinnremainingofboard(allsquares, hd, br); } public void showgui() { new sudokugui(dim, hd, br, allsquares[0][0].solutions.solutions, false, 0, allsquares[0][0].solutions.solutionshash); } }
i can tell problem is, though not causes it.
you either printing out different objects, or object's values changing between 1 print , another. can identify object printing either hashcode or tostring() call on object, , therefore tell whether same object in different cases.
if they're different, check aren't loading object temporary scope in 1 case , printing out 1 bigger scope in next case. it's possible you're referring different object; cannot tell what's going on code posted, possible got confused yourself.
your post's title reason this. not feasible "values in [the] hashmap not correct". when 1 new programming, , years afterwards, half of debugging believing (1) programming language constructs work advertised, , (2) screwed somewhere.
Comments
Post a Comment