2015-09-28 36 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甘地謝謝。 –

相關問題