Return String Representation of Deep Contents of Array in Java



In this article, we will learn how to generate a string representation of the contents of arrays in Java. Using Arrays.deepToString() transforms arrays into readable strings, making it easy to visualize their structure. This method is particularly useful for one-dimensional and multidimensional arrays.

Problem Statement

Given arrays with varying structures, write a Java program that prints the elements of these arrays and their string representations using Arrays.deepToString().
Input
A one-dimensional array:
Object[] ob = {"One", "Two", "Three", "Four"}
A two-dimensional array:
int[][] arr = {{10, 20, 30}, {40, 50, 75}, {100, 150, 200}}
Output

For the one-dimensional array:
Array elements...
Value = One
Value = Two
Value = Three
Value = Four
The string representation of the array is:
[One, Two, Three, Four]
For the two-dimensional array:
[[10, 20, 30], [40, 50, 75], [100, 150, 200]]

Steps to return a string representation of arrays

The following are the steps to return a string representation of the contents of arrays:

  • Import the Arrays class from the java.util package.
  • Create the arrays (one-dimensional or two-dimensional).
  • Loop through the array elements and print them.
  • Use Arrays.deepToString() to convert the arrays into a string format.
  • Print the string representation of the arrays.

Java Program to Return String Representation of Arrays

The following is an example of one dimensional Array:

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) {
      Object[] ob = {"One","Two", "Three", "Four"};
      System.out.println("Array elements...");
      for (Object value : ob) {
         System.out.println("Value = " + value);
      }
      System.out.println("The string representation of array is:");
      System.out.println(Arrays.deepToString(ob));
   }
}

Output

Array elements...
Value = One
Value = Two
Value = Three
Value = Four
The string representation of array is:
[One, Two, Three, Four]

Two-Dimensional Array Example

The following is an example of a Two-Dimensional array:

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) {
      int[][] arr = new int[3][3];
      arr[0][0] = 10;
      arr[0][1] = 20;
      arr[0][2] = 30;
      arr[1][0] = 40;
      arr[1][1] = 50;
      arr[1][2] = 75;
      arr[2][0] = 100;
      arr[2][1] = 150;
      arr[2][2] = 200;
      System.out.println(Arrays.deepToString(arr));
   }
}

Output

[[10, 20, 30], [40, 50, 75], [100, 150, 200]]

Code Explanation

In the first example, we create a one-dimensional array containing string elements. We print each element and then use Arrays.deepToString() to convert the array into a string. This string shows the array's structure in a readable format. In the second example, we construct a two-dimensional array and apply the same method to generate a string representation. The method makes it easier to visualize the nested structure of multidimensional arrays.

Updated on: 2024-11-11T19:15:27+05:30

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements