Java - Write code to Convert array To String using if and for loop

Requirements

Write code to convert array To String

Append each element in the array to the string

Hint

  • Use for loop
  • check the null value

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] array = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(arrayToString(array));
    }/* w  w  w. ja v  a 2  s.c  om*/

    public static String arrayToString(String[] array) {
        String toReturn = null;

        if (null != array) {
            toReturn = "";
            for (int i = 0; i < array.length; i++) {
                toReturn += array[i] + ",\n";
            }
        } else {
            System.out.println("arrayToString(null)!");
        }
        return toReturn;
    }
}

Related Example