Reputation: 313
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;
}
}
Upvotes: 4
Views: 23239
Reputation: 106
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
Upvotes: 0
Reputation: 285405
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.
Upvotes: 4
Reputation: 7573
Your toString operation is printing out newlines, and then returning the string representation (which has none). Replace System.out.println();
with result += "\n";
Upvotes: 1
Reputation: 137184
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
.
Upvotes: 1