2015-09-28 108 views
0

我想将三个数组合并成一个数组。只保留共同的元素。这不是一个重复的问题。我知道还有其他的例子在线,但那是使用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()); 

} 

}(在简单的顺序)

回答

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); 
     } 
    } 
} 
0

答案:

  • 问题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<Comparable[]> ls不是不合理的)。所有你需要做的HashSet(假设你做HashSet<Comparable> hs = new HashSet<>();)是ls.add(hs.toArray())。坦率地说,我不知道你为什么要这样做,因为你的方法应该返回hs.toArray()而不是(实际上,你的代码编译,看到你没有根据你发布的代码返回?)。
+0

@Herman甘地谢谢。 –

相关问题