【发布时间】:2016-10-02 12:38:24
【问题描述】:
我已经很久没有写过java了,我知道我的问题很简单,但我一辈子都无法弄清楚错误。
我正在尝试使用以下代码查找数组中的最小数字。该算法是正确的,但是在最后一个打印语句中尝试使用它时出现错误
package runtime;
import java.util.ArrayList;
public class app {
/**
* @param args the command line arguments
*/
public int findSmallElement(ArrayList<Integer> num)
{
int smElement;
smElement= num.get(0);
for(int i=0; i<num.size() ; i++)
if(num.get(i) < smElement)
smElement=num.get(i);
return smElement;
}
public static void main(String[] args) {
ArrayList<Object> num = new ArrayList<Object>();
num.add(100);
num.add(80);
num.add(40);
num.add(20);
num.add(60);
System.out.println("The size of the list is " +num.size());
System.out.println(num.findSmallElement());
}
}
【问题讨论】: