Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print two dimensional array of strings as String

Tags:

I know how to do the toString method for one dimensional arrays of strings, but how do I print a two dimensional array? With 1D I do it this way:

public String toString() {     StringBuffer result = new StringBuffer();     res = this.magnitude;      String separator = "";     if (res.length > 0) {         result.append(res[0]);         for (int i=1; i<res.length; i++) {             result.append(separator);             result.append(res[i]);         }     } return result.toString(); 

How can I print a 2D array?

like image 604
sasklacz Avatar asked Mar 07 '10 19:03

sasklacz


People also ask

How do I print a 2D array as a string?

We know that a two-dimensional array in Java is a single-dimensional array having another single-dimensional array as its elements. We can use the Arrays. toString() method to print string representation of each single-dimensional array in the given two-dimensional array.

How can a list of strings be stored within a two-dimensional array?

A list of strings can be stored within a 2D array. char str[3][15] = {""aaa"",""bbb"",""ccc""}; This represents an array with array name 'str' and it is of two dimensions. Here, 3 defines the number of strings that are stored in the array and the value 15 denotes the string length in total in the array.

How do you declare a 2D string in C++?

Here's one naive implementation, which writes out one string per line (assuming that the strings don't have any newlines in them): std::ofstream output("result. txt"); for (size_t i = 0; i < 137; ++i) for (size_t j = 0; j < 42; ++j) output << myArray[i][j] << std::endl; Hope this helps!


2 Answers

The Arrays class defines a couple of useful methods

  • Arrays.toString - which doesn't work for nested arrays
  • Arrays.deepToString - which does exactly what you want

 

String[][] aastr = {{"hello", "world"},{"Goodbye", "planet"}}; System.out.println(Arrays.deepToString(aastr)); 

Gives

  [[hello, world], [Goodbye, planet]] 
like image 104
Robert Christie Avatar answered Sep 27 '22 22:09

Robert Christie


You just iterate twice over the elements:

StringBuffer results = new StringBuffer(); String separator = "," float[][] values = new float[50][50];  // init values  for (int i = 0; i < values.length; ++i) {   result.append('[');   for (int j = 0; j < values[i].length; ++j)     if (j > 0)       result.append(values[i][j]);     else       result.append(values[i][j]).append(separator);   result.append(']'); } 

IMPORTANT: StringBuffer are also useful because you can chain operations, eg: buffer.append(..).append(..).append(..) since it returns a reference to self! Use synctactic sugar when available..

IMPORTANT2: since in this case you plan to append many things to the StringBuffer it's good to estimate a capacity to avoid allocating and relocating the array many times during appends, you can do it calculating the size of the multi dimensional array multiplied by the average character length of the element you plan to append.

like image 20
Jack Avatar answered Sep 27 '22 23:09

Jack