-2
I have an integer vector list in java (Arraylist Listvet).
public static ArrayList<Integer> ListVet;
And I need to make a comparison between two numbers from that same vector list. So, I did this:
for (int i=0; i<ListVet.size();i++)
{
if (ListVet[i] > ListVet[i+1])
{
}
}
However, this error occurred:
Multiple markers at this line
- The type of the Expression must be an array type but it resolved to Arraylist
Why did you make this mistake? Thank you.
Simple: an object of the type
ArrayList
is not a vector, so does not accept the index operator[idx]
. I believe you wish to use the methodget(idx)
in this specific case. Taking advantage, your code has a runtime bug: the last index that can be accessed islist.size() - 1
, and with the variablei
you arrive at exactly that amount, but you also usei+1
that exceeds the limits and therefore bursts exception– Jefferson Quesado