我试图将三个数组组合成一个数组。只保留公共元素。这不是一个重复的问题。我知道网上还有其他例子,但那是使用int[],我不知道如何使用Comparable。
我需要什么帮助:
-
如何将单个组合/更新数组添加到2d数组中
-
如何计算每次比较元素的迭代次数
-
如果我想我怎么能改变数组我现在有一个列表?-我在想也许这样会更容易添加
我是一个编程新手,我很感激你的帮助。我正试着通过读书和上网搜索来学习java。
这是我目前所掌握的。
public class Common{
Comparable [] col_1 = {1, 1, 2};
Comparable [] col_2 = {1, 1, 2,3};
Comparable [] col_3= {1, 1, 2,3,4,};
Comparable [][] collections = {col_1, col_2, col_3};
int comparisonCount = 0
public Comparable[] findCommon(Comparable [][] collections){
int i, j, k, x, y;
for(i = 0; i< col_1.length; i++){
for(j = 0; j < col_2.length; j++){
for(k = 0; k < col_3.length; k++){
comparisonCount++;
// This should be counting but is not...
if(col_1[i].compareTo(col_2[j]) == 0 && col_1[i].compareTo(col_3[k]) ==0){
//keep searching until last element & allow duplicates & add to collections or a temp[]
}
}
}
}
// Here I'm not sure how to add the elements to the collection
for (x = 0; x < collections.length; x++){
for(y = 0; y< collections[x].length; y++){
collections [x][y] = ?????? // not sure how to add results here
}
}
}
public void setComparisons(int count){
count = comparisonCount;
}
public int getComparisons(){
return comparisonCount;
}
public class Sorting {
public static void main(String[] args) {
Common m = new Common();
//I want to test it from here but I don't know how to initialize each array.
for(int x=0; x < m.collections.length; x++){
for(int y= 0; y< m.collections[x].length; y++){
System.out.println(m.collections[x][y]);
}
// what I should be getting is only (1, 1, 2) - the order is not important really. I just want to learn.
}
System.out.println(m.getComparisons());
}
}
要在可比较集合中只保留公共元素,可以使用TreeSet,它使用传递的比较器对元素进行比较。
另外,使用自定义比较器可以计算元素相互比较的次数:
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
public class Main {
public static Comparable[] findCommon(Comparable[][] collections, Comparator comparator) {
TreeSet<Comparable> set = new TreeSet<Comparable>(comparator);
Collections.addAll(set, collections[0]);
for (int i = 1; i < collections.length; i++)
set.retainAll(Arrays.asList(collections[i]));
return set.toArray(new Comparable[set.size()]);
}
public static void main(String[] args) {
Comparable[] col_1 = {1, 1, 2};
Comparable[] col_2 = {1, 1, 2, 3};
Comparable[] col_3 = {1, 1, 2, 3, 4};
Comparable[][] collections = {col_1, col_2, col_3};
final int comparisonCount = 0;
CountingComparator comparator = new CountingComparator();
System.out.println(Arrays.toString(findCommon(collections, comparator)));
System.out.println(comparator.getComparisonCount());
}
private static class CountingComparator implements Comparator<Comparable> {
private int comparisonCount;
public int getComparisonCount() {
return comparisonCount;
}
@Override
public int compare(Comparable o1, Comparable o2) {
comparisonCount++;
return o1.compareTo(o2);
}
}
}
答案(按简单程度排序):
- 问题2:为什么你需要保持比较计数?你的方法是,它总是数组长度的乘积(
- 问题1:也许可以考虑使用HashSet(见这里:http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html)。这比你的运行速度快得多,但可能没有相同的比较计数。
- 问题3:最后,为了能够添加最后的集合,您可能需要使用列表(
List<Comparable[]> ls
不是不合理的)。所有你要做的哈希集(假设你做HashSet<Comparable> hs = new HashSet<>();
)是ls.add(hs.toArray())
。坦率地说,我不知道为什么你会这样做,因为你的方法应该返回hs.toArray()
而不是(事实上,你的代码编译,看到你没有根据你发布的代码返回吗?)。
col_1.length * col_2.length * col_3.length
)。