我几乎完成了代码,其中方法返回具有相同元素的对象的最长序列。如果我的2D ArrayList myBoard
看起来像这样:
| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11|
+---+---+---+---+---+---+---+---+---+---+---+---+
0 | 11| 11| 12| 12| 12| 12| 12| 12| 12| 12| 12| 12|
+---+---+---+---+---+---+---+---+---+---+---+---+
1 | o| o| o| ~| ~| ~| ~| ~| ~| ~| ~| ~|
+---+---+---+---+---+---+---+---+---+---+---+---+
2 | b| b| b| b| ~| ~| ~| ~| ~| ~| ~| ~|
+---+---+---+---+---+---+---+---+---+---+---+---+
3 | 11| 11| 11| 11| 11| 11| ~| ~| ~| ~| ~| ~|
+---+---+---+---+---+---+---+---+---+---+---+---+
4 | b| b| b| b| ~| ~| ~| ~| ~| ~| ~| ~|
+---+---+---+---+---+---+---+---+---+---+---+---+
5 | 11| 11| 12| 12| 12|500| 12|480| 12| 12| 12| 12|
+---+---+---+---+---+---+---+---+---+---+---+---+
然后方法longestHorizontalSequence()应该返回
[(0,2,12), (0,3,12), (0,4,12), (0,5,12), (0,6,12), (0,7,12),....(0,11,12)]
但我的方法返回:
[(0,0,11), (0,1,11), (0,2,12), (0,3,12), (0,4,12), (0,5,12), (0,6,12), (0,7,12),.....,(0,11,12)]
即。它返回整行的最长序列。我试过调试但无法猜测我的代码应该改变什么。请问smb请看一下?提前谢谢!
public List<RowColElem<T>> getHorizontalSequence(ArrayList<ArrayList<T>> myBoard){
ArrayList<RowColElem<T>> result = new ArrayList<RowColElem<T>>();
int count = 1;
int max = 1;
int row1 = 0;
int col1 = 0;
for(int i = minRow; i <= maxRow; i++){
List<RowColElem<T>> currentList = new ArrayList<RowColElem<T>>();
RowColElem<T> obj = new RowColElem<T>(i, minRow, myBoard.get(i-minRow).get(0));
T elem = obj.getElem();
currentList.add(obj);
for(int j = minCol + 1; j <= maxCol; j++){
row1 = i - minRow;
col1 = j - minCol;
if(elem.equals(myBoard.get(row1).get(col1))
&& (!(elem.equals(this.element)))
&& (!(myBoard.get(row1).get(col1).equals(this.element)))){
count++;
RowColElem<T> obj1 = new RowColElem<T>(i,j, myBoard.get(row1).get(col1));
currentList.add(obj1);
obj = new RowColElem<T>(i, j, myBoard.get(row1).get(col1));
if(count > max){
max = count;
/* while(max != 0){
currentList.remove(max--);
}*/
}
if(currentList.size() > result.size()){
result.clear();
result.addAll(currentList);
}
}
else{
if(obj.getElem().equals(this.element)){
currentList.remove(0);
}
elem = myBoard.get(row1).get(col1);
obj = new RowColElem<T>(i,j, elem);
currentList.add(obj);
count = 1;
}
}
}
return result;
}
班级**RowColElem**
public class RowColElem<T>{
private int row;
private int col;
private T e;
// Create a RowColElem with the parameter parts
public RowColElem(int r, int c, T e){
this.row = r;
this.col = c;
this.e = e;
}
// Return the row
public int getRow(){
return this.row;
}
// Return the column
public int getCol(){
return this.col;
}
// Return the element
public T getElem(){
return this.e;
}
// Return a pretty string version of the triple formated as
// (row,col,elem)
public String toString(){
return "(" + this.row + "," + this.col + "," + this.e.toString() + ")";
}
}
答案 0 :(得分:0)
而不是:
else{
if(obj.getElem().equals(this.element)){
currentList.remove(0);
}
elem = myBoard.get(row1).get(col1);
obj = new RowColElem<T>(i,j, elem);
currentList.add(obj);
count = 1;
}
尝试:
else{
currentList.clear();
elem = myBoard.get(row1).get(col1);
obj = new RowColElem<T>(i,j, elem);
currentList.add(obj);
count = 1;
}
如果我是正确的,你没有正确清除你的currentList
arraylist,所以它保持整行,而不是你想要的那一组。
或者,放弃整个RowColElem类并简单地跟踪每个位置的起始位置和最长水平序列将更简单。它可以很容易地在2个for循环中完成,每当左边的对象等于当前对象时,你递增最长的子序列变量。每次向前移动时,请检查该号码是否为&gt; max,因此存储序列的起始位置和当前位置(序列的结尾)。