【问题标题】:Return the list of the sequence返回序列的列表
【发布时间】:2015-09-18 19:53:08
【问题描述】:

我的方法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;
    }

}

【问题讨论】:

    标签: java algorithm arraylist


    【解决方案1】:

    欢迎来到 stackoverflow :-)

    我无法完全理解您的代码,因此我开始对其进行更改以使其更具可读性。以下几点可能是问题的一部分:

    • 您在算法中使用了大量的列表操作和对象创建,这使得它很难阅读和理解。使用简单的int 变量和自我解释的名称使其更具可读性。它还有助于将其拆分为多种方法。
    • 我看不出你有什么理由减去1 int 这一行:for(int j = 1; j &lt; myBoard.get(i).size() - 1; j++){
    • longestHorizontalSequence 的签名中,您有Arraylist&lt;ArrayList&lt;T&gt;&gt; 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多