Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I swap the values of specific indices of my List

I have a List called myqueue and I’m trying to swap the values of two specific indexes in my List.

Here is what I have so far:

private Comparator<T> cc;
private List<T> myQueue;
   
private void exch(int i, int j) {  // helper method to exchange items at indices i and j
    T temp = this.myQueue.get(i) ;
    T temp1 = this.myQueue.get(j);
    this.myQueue.get(i) = temp1;
    this.myQueue.get(j) = temp;

}

However, there is a compilation error when I do this, which says "Variable expected". I’m confused on what I should do to resolve this. The first thoughts that come to my mind was to do this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void exch(int i, int j) {  // helper method to exchange items at indices i and j
    T temp = this.myQueue.get(i) ;
    T temp1 = this.myQueue.get(j);
    temp = temp1;
    temp1 = temp;
    temp = this.myQueue.get(i) ;
    temp1 = this.myQueue.get(j)

since I tried to use variables, but that would also not work as well. Can someone please help me resolve this issue?

>Solution :

You are trying to use a get method to set a value.

If you read oracle documentation Oracle doc you can find that the get method only return values, you could use set method and the index that you want to change to modify the list

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading