Mark
Mark

Reputation: 1

Return the list of the sequence

my method longestHorizontalSequence(Arraylist> myBoard) should return me the longest horizontal sequence of objects with the same element. If myBoard looks like the following:

    |  0|  1|  2|  3|  4|  5|
    +---+---+---+---+---+---+
  0 |  ~|  x|  x|  x|  x|  x|
    +---+---+---+---+---+---+
  1 |  o|  o|  o|  o|  o|  o|
    +---+---+---+---+---+---+
  2 |  b|  b|  b|  ~|  ~|  ~|
    +---+---+---+---+---+---+
  3 |  ~|  ~|  ~|  ~|  ~|  ~|
    +---+---+---+---+---+---+

it should returns me [[(1,0,o), (1,1,o), (1,2,o), (1,3,o), (1,4,o), (1,5,o)] and the method does not count this.element which is ~. But instead my method gives me [] and when I debug it gives me: [(1,0,o), (1,1,o), (1,2,o), (1,2,o), (1,3,o), (1,3,o), (1,4,o), (1,4,o)] for the second line. The mistake is in my if loop and i do not know how to fix the bug. I'd appreciate if smb could help me out here. Thanks!

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;
 }

Class 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;
    }

}

Upvotes: 0

Views: 87

Answers (1)

Stefan Dollase
Stefan Dollase

Reputation: 4620

Welcome to stackoverflow :-)

I was not able to understand your code completely, so I started to change it to make it more readable. Here are some points that might be part of the problem:

  • You use a lot of list operations and object creations in your algorithm, which makes it pretty hard to read and understand. Use simple int variables and self explaining names to make it more readable. It also helps to split it in multiple methods.
  • I do not see any reason why you substract 1 int this line: for(int j = 1; j < myBoard.get(i).size() - 1; j++){
  • In the signature of longestHorizontalSequence you have Arraylist<ArrayList<T>> myBoard. The first Arraylist should also be an ArrayList ... probably just a typo.

After I was finished, I got this code, which should solve your problem. However, I cannot tell you what exactly is the problem with your code.

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;
}

Output:

[(1,0,o), (1,1,o), (1,2,o), (1,3,o), (1,4,o), (1,5,o)]

Finally, a general advice for asking questions: If the question contains the complete code, it makes it much easier to start solving the problem. Especially, the code should contain:

  • The code, which is the subject of the question.
  • The code, that calls the subject code.
  • The code, that generates the input for the subject code.
  • The code, that tests if the result of the subject code is correct for the given input. At least, a method to output the result to the console should be provided.

See also http://sscce.org/

Upvotes: 1

Related Questions