我有一个float [],我想得到一个具有相同元素的列表.我可以做一个又一个添加它们的丑陋的事情,但我想使用Arrays.asList方法.但是有一个问题.这有效:
List<Integer> list = Arrays.asList(1,2,3,4,5);
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
int[] ints = new int[] {1,2,3,4,5};
List<Integer> list = Arrays.asList(ints);
Run Code Online (Sandbox Code Playgroud)
asList方法接受一个varargs参数,对我来说,扩展是一个数组的"简写".
问题:
为什么第二段代码返回List<int[]>
但不返回List<int>
.
有没有办法纠正它?
为什么自动装箱不在这里工作; 即int[]
到Integer[]
?
我的for循环太长了.作为演示,方法B有三个for循环.有没有办法可以将它减少为一个简短的for循环(一个循环)?谢谢
public class MyMates
{
private static TreeSet<String> myTable = new TreeSet<String>();
private static String[] names1 = null;
private static String[] names2 = null;
private static String[] names3 = null;
public MyMates()
{
super();
myTable = new TreeSet<String>();
}
public static String methodA(String aTemp)
{
String[] names1 = new String[] {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
String[] names2 = new String[] { "Alan", "Amy", "Jeremy", "Helen", "Alexi"};
String[] names3 = new String[] { "Adel", "Aaron", "Amy", "James", "Alice" };
return aTemp; …
Run Code Online (Sandbox Code Playgroud)