我的方法longest horizontal sequence(arraylist>myboard)应该返回具有相同元素的对象的最长水平序列。如果myBoard
如下所示:
| 0| 1| 2| 3| 4| 5|
+---+---+---+---+---+---+
0 | ~| x| x| x| x| x|
+---+---+---+---+---+---+
1 | o| o| o| o| o| o|
+---+---+---+---+---+---+
2 | b| b| b| ~| ~| ~|
+---+---+---+---+---+---+
3 | ~| ~| ~| ~| ~| ~|
+---+---+---+---+---+---+
它应该返回me
[[(1,0,o), (1,1,o), (1,2,o), (1,3,o), (1,4,o), (1,5,o)]
并且该方法不计算this.element
,即~
但是我的方法给了我[]
并且当我调试时它给了我:[(1,0,o), (1,1,o), (1,2,o), (1,2,o), (1,3,o), (1,3,o), (1,4,o), (1,4,o)]
第二行错误在我的if loop
中,我不知道如何修复错误如果smb能帮到我我会很感激的谢谢!public List<RowColElem<T>> longestHorizontalSequence(Arraylist<ArrayList<T>> myBoard){
ArrayList<RowColElem<T>> result = new ArrayList<RowColElem<T>>();
int count = 1;
int max = 1;
// int elemCount = 1;
for(int i = 0; i < myBoard.size(); i++){
List<RowColElem<T>> currentList = new ArrayList<RowColElem<T>>();
RowColElem<T> obj = new RowColElem<T>(i, 0, myBoard.get(i).get(0));
T elem = obj.getElem();
// currentList.add(obj);
for(int j = 1; j < myBoard.get(i).size() - 1; j++){
currentList.add(obj);
if(elem.equals(myBoard.get(i).get(j))
&& (!(elem.equals(this.element)))
&& (!(myBoard.get(i).get(j).equals(this.element)))){
count++;
RowColElem<T> obj1 = new RowColElem<T>(i,j, myBoard.get(i).get(j));
currentList.add(obj1);
obj = new RowColElem<T>(i, j+1, myBoard.get(i).get(j));
elem = obj.getElem();
}
else{
elem = myBoard.get(i).get(j);
obj = new RowColElem<T>(i, j, myBoard.get(i).get(j));
while(count > 0){
currentList.remove(0);
count--;
}
if(count > max){
max = count;
}
else if(result.size() < currentList.size()){
result.clear();
result.addAll(currentList);
}
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(){
String result = "";
if(this.e instanceof String){
String element = (String)this.e;
result = "(" + this.row + "," + this.col + "," + element + ")";
}
else if(this.e instanceof Integer){
Integer element = (Integer)this.e;
result = "(" + this.row + "," + this.col + "," + element + ")";
}
else if(this.e instanceof Character){
Character element = (Character)this.e;
result = "(" + this.row + "," + this.col + "," + element + ")";
}
return result;
}
}
最佳答案
欢迎使用stackoverflow:-)
我不能完全理解你的代码,所以我开始修改它,使它更可读。以下是一些可能是问题的一部分:
在算法中使用了大量的列表操作和对象创建,这使得它很难阅读和理解使用简单的int
变量和自我解释的名称使其更具可读性。它还有助于将它分成多种方法。
我看不出你为什么在这一行中减除1
在for(int j = 1; j < myBoard.get(i).size() - 1; j++){
的签名中有longestHorizontalSequence
。第一个Arraylist<ArrayList<T>> myBoard
也应该是一个Arraylist
可能只是打字错误。
在我完成之后,我得到了这个代码,它应该可以解决你的问题。但是,我不能告诉你你的代码到底有什么问题。
private void run() {
System.out.println(longestHorizontalSequence(createBoard(), "~"));
}
private ArrayList<ArrayList<String>> createBoard() {
ArrayList<ArrayList<String>> board = new ArrayList<ArrayList<String>>();
board.add(createList("~", "x", "x", "x", "x", "x"));
board.add(createList("o", "o", "o", "o", "o", "o"));
board.add(createList("b", "b", "b", "~", "~", "~"));
board.add(createList("~", "~", "~", "~", "~", "~"));
return board;
}
private <T> ArrayList<T> createList(T... items) {
ArrayList<T> result = new ArrayList<T>();
for (T item : items) {
result.add(item);
}
return result;
}
public <T> List<RowColElem<T>> longestHorizontalSequence(ArrayList<ArrayList<T>> board, T ignoredElement) {
int maxI = 0;
int maxJStart = 0;
int maxJLength = 0;
for (int i = 0; i < board.size(); i++) {
ArrayList<T> line = board.get(i);
int start = findNextNotMatching(line, 0, ignoredElement, -1);
while (start != -1) {
int end = findNextNotMatching(line, start, line.get(start), line.size());
int length = end - start;
if (maxJLength < length) {
maxI = i;
maxJStart = start;
maxJLength = length;
}
start = findNextNotMatching(line, end, ignoredElement, -1);
}
}
return createResult(board, maxI, maxJStart, maxJLength);
}
private <T> ArrayList<RowColElem<T>> createResult(ArrayList<ArrayList<T>> board, int i, int start, int length) {
ArrayList<RowColElem<T>> result = new ArrayList<RowColElem<T>>();
for (int j = start; j < start + length; j++) {
result.add(new RowColElem<T>(i, j, board.get(i).get(j)));
}
return result;
}
private <T> int findNextNotMatching(ArrayList<T> line, int start, T element, int defaultValue) {
for (int j = start; j < line.size(); j++) {
if (!line.get(j).equals(element)) {
return j;
}
}
return defaultValue;
}
输出:
[(1,0,o), (1,1,o), (1,2,o), (1,3,o), (1,4,o), (1,5,o)]
最后,提出问题的一般建议:如果问题包含完整的代码,那么开始解决问题就容易得多特别是,代码应该包含:
代码,这是问题的主题。
调用主题代码的代码。
生成主题代码输入的代码。
测试主题代码的结果对于给定输入是否正确的代码。至少,应该提供一个将结果输出到控制台的方法。
另见http://sscce.org/
关于java - 返回序列列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32660018/