Converts collection to a string array by calling toString on each member. - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Converts collection to a string array by calling toString on each member.

Demo Code


//package com.java2s;

import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "java2s.com");
        System.out.println(java.util.Arrays
                .toString(toStringArray(collection)));
    }//from   w ww.  j ava2s.  c om

    /**
     * Converts collection to a string array by calling toString on each member.
     *
     * @param collection collection to convert
     * @return string array
     */
    public static String[] toStringArray(Collection collection) {
        String[] stringArray = new String[collection.size()];

        int i = 0;
        for (Object string : collection) {
            stringArray[i++] = string.toString();
        }

        return stringArray;
    }
}

Related Tutorials