Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change ordering of elements in a vector?
    primarykey
    data
    text
    <p>I have a vector of type Fruit. I can insert different types of fruits on my drawing canvas. If I have two overlapping fruits, I want to be able to raise the fruit after selecting it so that it now appears as if it is on top of the other fruit. </p> <p>My basic idea for this is to have a raiseFruit() that is called after the jbutton is pressed. I create a temp variable set equal to the fruit that is selected, and then attempt to set it equal to the fruit at (a+1)...so the next position in the vector. And then I set this equal to temp variable. I think this is the right idea?</p> <pre><code>public void raiseFruit() { //raisefruit button if (_fruitIsSelected==true) { for (int i=0; i&lt;_storedFruit.size() - 1; i++) { FunFruit a = _storedFruit.get(i); FunFruit b = _storedFruit.get(i + 1); if (_storedFruit.get(i+1)!=null) { FunFruit temp = a; _storedFruit.get(i) = _storedFruit.get(i+1); a=b; b = temp; //_storedShapes.setElementAt(_storedShapes.get(i+1), i); //_storedShapes.setElementAt(_currentShape, i+1); repaint(); } } } } </code></pre> <p>I did try using setElementAt and it will work, but then if I press "raisefruit" again it will make the elements in front of it just entirely disappear. I just posted my code above with the part that keeps making elements in front of it disappear commented out. Instead of "a=b" I am trying to set whats in the vector at variable a equal to whats in the vector at variable b. Except, i keep getting an error that "left hand side must be a variable." Because I know that just setting a = to be just changes the pointers. I don't get how to actually change it inside the vector too, without the fruit elements in front being deleted. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload