[英]New Object in a loop is getting overwritten in Java
所以我正在嘗試創建一個新對象以添加到列表中
public void findWord(char boggle[][], boolean visited[][], int i,
int j, String str)
{
visited[i][j] = true;
str = str + boggle[i][j];
if (hasWord(str)) {
for (char c : str.toCharArray()) {
Position<Character> pos = new Position<>(c, i, j);
list.add(pos);
}
}
for (int row = i - 1; row <= i + 1 && row < 4; row++)
for (int column = j - 1; column <= j + 1 && column < 4; column++)
if (row >= 0 && column >= 0 && !visited[row][column])
findWord(boggle, visited, row, column, str);
visited[i][j] = false;
}
問題是基本上每個位置(行和列)都被這個對象的最后一個實例所覆蓋,但元素本身不是。
我的職位類:
public class Position<T> {
private T element;
private int row;
private int column;
Position () {
this(null,0,0);
}
Position (T element) {
this.element = element;
}
Position (T element, int row, int column) {
this.element = element;
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public T getElement() {
return element;
}
public String toString() {
return element + "(" + Integer.toString(row) + "," + column + ")";
}
也就是說,我打印后的 List 輸出類似於:
a(2,2) a(2,2) r(2,2) o(2,2) n(2,2)
最后一個位置在 Element 和索引中都是正確的,但所有其他位置在矩陣索引中都失敗了。 應該 :
a(0,0) a(1,0) r(1,1) o(1,2) n(2,2)
孔類:
import java.io.*;
public class Boogle {
LinkedList<Position<Character>> list = new LinkedList<>();
static QuadHashTable<String> table = new QuadHashTable<>();
String word;
static char matrix [] [] = {
{'a', '-', '-', '-'},
{'a', 'r', 'o', '-'},
{'-', '-', 'n', '-'},
{'-', '-', '-', '-'}
};
public Boogle () {
this(null);
}
public Boogle (char matrix [] []) {
this.matrix = matrix;
}
public boolean hasWord (String s) {
return s.equals(table.search(s));
}
public void findWordsUtil(char boggle[][], boolean visited[][], int i,
int j, String str)
{
visited[i][j] = true;
str = str + boggle[i][j];
if (hasWord(str))
for (char c : str.toCharArray()) {
Position<Character> pos = new Position<>(c, i, j);
list.add(pos);
}
for (int row = i - 1; row <= i + 1 && row < 4; row++)
for (int column = j - 1; column <= j + 1 && column < 4; column++)
if (row >= 0 && col >= 0 && !visited[row][column])
findWordsUtil(boggle, visited, row, column, str);
visited[i][j] = false;
}
public LinkedList<Position<Character>> solve () {
String s = "";
boolean visited[][] = new boolean[4][4];
String str = "";
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
findWordsUtil(matrix, visited, i, j, str);
return list;
}
public static void main (String args []) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/home/dsolipa/Desktop/allWords.txt"));
String line;
while((line = br.readLine()) != null) {
table.insert(line);
}
Boogle boogle = new Boogle(matrix);
System.out.println(boogle.solve());
//System.out.println(boogle.hasWord("or"));
}
}
似乎只有當字符串是if (hasWord(str))
傳遞if (hasWord(str))
條件,並且您的 for 循環正在遍歷字符串str並在 n 的位置添加所有字符,即 (2,2)。 重新檢查 QuadHashTable 類中的實現。
請進行以下更改:
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class Boogle {
LinkedList<Position<Character>> list = new LinkedList<>();
LinkedList<Position<Character>> table = new LinkedList<>();
Map indexMap = new HashMap();
static QuadHashTable<String> table = new QuadHashTable<>();
String word;
static char matrix [] [] = {
{'a', '-', '-', '-'},
{'a', 'r', 'o', '-'},
{'-', '-', 'n', '-'},
{'-', '-', '-', '-'}
};
public Boogle () {
this(null);
}
public Boogle (char matrix [] []) {
this.matrix = matrix;
}
public boolean hasWord(String s) {
return s.equals(table.search(s));
}
public void findWordsUtil(char boggle[][], boolean visited[][], int i,
int j, String str)
{
visited[i][j] = true;
str = str + boggle[i][j];
if (hasWord(str))
{
for (char c : str.toCharArray())
{
ArrayList coordinates = (ArrayList)indexMap.get(c);
String xy = (String)coordinates.get(0);
if(coordinates.size() !=1) // or check with flag in visited matrix
{
coordinates.remove(xy);
indexMap.put(c,coordinates);
}
String [] rowCol = xy.split(":");
int x = Integer.parseInt(rowCol[0]);
int y = Integer.parseInt(rowCol[1]);
Position<Character> pos = new Position<>(c, x, y);
list.add(pos);
}
return;
}
for (int row = i - 1; row <= i + 1 && row < 4; row++)
for (int column = j - 1; column <= j + 1 && column < 4; column++)
if (row >= 0 && column >= 0 && !visited[row][column])
findWordsUtil(boggle, visited, row, column, str);
visited[i][j] = false;
}
public LinkedList<Position<Character>> solve () {
String s = "";
boolean visited[][] = new boolean[4][4];
String str = "";
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
if(indexMap.get(matrix[i][j])!=null)
{
ArrayList list = (ArrayList) indexMap.get(matrix[i][j]);
list.add(i+":"+j);
indexMap.put(matrix[i][j],list);
}else if(matrix[i][j]!='-')
{
ArrayList list = new ArrayList();
list.add(i+":"+j);
indexMap.put(matrix[i][j],list);
}
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
findWordsUtil(matrix, visited, i, j, str);
return list;
}
public static void main (String args []) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/home/dsolipa/Desktop/allWords.txt"));
String line;
while((line = br.readLine()) != null) {
table.insert(line);
}
Boogle boogle = new Boogle(matrix);
System.out.println(boogle.solve());
//System.out.println(boogle.hasWord("or"));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.