有一个非常有用的Arrays.asList()
:
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
但是没有Arrays.array()
:
public static <T> T[] array(T... values) {
return values;
}
虽然绝对是微不足道的,但这将是构造数组的一种非常方便的方法:
String[] strings1 = array("1", "1", "2", "3", "5", "8");
// as opposed to the slightly more verbose
String[] strings2 = new String[] { "1", "1", "2", "3", "5", "8" };
// Of course, you can assign array literals like this
String[] strings3 = { "1", "1", "2", "3", "5", "8" };
// But you can't pass array literals to methods:
void x(String[] args);
// doesn't work
x({ "1", "1", "2", "3", "5", "8" });
// this would
x(array("1", "1", "2", "3", "5", "8"));
在 Java 语言的其他任何地方,除了java.util.Arrays
?