我搜索了许多网站,但我发现的唯一一件事就是This link。
我有一个BSNode课程。我做了一个ArrayList<BSNode> data,现在我想交换两个变量,但是我面临这样的错误:
The type of the expression must be an array type but it resolved to ArrayList<BSNode>有什么办法可以把这两样换掉吗?当我只想比较两个变量时,我只需在BSNode类中实现可比较的方法,然后重写一个方法。但是,我应该如何在数组列表中交换它们呢?
我有这样的方法:
public static void permutation (ArrayList<BSNode> data, int k, int n) 我想这么做:
for (int i = k; i < n; i++) {
Collections.swap(data, data[k], data[i]);
} 发布于 2013-12-03 10:22:48
使用Collections.swap(data, k,i);而不是Collections.swap(data, data[k], data[i]);
发布于 2013-12-03 10:21:38
您可以使用
Collections.swap(List<?> list, int i, int j);发布于 2013-12-03 10:27:50
如javadoc中所示,Collections.swap采用一个List参数和2个in,表示您希望交换的元素。所以你要找的解决方案是这样的
Collections.swap(data, k,i);Collections.swap
public static void swap(List<?> list, int i, int j)
list - The list in which to swap elements.
i - the index of one element to be swapped.
j - the index of the other element to be swapped.https://stackoverflow.com/questions/20348237
复制相似问题