我正在尝试Map
通过向上或向下移动所选项目来修改 a 中的元素列表。我的想法是记住所选项目旁边的元素。因为元素在地图中,所以我发现很难做到这一点,尽管我使用 aLinkedHashMap
如果有人能给我一个关于如何做到这一点的想法,我将不胜感激。
注意:我已经概括了这个问题,下面的代码只是一个参考。
public void moveUp(ActionEvent evt) {
List<String> selectedItems = myBean().getSelectedItems();
if(selectedItems.isEmpty()) return;
Map<String, String> avlbleItems = new LinkedHashMap<String, String>(myBean().getListBoxItems());
List<String> avlbleItemsList = new ArrayList<String>(myBean().getListBoxItems().values());
int size = avlbleItems.size();
List<String> tail = new ArrayList();
List<String> head = new ArrayList();
int buf = 0,k = 0,j = 0,m = 0,n = 0;
try {
label:
for (Iterator it = selectedItems.iterator(); it.hasNext();buf++) {
String st = (String) it.next();
if(buf==0) {
for (int i = 0; i < size; i++) {
if(avlbleItemsList.get(i).equals(st)) {
k = i-1;
m = k+2;
n = m;
}
}
if(k < 0) return;
if(k == 0) continue label;
}
if(n < size && selectedItems.contains(avlbleItemsList.get(n))) {
n++;
}
if(k+2 < size && !selectedItems.contains(avlbleItemsList.get(k+2))) {
m++;
}
if(!it.hasNext()) {
for (int i = 0; i < size; i++) {
if(avlbleItemsList.get(i).equals(st)) {
j = i+1;
if(j == size) break label;
if(j < size) {
tail = avlbleItemsList.subList(j, size);
}
}
}
}
}
avlbleItems.clear();
for (Iterator ite = head.iterator(); ite.hasNext();) {
String st = (String) ite.next();
avlbleItems.put(st, st);
}
for (Iterator it = selectedItems.iterator(); it.hasNext();) {
String st = (String) it.next();
avlbleItems.put(st, st);
}
avlbleItems.put(avlbleItemsList.get(k), avlbleItemsList.get(k));
for (int i = n; i < j-1; i++) {
avlbleItems.put(avlbleItemsList.get(i), avlbleItemsList.get(i));
}
for (Iterator it = tail.iterator(); it.hasNext();) {
String st = (String) it.next();
avlbleItems.put(st, st);
}
} catch (IndexOutOfBoundsException e) {
handleException();
return;
} catch (NoSuchElementException e) {
handleException();
return;
} catch (IllegalArgumentException e) {
handleException();
return;
}
}