我搜索了很多网站,但我唯一找到的是 This link
我有一个 BSNode 类。我做了一个ArrayList<BSNode> data
现在我想交换两个变量,但我遇到了这个错误:
The type of the expression must be an array type but it resolved to ArrayList<BSNode>
有什么办法可以交换这两个吗?当我只想比较两个变量时,我只是在 BSNode 类中实现 comparable 并重写一个方法。但是我应该怎么做才能在数组列表中交换它们?
我有这个方法:
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]);
}
最佳答案
使用 Collections.swap(data, k,i);
而不是 Collections.swap(data, data[k], data[i]);
关于java - ArrayList<class> collection.swap 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20348237/