Importance of deepToString() and asList() methods in Java?



In Java, both deepToString() and asList() methods are static methods of the Arrays class. An array is an object that holds a fixed number of values of a similar type in a contiguous memory location.

The deepToString() Method

In Java, the deepToString() method is used to convert a multi-dimensional array into a string. It checks if any element in the array is also an array; it will convert that inner array to a string as well.

Syntax

Following is the syntax of the Arrays.deepToString() method:

public static String deepToString(Object[] a)

Here,

  • a: An array whose string representation is to be returned. It can be a single or multi-dimensional array.

Example 1

In the following example, we use the deepToString() method to retrieve the string representation of an array. This method is used for multi-dimensional arrays, but still, we can use it with a single-dimensional primitive array by wrapping it inside an Object[]:

import java.util.Arrays;

public class DeepToStringTest {
   public static void main(String[] args) {
      int[] array = {10, 20, 30, 40};
      
      System.out.println("The single-dimensional array is: ");
      for (int num : array) {
         System.out.print(num + " ");
      }
      
      System.out.println("\nString representation using deepToString(): ");
	  // using the deepToString() method
      System.out.println(Arrays.deepToString(new Object[] { array }));
   }
}

The following example displays the string representation of the single-dimensional array:

The single-dimensional array is: 
10 20 30 40 
String representation using deepToString():
[[10, 20, 30, 40]]

Example 2

Here is another example showing the importance of the deepToString() method for converting a multi-dimensional array {{1, 2, 3}, {11, 12, 13}, {21, 22, 23}} into a string format:

import java.util.Arrays;

public class DeepToStringTest {
   public static void main(String[] args) {
      //multi-dimensional array
      int[][] array = new int[][] {
         {1, 2, 3},
         {11, 12, 13},
         {21, 22, 23}
      };
      
      System.out.println("The multi-dimensional array is: ");
      for (int i = 0; i < array.length; i++) {
         for (int j = 0; j > array[i].length; j++) {
            System.out.print(array[i][j] + " ");
         }
         System.out.println();
      }
      
      System.out.println("String representation using deepToString():");
      //using deepToString() method
      System.out.println(Arrays.deepToString(array));
   }
}

The above program produces the following output:

The multi-dimensional array is: 
1 2 3 
11 12 13 
21 22 23 
String representation using deepToString():
[[1, 2, 3], [11, 12, 13], [21, 22, 23]]

The asList() Method

In Java, the asList() method creates a list with a fixed size by converting the array. This means that we cannot add an element by the add() method to the created list. This method acts as a bridge between an array and a list because the list returned by the asList() method cannot extend the size but can use all other methods of a list.

Syntax

Following is the syntax of the Arrays.asList() method:

public static List asList(T... a)

Here,

  • T... a: An array of elements to be converted into a List.

Example 1

The following program shows the basic importance of the Arrays.asList() method to create a new fixed size list by converting the given array {"Welcome", "to", "TutorialsPoint"}:

import java.util.Arrays;
public class AsListTest {
   public static void main(String[] args) {
      String[] strArray = {"Welcome", "to", "TutorialsPoint"};
      System.out.println("The given array is: ");
      for(int i = 0; i<strArray.length; i++){
         System.out.print(strArray[i] + " ");
      }
      System.out.println("\nNew list after converting array:");
	  //using asList() method
      System.out.println(Arrays.asList(strArray));
   }
}

Following is the output of the above program:

The given array is: 
Welcome to TutorialsPoint 
New list after converting array:
[Welcome, to, TutorialsPoint]

Example 2

This is another example that shows the importance of the asList() method. As this method "converts" an array into a list, all the list methods become available and allow us to manipulate the data in ways that are not possible with arrays:

import java.util.Arrays;
import java.util.List;

public class AsListTest {
   public static void main(String[] args) {
      // Array of strings
      String[] fruits = {"Apple", "Banana", "Cherry", "Mango"};
      System.out.println("The array is: ");
      for(int i = 0; i<fruits.length; i++){
          System.out.print(fruits[i] + " ");
      }
      // Convert array to list using asList()
      List<String> fruitList = Arrays.asList(fruits);
      
      // Now we can use all the list methods
      System.out.println("\nIs Banana in the list? " + fruitList.contains("Banana"));
      System.out.println("Size of fruitList is: " + fruitList.size());
      System.err.println("Is fruitList is empty? " + fruitList.isEmpty());
   }
}

The above program displays the following output:

The array is: 
Apple Banana Cherry Mango 
Is Banana in the list? true
Size of fruitList is: 4
Is fruitList is empty? false
Updated on: 2025-05-29T17:38:52+05:30

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements