我需要将 en int 元素添加到数组中。
我想过将数组转换为arrayList,添加int,然后再次将arrayList转换为数组。
正如所料,我完全失败了。
aGrades 是一个数组,lGrades 是一个 ArrayList
// add one grade from 1-5
public void enter (int grade){
ArrayList<Integer> lGrades = new ArrayList<Integer>(Arrays.asList(aGrades));
lGrades.add(grade);
aGrades = listArray.toArray(lGrades);
}
现在的错误是:
Histo.java:28: error: no suitable constructor found for ArrayList(List<int[]>)
ArrayList<Integer> lGrades = new ArrayList<Integer>(Arrays.asList(aGrades));
^
constructor ArrayList.ArrayList(Collection<? extends Integer>) is not applicable
(actual argument List<int[]> cannot be converted to Collection<? extends Integer> by method invocation conversion)
constructor ArrayList.ArrayList() is not applicable
(actual and formal argument lists differ in length)
constructor ArrayList.ArrayList(int) is not applicable
(actual argument List<int[]> cannot be converted to int by method invocation conversion)
Histo.java:30: error: incompatible types
aGrades = lGrades.toArray(new Integer[lGrades.size()]);
^
required: int[]
found: Integer[]
这可能是一团糟,但我已经搜索了很多关于这个的线程,现在我很困惑。
非常感谢!