I know I have done this in the past. I have an ArrayList that has been populated with integers. I need to iterate over it and find the maximum value. However, when I iterate over an array with something like this:
for (int i = 0; i < list.size(); i++)
{
if (list.get(i) > max)
{
max = list.get(i);
}
}
I get an error that says java.lang.Object cannot be converted to int or that > is a bad operand type. I have never encountered this before, and I have used arraylists multiple times for this same purpose. What am I doing wrong here?
max is declared as an int but is not initialized.
答案 0 :(得分:4)
你很可能就像这样声明了ArrayList:
ArrayList list = new ArrayList<>();
而不是:
ArrayList<Integer> list = new ArrayList<>();