Java Collection to Array toArray(Collection c)

Here you can find the source of toArray(Collection c)

Description

Returns a string array contains all the String object in the Collection c.

License

Apache License

Parameter

Parameter Description
c Collection of String object.

Exception

Parameter Description
ClassCastException if the object in the Collection isnot a String object.

Declaration

public static String[] toArray(Collection c) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**/* w w  w . j  a v  a2 s . co  m*/
     * Returns a string array contains all the String object in the
     * Collection c.
     *
     * @param c
     *            Collection of String object.
     * @throws ClassCastException
     *             if the object in the Collection is
     *             not a String object.
     *
     * @author chenke
     */
    public static String[] toArray(Collection c) {
        if (c == null || c.size() == 0) {
            return EMPTY_STRING_ARRAY;
        }
        String[] result = new String[c.size()];
        int i = 0;
        for (Iterator iter = c.iterator(); iter.hasNext();) {
            result[i++] = (String) iter.next();
        }
        return result;
    }
}

Related

  1. collectionToObjectArray(Collection c)
  2. collectionToObjectArray(final Collection _list)
  3. convertToArray(Collection collection)
  4. convertToArray(Collection input)
  5. convertToArray(Collection items)
  6. toArray(Collection coll)
  7. toArray(Collection bytes)
  8. toArray(Collection c, T[] arr)
  9. toArray(Collection collection)