Java Program to convert array to String for one dimensional and multi-dimensional arrays

Java 8Object Oriented ProgrammingProgramming

For converting array to 1D and 2D arrays, let us first create a one-dimensional and two-dimensional array −

One-Dimensional

String str[] = {"p", "q", "r", "s", "t", "u", "v", "w","x", "y", "z"};

Two-Dimensional

doubled [][]= {
   {1.2, 1.3, 2.1, 4.1},
   {1.5, 2.3},
   {2.5, 4.4},
   {3.8},
   {4.9},
   {3.2, 2.1, 3.2, 7.2}
};

Converting array to string for one-dimensional array −

Arrays.toString(str);

Converting array to string for two-dimensional array −

Arrays.deepToString(d);

Example

import java.util.Arrays;
public class Demo {
   public static void main(String args[]) {
      String str[] = {"p", "q", "r", "s", "t", "u", "v", "w","x", "y", "z"};
      doubled [][]= {
         {1.2, 1.3, 2.1, 4.1},
         {1.5, 2.3},
         {2.5, 4.4},
         {3.8},
         {4.9},
         {3.2, 2.1, 3.2, 7.2}
      };
      System.out.println("One dimensional = "+Arrays.toString(str));
      System.out.println("Two dimensional = "+Arrays.deepToString(d));
   }
}

Output

One dimensional = [p, q, r, s, t, u, v, w, x, y, z]
Two dimensional = [[1.2, 1.3, 2.1, 4.1], [1.5, 2.3], [2.5, 4.4], [3.8], [4.9], [3.2, 2.1, 3.2, 7.2]]
raja
Published on 23-Apr-2019 16:14:26
Advertisements