Results 1 to 4 of 4
Thread: Traversing a list and comparing
- 04-03-2010, 08:12 AM #1
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
Traversing a list and comparing
Hi all,
I'm working on a java Hanjie solver and this is part of the code. Everything else seems okay but I always get an error when I run it.
Eclipse (i'm using Galileo version) points the exception to be at:
The section of the code is:Java Code:char a = combo1.get(i).charAt(i);
combo1Java Code:for ( i = 0; i < line.size(); i++) { boolean same = false; char a = combo1.get(0).charAt(i); int postn = 0; for (int x = 1; x < combo1.size(); x++) { if (combo1.get(x).charAt(i) == a) { same = true; postn = x; } }
...refers to a List<String> of all possible combinations of black and white assignments (where '1' represents black and white '0').
Essentially, this code snippet supposedly picks the first item (a string of 1s and 0s), gets the character at a particular position and compares it with the characters in the same position in the rest of the items in the list. Apparently, if they overlap, then this assignment should be applied to the line.
I'm assuming the error (which Eclipse gives as java.lang.IndexOutOfBoundsException) must be to do with something other than this particular line but I'm just curious if there IS anything wrong with this part itself.
There's a whole lot of code surrounding this one and I don't want to paste the whole thing lest it confuses myself and anyone else more.
It looks like I will have to anyway if there's nothing with this code in particular...
And another question; when removing an item from a list, does everything after/before it shift up/down?
Thanks in advance
- 04-03-2010, 08:30 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
We'll need to see the rest of the code. The code seems to be assuming that combo1.get(0) has at least as many characters as line (whatever it is) has elements. And that assumption seems to be faulty.
-Gary-
- 04-03-2010, 08:49 AM #3
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
Here's the rest of the code...
Thanks for the reply Gary, here's the rest, or rather, all of the code (I'm not sur if I'm allowed to do that?) References to other classes not in the code are from another package:
Java Code:public class MyHanjieSolver3 implements Solver { boolean row; boolean column; int last; public MyHanjieSolver3() { this.row = false; this.column = true; this.last = -1; } //******************************************************************************************************************************************************************** @Override public void checkConsistency(Grid hanjie) throws HanjieException { // TODO Auto-generated method stub int i, j; String line = null; Value v; if (column) { for (i = 0; i < hanjie.getHeight(); i++) { for (j = 0; j < hanjie.getHeight(); j++) { v = hanjie.getColumn(i).getCellAt(j).getValue(); if (v == Value.BLACK) line = line + "1"; else if (v == Value.WHITE) line = line + "0"; else if (v == Value.UNKNOWN) line = line + "2"; } Pattern pattern = hanjie.getColumn(i).getConstraintsAsRegularExpression(); boolean matched = pattern.matcher(line).matches(); if (!matched) throw new HanjieException(); } } else { for (i = 0; i < hanjie.getWidth(); i++) { for (j = 0; j < hanjie.getWidth(); j++) { v = hanjie.getRow(i).getCellAt(j).getValue(); if (v == Value.BLACK) line = line + '1'; else if (v == Value.WHITE) line = line + '0'; else if (v == Value.UNKNOWN) line = line + '2'; } Pattern pattern = hanjie.getRow(i).getConstraintsAsRegularExpression(); boolean matched = pattern.matcher(line).matches(); if (!matched) throw new HanjieException(); } } } //******************************************************************************************************************************************************************** @Override //Checks if hanjie puzzle is complete... public boolean isComplete(Grid hanjie) { // TODO Auto-generated method stub //return false; for(int i = 0; i < hanjie.getWidth(); i++) { for(int j = 0; j < hanjie.getHeight(); j++) { if(hanjie.getColumn(i).getCellAt(j).getValue() == Value.UNKNOWN) return false; } } for(int i = 0; i < hanjie.getHeight(); i++) { for(int j = 0; j < hanjie.getWidth(); j++) { if(hanjie.getRow(i).getCellAt(j).getValue() == Value.UNKNOWN) return false; } } return true; } //******************************************************************************************************************************************************************** @Override public Line solveNext(Grid hanjie) { // TODO Auto-generated method stub this.last += 1; Line line = null; Line tempLine = null; if(this.row == true) line = hanjie.getRow(last); if(this.column == true) line = hanjie.getColumn(last); //******************************************************************** if (this.column) { line = hanjie.getColumn(this.last); tempLine = drawLine(hanjie, this.column, line); line = tempLine; } else { line = hanjie.getRow(this.last); tempLine = drawLine(hanjie, this.column, line); line = tempLine; } //******************************************************************** if(this.row == true && this.last == hanjie.getHeight() - 1) { this.row = false; this.column = true; this.last = -1; } if(this.column == true && this.last == hanjie.getWidth() - 1) { this.row = true; this.column = false; this.last = -1; } return line; } //******************************************************************************************************************************************************************** public static Line drawLine (Grid hanjie, boolean column, Line line) { int i; //list one of all combinations... List<String> combo1 = new ArrayList<String>(); //creates list of possible all combinations using combos() method... if (column)//if line is a column, create combinations to fit vertically... combos1(hanjie, hanjie.getHeight(), combo1); else //if line is a row, create combinations to fit horizontally... combos1(hanjie, hanjie.getWidth(), combo1); //******************************************************************** //going through combo1 and removing combinations inconsistent with assignments already made on the cells.... if (column) { combos2(hanjie, hanjie.getHeight(), combo1, line); } else { combos2(hanjie, hanjie.getWidth(), combo1, line); } //******************************************************************** //combo1 has been updated... //now eliminating possible combinations that are inconsistent with the current line's constraints... String match = null; for (i = 0; i < line.size(); i++) { if (line.getCellAt(i).getValue() == Value.BLACK) match = match + '1'; else if (line.getCellAt(i).getValue() == Value.WHITE) match = match + '0'; else match = match + '2'; } Pattern pattern = line.getConstraintsAsRegularExpression(); for (i = 0; i < combo1.size(); i++)//goes through list { boolean matched = pattern.matcher(match).matches(); if (!matched) combo1.remove(i);//removes combination from list... } //******************************************************************** //combo1 has been updated... //now finding overlapping assignments in the possible combinations leftover from the eliminations... for ( i = 0; i < line.size(); i++) { boolean same = false; char a = combo1.get(0).charAt(i); int postn = 0; for (int x = 1; x < combo1.size(); x++) { if (combo1.get(x).charAt(i) == a) { same = true; postn = x; } } if (same == true) { if (a == '1') line.getCellAt(postn).setValue(Value.BLACK); else if (a == '0') line.getCellAt(postn).setValue(Value.WHITE); } else line.getCellAt(postn).setValue(Value.UNKNOWN); } //******************************************************************** return line; } //******************************************************************************************************************************************************************** //method to create a list of all possible combinations... //method asks for the current hanjie puzzle, line's size and current list to update... public static void combos1 (Grid hanjie, int j, List<String> combo1) { String str; for (int i = 0; i < j; i++) { str = Integer.toBinaryString(i); str = zeroFiller(Integer.toBinaryString(i),j,str.length()); //converts to binary and fills in the leading zeros... combo1.add(str); } } //******************************************************************************************************************************************************************** //method to eliminate/remove possible combinations that are inconsistent with assignments already made on current line... //method asks for the current hanjie puzzle, line's size, current list to update and current line... public static void combos2 (Grid hanjie, int j, List<String> combo1, Line line) { for (int i = 0; i < j; i++)//go through entire line... { for (int x = 0; x < combo1.size(); x++)//go through entire list... { if (((combo1.get(x).charAt(i) == '1')&& (line.getCellAt(i).getValue() == Value.BLACK)) || ((combo1.get(x).charAt(i) == '0')&& (line.getCellAt(i).getValue() == Value.WHITE))) { combo1.remove(x); } } } } //******************************************************************************************************************************************************************** //method to add zeros in front of combinations to get correct length.. //method asks for string to fill, length/width of current column/row and length of string to fill... public static String zeroFiller (String tofill, int hanjiex, int i) { if ( i == hanjiex ) return tofill; else { int j = hanjiex - i; for (int k=0; k<j; k++) { tofill = "0" + tofill; } return tofill; } } }
- 04-03-2010, 10:49 AM #4
Member
- Join Date
- Apr 2010
- Location
- At my desk
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
[UnSolved] Traversing Trees problem
By RllyNew2Java in forum New To JavaReplies: 8Last Post: 12-24-2009, 07:59 PM -
Comparing List of maps
By thorne_ in forum New To JavaReplies: 1Last Post: 06-10-2009, 02:30 AM -
Traversing a JTabbedPane
By Inks in forum AWT / SwingReplies: 12Last Post: 03-11-2009, 05:15 AM -
Traversing through a stack of objects, and puttin them info in an array
By szimme101 in forum New To JavaReplies: 1Last Post: 03-25-2008, 05:06 AM -
Traversing a directory
By Java Tip in forum Java TipReplies: 0Last Post: 01-14-2008, 09:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks