//package com.nowjava;
/**来自
时代Java公众号 - nowjava.com**/
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] argv) {
List list = java.util.Arrays.asList("asdf", "nowjava.com");
System.out.println(java.util.Arrays.toString(toArray(list)));
}
/**
* Converts a list to a new copy of array based on the start index and end index.
*
* @param list
* @param startIndex
* @param endIndex
* @return
*/
@SuppressWarnings({ "unchecked", "cast" })
public static final <T> T[] toArray(List<T> list, int startIndex,
int endIndex) {
if (isEmpty(list)) {
return (T[]) list.toArray();
}
List<T> subList = list.subList(startIndex, endIndex);/**时代Java公众号 - nowjava.com**/
return (T[]) subList.toArray((T[]) java.lang.reflect.Array
.newInstance(list.get(0).getClass(), subList.size()));
}
public static final <T> T[] toArray(List<T> list) {
return toArray(list, 0, list.size());
}
/**
* This is a convenience method that returns true if the specified collection is null or empty
*
* @param <T>
* Any type of object
* @param collection
* @return
*/
public static <T> boolean isEmpty(Collection<T> collection) {
return collection == null || collection.isEmpty();
}
/**
* This is a convenience method that returns true if the specified map is null or empty
*
* @param <T>
* any type of key
* @param <U>
* any type of value
* @param map
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。