Syntax error Dump the content of an array in Java

Dump the content of an array in Java



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));
Updated on: 2020-06-25T14:40:53+05:30

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements