Java tutorial
//package com.java2s; /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.List; public class Main { /** * Copy the given Collection into a String array. * The Collection must contain String elements only. * @param collection the Collection to copy * @return the String array ({@code null} if the passed-in * Collection was {@code null}) */ public static String[] toStringArray(Collection<String> collection) { if (collection == null) { return null; } return collection.toArray(new String[collection.size()]); } /** * Copy the given Enumeration into a String array. * The Enumeration must contain String elements only. * @param enumeration the Enumeration to copy * @return the String array ({@code null} if the passed-in * Enumeration was {@code null}) */ public static String[] toStringArray(Enumeration<String> enumeration) { if (enumeration == null) { return null; } List<String> list = Collections.list(enumeration); return list.toArray(new String[list.size()]); } }