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