集册 Java实例教程 将集合转换为字符串数组

将集合转换为字符串数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

434
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将集合转换为字符串数组


//package com.nowjava;
/**
 * n o w    j a v a  . c o m 提 供 
**/

import java.util.Collection;


public class Main {

    public static void main(String[] argv) {

        Collection collection = java.util.Arrays.asList("asdf",

                "nowjava.com");

        System.out.println(java.util.Arrays

                .toString(toStringArray(collection)));

    }


    /**

     * Converts collection to array of strings

     * @param collection source collection

     */// 来自 n o w j a v a . c o m - 时代Java

    public static String[] toStringArray(Collection collection) {

        if (collection == null) {

            return null;

        }

        if (collection.isEmpty()) {

            return new String[] {};

        }

        int index = 0;

        String[] result = new String[collection.size()];

        for (Object item
展开阅读全文