我的疑问是如何在ArrayList中选择一个元素,这样我就可以在下一个元素中交换,我希望将元素向下移动并交换到前一个元素,然后移动项目。
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;
}
如果这个方法是正确的,输出的一个例子是
--- 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