打印2D Arraylist

时间:2015-09-05 18:22:03

标签: java arraylist tostring

当我尝试打印2D ArrayList时,输出如下所示:

A B CD E FG H I

而不是:

A B C
D E F
G H I

在构造函数public DenseBoard(T[][] x, T fillElem)中,我将2D array的元素复制到2D ArrayList中。然后,在toString()方法中,我循环遍历2D ArrayList的元素并输出结果(但我无法获得所需的结果,如上所述)

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

4 个答案:

答案 0 :(得分:3)

在toString方法中没有println的位置,因为它的用途是构建一个String,而不是输出任何标准输出。
变化:

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

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

顺便说一句,如果要经常调用此方法,请考虑使用StringBuilder。

答案 1 :(得分:1)

问题出在你的toString方法中:你永远不会在那里添加换行符。而不是给System.out.println();打电话打印换行符。

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()返回与系统相关的行分隔符字符串。

请注意,您应该考虑使用StringBuilder而不是与String连接。

答案 2 :(得分:1)

你的toString操作是打印出换行符,然后返回字符串表示(没有)。将System.out.println();替换为result += "\n";

答案 3 :(得分:0)

首先我添加了一些日志....

 var userInput=req.body["g-recaptcha-response"]? "&response="+req.body["g-recaptcha-response"]:"";
        var remoteIp="&remoteip="+req.connection.remoteAddress;

        var finalRequest="https://www.google.com/recaptcha/api/siteverify?secret=..."+userInput;//+remoteIp;

        request({
            uri: finalRequest,
            method: "POST",
            timeout: 10000,
            followRedirect: true,
            maxRedirects: 10
        }, function(error, response, body) {
            console.log(body);

        });

在我运行了几次之后,我发现它不是一个2D数组作为参数传递给构造函数,所以我尝试使用这段代码修复...

公共类测试员{

public static void main(String [] args){

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

现在输出了......

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