简体   繁体   中英

Printing 2D Arraylist

When I try to print 2D ArrayList , the output looks like this:

A B CD E FG H I

instead of:

A B C
D E F
G H I

In the constructor public DenseBoard(T[][] x, T fillElem) , I copy the elements of the 2D array into the 2D ArrayList . Then, in toString() method, I loop throught the elements of the 2D ArrayList and output the result (but I can't get the desired result as I've mentioned above)

Class Tester

public class Tester {

    public static void main(String[] args){

        String[][] myString = {{"A B C"}, {"D E F"}, {"G H I"}};
        DenseBoard<String> temp1 = new DenseBoard<String>(myString, "a");
        System.out.println(temp1);
    }
}

Class DenseBoard

import java.util.*;
public class DenseBoard <T> {

    private T element;
    private ArrayList<ArrayList<T>> myBoard;

    public DenseBoard(T[][] x, T fillElem){
        this.myBoard = new ArrayList<ArrayList<T>>();
        this.element = fillElem;

        for(int i = 0; i < x.length; i++){
            ArrayList<T> values = new ArrayList<T>();
            for(int j = 0; j < x[i].length; j++){
                values.add(x[i][j]);
            }
            myBoard.add(values);
        }
    }

    public String toString(){
        String result = "";
        for(int i = 0; i < myBoard.size(); i++){
            for(int j = 0; j < myBoard.get(i).size(); j++){
                result += myBoard.get(i).get(j);
            }
            System.out.println();
        }
        return result;
    }
}

There is no place for a println within your toString method, since its use is to build a String, not to output anything to the standard output.
Change:

 public String toString(){
      String result = "";
      for(int i = 0; i < myBoard.size(); i++){
          for(int j = 0; j < myBoard.get(i).size(); j++){
              result += myBoard.get(i).get(j);
          }
          System.out.println();
      }
      return result;
  }

to

 public String toString(){
      String result = "";
      for(int i = 0; i < myBoard.size(); i++){
          for(int j = 0; j < myBoard.get(i).size(); j++){
              result += myBoard.get(i).get(j);
          }
          // System.out.println();
          result += "\n";
      }
      return result;
  }

As an aside, if this method is to be called frequently, consider using a StringBuilder.

The problem is in your toString method: you never add a line break there. Instead of you call System.out.println(); which prints a line break.

public String toString(){
    String result = "";
    for(int i = 0; i < myBoard.size(); i++){
        for(int j = 0; j < myBoard.get(i).size(); j++){
            result += myBoard.get(i).get(j);
        }
        result += System.lineSeparator();
    }
    return result;
}

System.lineSeparator() returns the system-dependent line separator string.

Note that you should consider using a StringBuilder instead of concatenating to a String .

Your toString operation is printing out newlines, and then returning the string representation (which has none). Replace System.out.println(); with result += "\\n";

First I added some logging....

import java.util.*;
public class DenseBoard <T> {
private T element;
    private ArrayList<ArrayList<T>> myBoard;

 public DenseBoard(T[][] x, T fillElem){
      this.myBoard = new ArrayList<ArrayList<T>>();
      this.element = fillElem;

      for(int i = 0; i < x.length; i++){
         ArrayList<T> values = new ArrayList<T>();
          for(int j = 0; j < x[i].length; j++){
              T nextElement = x[i][j];
              System.err.println(String.format("adding x[%1$d][%2$d] (%3$s)", i, j, nextElement));
              values.add(x[i][j]);
          }
          System.err.println(String.format("added %1$d elements", values.size()));
          myBoard.add(values);
      }
  }
 public String toString(){
      String result = "";
      for(int i = 0; i < myBoard.size(); i++){
          for(int j = 0; j < myBoard.get(i).size(); j++){
              result += myBoard.get(i).get(j);
          }
          // replaced the call to System.out.println() with "\n"..
          result+= "\n";

      }
      return result;
  }
}

After I ran a couple of times, it occurred to me that it was not a 2D array that was being passed as an argument to the constructor, so I tried this code to fix...

public class Tester {

public static void main(String[] args){

        String[][] myString = {{"A", "B", "C"}, {"D", "E", "F"}, {"G","H", "I"}};
        DenseBoard<String> temp1 = new DenseBoard<String>(myString, "a");
        System.out.println(temp1);
    }
}

Now the output I got...

$ java Tester
adding x[0][0] (A)
adding x[0][1] (B)
adding x[0][2] (C)
added 3 elements
adding x[1][0] (D)
adding x[1][1] (E)
adding x[1][2] (F)
added 3 elements
adding x[2][0] (G)
adding x[2][1] (H)
adding x[2][2] (I)
added 3 elements
ABC
DEF
GHI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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