Dump the content of an array in Java

Java 8Object Oriented ProgrammingProgramming

An array can be easily printed by using the method java.util.Arrays.toString() in Java. This method returns a string representation of the array contents that is enclosed in square brackets. If the array is null, then this method returns null.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String args[]) {
      String str[] = {"John", "Harry", "Sally", "Emma", "Peter"};
      System.out.println("The array content is:");
      System.out.println(Arrays.toString(str));
   }
}

Output

The array content is:
[John, Harry, Sally, Emma, Peter]

Now let us understand the above program.

The string array str[] is defined. Then the array content is printed using the Arrays.toString() method. A code snippet which demonstrates this is as follows −

String str[] = {"John", "Harry", "Sally", "Emma", "Peter"};
System.out.println("The array content is:");
System.out.println(Arrays.toString(str));
raja
Updated on 25-Jun-2020 14:40:53

Advertisements