【发布时间】:2015-03-15 17:24:09
【问题描述】:
我正在尝试创建一个具有可变长度序列的数组,因此我决定使用 ArrayList。
由于我想求数字序列的总和,我尝试使用 ArrayList.get(int index) 方法来获取 ArrayList 中某个元素的值,但 NetBeans 向我抱怨 incompatible types: Object cannot be converted to int对于以下代码:
int seqLen = sequence.size(); // number of elements in ArrayList sequence
int seqSum = 0; // sum of all elements in the sequence
for(int i = 0; i <= seqLen; i++) {
int seqPart = sequence.get(i);
seqSum+= seqPart;
}
具体来说,int seqPart = sequence.get(i);这一行
我不确定为什么它认为i 是object 而不是int,而这段代码对于程序的运行至关重要。
基本上,我在这里做错了什么,我已经检查了 ArrayList 的 JavaDoc 几次,但我仍然没有弄清楚哪里出了问题。
【问题讨论】:
-
你在这个 ArrayList 中保留了哪些值?