Convert a Short Array to String in Java
Declaration :
1 |
public static String toString(short[] a) |
Explanation :
Purpose | The java.util.Arrays.toString(short[] a) method returns a string representation of the contents of the specified short array. The string representation consists of a list of the array’s elements enclosed in square brackets (“[]”). Adjacent elements are separated by the characters ” ” (a comma followed by a space). |
Parameters | a ===> This is the array whose string representation to return. |
Return Value | This method returns a string representation of a. |
Exception | NA |
Java Program : Example
Below example will explain java.util.Arrays.toString() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.c4learn; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // initializing short array short[] arrSh = new short[] { 5, 10, 15, 20 }; // Print the short array System.out.println("The array is:"); for (short num : arrSh) { System.out.println("Element value is " + num); } System.out.println("\nString Representation :"); System.out.println(Arrays.toString(arrSh)); } } |
Output of Program :
1 2 3 4 5 6 7 8 |
The array is: Element value is 5 Element value is 10 Element value is 15 Element value is 20 String Representation : [5, 10, 15, 20] |