Copy the given Collection into a String array. The Collection must contain String elements only. - Android java.util

Android examples for java.util:Collection Contains

Description

Copy the given Collection into a String array. The Collection must contain String elements only.

Demo Code

/*/* w w  w  . j  a  v  a 2 s . c  o  m*/
 * Copyright (C) 2012 Google Inc.
 *
 * 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</code> if the passed-in Collection was
   *         <code>null</code>)
   */
  public static String[] toStringArray(Collection collection) {
    if (collection == null) {
      return null;
    }
    return (String[]) 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</code> if the passed-in Enumeration was
   *         <code>null</code>)
   */
  public static String[] toStringArray(Enumeration enumeration) {
    if (enumeration == null) {
      return null;
    }
    List list = Collections.list(enumeration);
    return (String[]) list.toArray(new String[list.size()]);
  }

}

Related Tutorials