Example usage for java.util ArrayList toArray

List of usage examples for java.util ArrayList toArray

Introduction

In this page you can find the example usage for java.util ArrayList toArray.

Prototype

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) 

Source Link

Document

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Usage

From source file:Main.java

private static <T> T[] prependTo(T outerInstance, T[] params) {
    ArrayList<T> paramsList = new ArrayList<>(params.length + 1);

    paramsList.add(outerInstance);//ww  w  .java  2  s .  c  o  m
    for (T param : params) {
        paramsList.add(param);
    }
    return paramsList.toArray(params);
}

From source file:Main.java

/**
 * Concatenate two String arrays.//from  ww w .  j av a  2s . co  m
 * @param array1 cannot be null.
 * @param array2 cannot be null.
 * @return result.
 */
public static String[] concat(String[] array1, String[] array2) {
    ArrayList<String> result = new ArrayList<>();
    result.addAll(Arrays.asList(array1));
    result.addAll(Arrays.asList(array2));
    String[] array = new String[result.size()];
    result.toArray(array);
    return array;
}

From source file:Main.java

/**
 * Marshal the elements from the given enumeration into an array of the given type. Enumeration elements must be assignable to the type
 * of the given array. The array returned will be a different instance than the array given.
 * /*from  w  w  w  . j  a  v  a  2  s.c  o  m*/
 * @param enumeration
 *            the enumeration
 * @param array
 *            the array
 * @return the array representation of the enumeration
 * @param <A>
 *            the type of the array
 * @param <E>
 *            the type of th enumeration
 */
public static <A, E extends A> A[] toArray(Enumeration<E> enumeration, A[] array) {
    final ArrayList<A> elements = new ArrayList<A>();
    while (enumeration.hasMoreElements()) {
        elements.add(enumeration.nextElement());
    }
    return elements.toArray(array);
}

From source file:Main.java

public static String[] getValueByGap(int max, int min, int gap) {

    ArrayList<String> list = new ArrayList<String>();
    for (int i = min; i <= max; i += gap) {
        list.add(String.valueOf(i));
    }//www.  j  ava  2  s  .com
    String[] s = new String[list.size()];
    return list.toArray(s);
}

From source file:com.mycompany.asyncreq.Main.java

private static void writeToCSV(List<ArrayList<String>> csvInput, String csv) throws IOException {

    CSVWriter writer = null;//w  w w  .j a  v  a2 s  . co m

    try {
        writer = new CSVWriter(new FileWriter(csv));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (ArrayList<String> each : csvInput) {
        String[] eachTemp = each.toArray(new String[each.size()]);
        writer.writeNext(eachTemp);
    }

    writer.close();
}

From source file:com.topsoft.botspider.io.UnionData.java

public static void setParseClass(Configuration conf, Class... classez) {
    if (conf == null || classez == null)
        return;//from   w  ww  .j av  a 2s .c  o  m
    ArrayList<String> arrParse = new ArrayList<String>(classez.length);
    for (Class clzss : classez) {
        arrParse.add(clzss.getName());
    }
    conf.setStrings(UNION_CLASS, arrParse.toArray(new String[arrParse.size()]));
}

From source file:Main.java

/**
 * Trims an array of Strings to remove the whitespace. If the string is empty then its removed from the array.
 * //  w w  w  .j  a  v a2 s.  co  m
 * @param input The array of strings to be trimmed
 * @return The same array of strings but all elements have been trimmed of whitespace
 */
public static String[] trimStringArray(final String[] input) {
    final ArrayList<String> output = new ArrayList<String>();
    for (int i = 0; i < input.length; i++) {
        String s = input[i].trim();
        if (!s.equals(""))
            output.add(s);
    }
    return output.toArray(new String[0]);
}

From source file:com.topsoft.botspider.io.UnionData.java

public static void setParseClass(Job job, Class[] parseClass) {
    if (job == null || parseClass == null)
        return;/* w w  w.  j a  v  a2  s.  c  o m*/
    ArrayList<String> arrParse = new ArrayList<String>(parseClass.length);
    for (Class clzss : parseClass) {
        arrParse.add(clzss.getName());
    }
    job.getConfiguration().setStrings(UNION_CLASS, arrParse.toArray(new String[arrParse.size()]));
}

From source file:Main.java

public static String[] getJSONObjectKeys(JSONObject inputObject) {
    Iterator keys = inputObject.keys();
    ArrayList<String> objectKeys = new ArrayList<String>();

    while (keys != null && keys.hasNext()) {
        objectKeys.add((String) keys.next());
    }//from   www .j a va 2s .  c  o  m
    String[] returnArray = new String[objectKeys.size()];
    return objectKeys.toArray(returnArray);
}

From source file:Main.java

public static ActivityInfo[] getIntentHandlers(Context ctx, Intent intent) {
    List<ResolveInfo> list = ctx.getPackageManager().queryIntentActivities(intent, 0);
    ArrayList<ActivityInfo> activities = new ArrayList<ActivityInfo>();
    if (list != null) {
        for (ResolveInfo ri : list) {
            activities.add(ri.activityInfo);
        }// w  w  w  .  j a v a  2 s  .  c o m
    }
    return activities.toArray(new ActivityInfo[activities.size()]);
}