繁体   English   中英

返回序列列表

[英]Return the list of the sequence

我的方法longestHorizo​​ntalSequence(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 |  ~|  ~|  ~|  ~|  ~|  ~|
    +---+---+---+---+---+---+

它应该返回我[[(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 int减去这行的任何原因: for(int j = 1; j < myBoard.get(i).size() - 1; j++){
  • longestHorizontalSequence的签名中,您具有Arraylist<ArrayList<T>> myBoard 第一个Arraylist也应该是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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM