Home  >  Article  >  Java  >  What is the importance of deepToString() and asList() methods in Java?

What is the importance of deepToString() and asList() methods in Java?

王林
王林forward
2023-08-18 15:13:19623browse

An array is an object that holds a fixed number of values ​​of a single type in contiguous memory locations. The deepToString() and asList() methods are both static methods of the Arrays class. deepToString() method converts a multidimensional array to a string and checks if the array has elements as an array and then converts the array to string format. asList() Creates a list with a fixed size, which means we cannot add to the list returned by Arrays.asList() via the add() method element. The asList() method acts as a bridge between arrays and lists because the list returned by the asList() method cannot be extended in size, but all other methods of lists can be used.

The syntax of Arrays.deepToString()

public static String deepToString(Object[] a)

Example

’s Chinese translation is:

Example

import java.util.Arrays;
public class DeepToStringTest {
   public static void main(String [] args){
      int[][] array = new int[][] {{1, 2, 3}, {11, 12, 13}, {21, 22,23}};
      System.out.println(<strong>Arrays.deepToString</strong>(array));
   }
}

Output

[[1, 2, 3], [11, 12, 13], [21, 22, 23]]

The syntax of Arrays.asList()

public static List asList(T... a)

Example

’s Chinese translation is:

Example

import java.util.Arrays;
public class AsListTest {
   public static void main(String[] args) {
      String[] strArray = {"Welcome", "to", "TutorialsPoint"};
      System.out.println(Arrays.asList(strArray));
   }
}

Output

[Welcome, to, TutorialsPoint]

The above is the detailed content of What is the importance of deepToString() and asList() methods in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete