Ala*_*an2 24 java generics list arraylist
我有以下代码:
List<int> intList = new ArrayList<int>();
for (int index = 0; index < ints.length; index++)
{
intList.add(ints[index]);
}
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误......
Syntax error on token "int", Dimensions expected after this token
Run Code Online (Sandbox Code Playgroud)
错误发生在以List.开头的行上.有人能解释我收到错误的原因吗?
nob*_*beh 45
在Java泛型是不适用于基本类型为int.您应该使用包装类型,例如Integer:
List<Integer> ints = ...
Run Code Online (Sandbox Code Playgroud)
并且,要访问a List,您需要使用ints.get(index).
wat*_*ios 10
您只能在该<>部分中使用Object类型,而您尝试使用基本类型.试试这个...
List<Integer> intList = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
然后,您需要使用intList.get(index)和intList.set(index,value)(以及intList.add(value)您尝试执行的操作)访问值
| 归档时间: |
|
| 查看次数: |
86757 次 |
| 最近记录: |