有没有办法切换链接列表中的两个元素而不删除并重新插入它们?我目前正在使用的代码是:切换链接列表中的两个元素
void exchange(int i, int j) {
int[] temp = matrix.get(i);
matrix.remove(i);
matrix.add(i, matrix.get(j - 1));
matrix.remove(j);
matrix.add(j, temp);
}
其中matrix
是我的链接列表。
在Java中,LinkedList的是,如果你意图通过索引访问元素一个不错的选择。考虑ArrayList或Vector。 – DwB 2011-01-13 16:49:26