0

My doubt is how to select an element in an ArrayList so that I can swap with the next element in the case I want to move the element down and swap to the previous element in the case, move the item up.

public class ShapesFX{

        private ArrayList <Shape> shapes;

        public ShapesFX() {
            this.shapes= new ArrayList <> ();
    }

  / **
     * Swap positions with the immediate previous element. The method can not
     * Be successful if the reference element is the last.
     *
     * @ Param element element reference to the exchange
     * @ Return true if the exchange was successful. false, d.c.
     * @ Throws Exception if the element does not exist
     * /

    @ Override
    public boolean moveUP (Shape element) throws Exception {
        if (element != null) {
            throw new Exception ("Element does not exist");
        } else if (element != Null) {
            for (FormaGeometrica sh : this.shapes) {
                if (sh == element) {
                    Collections.swap ((List<Shape>) element, 0, 0);
                }
            }
            return true;
        }
        return false;
    }

  / **
     * Swap positions with the next immediate element. The method can not
     * Be successful if the reference element is the first.
     *
     * @ Param element element reference to the exchange
     * @ Return true if the exchange was successful. false, d.c.
     * @ Throws Exception if the element does not exist
     * /

    @ Override 
    public boolean moverDown (Shape element) throws Exception {
        if (element != null) {
            throw new Exception ("Element does not exist");
        } else if (element != Null) {
            for (Shape sh : this.shapes) {
                if (sh == element) {
                    Collections.swap ((List<Shape>) element, 0, 0);
                }
            }
            return true;
        }
        return false;
    }

and if this methods is correctly, one example of the output is

--- List of Figures ---
F1
F2
F3
F4

--- Move F2 UP ---
F2
F1
F3
F4

F2 change position with F1

--- Move F3 DOWN ---
F2
F1
F4
F3

F3 change position with F4
DSanches
  • 311
  • 2
  • 3
  • 15
  • Possible duplicate: (http://stackoverflow.com/questions/4938626/moving-items-around-in-an-arraylist) – Andrew Jun 24 '14 at 15:32
  • Select the index of element which you want to move and swap with the previous index element or element in the next index – Vivin Jun 24 '14 at 15:35
  • 1
    Can you make sure that the code you posted compiles with a java compiler? Example: `Else` -> `else`, `! =` -> `!=`, `/ **` -> `/**` etc. – Erwin Bolwidt Jun 24 '14 at 15:36
  • I think you need to (re)read the Javadoc for Collections.swap. You are casting a Shape to a List, and you are specifying the same value for both of the index parameters. – Danny Jun 24 '14 at 15:37
  • Thanks @Erwin Bolwidt, Its my fault, but I re-edit the code now – DSanches Jun 24 '14 at 15:40
  • Andrew this topics(http://stackoverflow.com/questions/4938626/moving-items-around-in-an-arraylist) is good but I wanna move just 2 element and they say me to use Collections.Swap(item,0,1); – DSanches Jun 24 '14 at 15:42
  • Vwin, you are the one who is right, maybe I need to Select the index of element which you want to move and swap with the previous index element or element in the next index, I will try – DSanches Jun 24 '14 at 15:44

0 Answers0