gpt4 book ai didi

java - ArrayList collection.swap 方法

转载 作者:行者123 更新时间:2023-12-03 20:17:45 25 4
gpt4 key购买 nike

我搜索了很多网站,但我唯一找到的是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com