Returns a string representation of the given array. - Java java.lang

Java examples for java.lang:String Array

Description

Returns a string representation of the given array.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] arr = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(toString(arr));
    }/*from ww  w  .j a  v  a 2s .  c om*/

    /**
     * Returns a string representation of the given array.
     * @param arr
     * @return
     */
    public static String toString(int[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; ++i) {
            if (i > 0) {
                // every element after first one has comma before it
                sb.append(", ");
            }
            sb.append(arr[i]);
        }
        sb.append("]");
        return sb.toString();
    }
}

Related Tutorials