How to compare numbers in vector list?

Asked

Viewed 59 times

-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.

  • 1

    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 method get(idx) in this specific case. Taking advantage, your code has a runtime bug: the last index that can be accessed is list.size() - 1, and with the variable i you arrive at exactly that amount, but you also use i+1 that exceeds the limits and therefore bursts exception

1 answer

3

Arraylist is an object, and as in Java there is no way to overwrite operators like [] to access the position of the list, you have to use methods, ie, ListVet.get(i) > ListVet.get(i+1)

If you create an array itself, it has a static size, such as Integer[] ListVet, then you will access with ListVet[i].

  • What if I wanted to make the value of the previous vector list receive the value of the later vector list? Type: Listvet[i] = Listvet[i+1]. The vector would look like this, but in the vector list, it doesn’t work like this.

  • You would have to use another method, Listvet.set(i, Listvet.get(i+1))

  • The first is the value of the list, and the second is the value that will replace the "i", correct?

  • 1

    The first argument corresponds to the position of the list, and the second to the value you will insert.

  • Thank you. :) :)

  • Just one more question: How do I convert an Integer Arraylist to String, and vice versa? Thank you.

  • Open a new question and explain your question better.

  • @Gabrielaugusto If the answer solved your problem, you can accept it, see how to do reading the FAQ. In fact, I suggest you consider doing the same (accepting an answer if it has solved your problem) in all your previous questions, as this is useful for future visitors who have the same question, since it indicates that it has actually solved the problem. Of course you are not obliged to accept any reply, but if it was useful and solved your problem, it is important to do so for the reasons mentioned in the FAQ link I quoted

  • @Gabrielaugusto And as already suggested, if you have other questions, do not hesitate to open a new question. The site works with well-focused and specific questions, and if you have questions about other things (even if they are related), the ideal is to open new questions, one for each specific question - not forgetting to search before if the question already exists on the site, of course - see more details on the page [Ask] and in the Manual on how not to ask questions

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.