【问题标题】:How to combine common elements in to one array?如何将常见元素组合到一个数组中?
【发布时间】:2015-09-28 02:46:14
【问题描述】:

我正在尝试将三个数组合并为一个数组。只保留共同的元素。这不是一个重复的问题。我知道网上还有其他示例,但那是使用 int [] 而我不知道如何使用 Comparable。

我需要什么帮助:

  1. 如何将单个组合/更新数组添加到二维数组。

  2. 如何计算每次比较元素的迭代次数。

  3. 如果我想要如何将我现在拥有的数组更改为列表? - 我在想也许这会更容易添加。

我是编程新手,我将不胜感激。我正在尝试通过阅读书籍和在线搜索来学习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());

}

}

【问题讨论】:

    标签: java algorithm multidimensional-array


    【解决方案1】:

    要仅保留可比较集中的常见元素,您可以使用 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】:

      答案(按简单顺序):

      • 问题2:为什么需要保留比较计数?按照您的方式,它始终是数组长度的乘积 (col_1.length * col_2.length * col_3.length)。
      • 问题 1:或许可以考虑使用 HashSet(参见此处:http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html)。这比您拥有的运行速度快很多,但比较计数可能不同。
      • 问题 3:最后,为了能够添加最后一个集合,您可能需要使用列表(List&lt;Comparable[]&gt; ls 并非不合理)。您对 HashSet 所做的一切(假设您创建 HashSet&lt;Comparable&gt; hs = new HashSet&lt;&gt;();)就是 ls.add(hs.toArray())。坦率地说,我不知道你为什么要这样做,因为你的方法应该返回 hs.toArray() (事实上,你的代码是否编译,因为你没有根据你发布的代码返回?) .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-16
        • 2023-01-18
        • 2019-12-05
        • 1970-01-01
        • 2015-02-21
        • 1970-01-01
        相关资源
        最近更新 更多