2014-12-11 52 views
0

目標是循環遍歷0到n個數字集合,並找出所有已通過集合的數字。我知道如何查找共同的集合,如果我將該方法硬編碼爲接受特定數量的Comparable []。但讓我們說我想將三個Comparable []存儲到一個Object []中,並將Object []作爲參數傳遞給一個方法。我怎樣才能從對象[]中「展開」三個Comparable [],以便我可以對它們進行操作?Java:如何循環訪問Java中的n個數組?數組作爲一個對象數組傳遞

這裏是一個工作的硬編碼的方法:

public static Comparable[] compare2(Comparable[] test1, Comparable[] test2, 
     Comparable[] test3) { 
    Comparable[] inCommon = new Comparable[25]; 
    int counter = 0; 
    int comparisons = 0; 
    for (Comparable c : test1) { 
     for (Comparable d : test2) { 
      comparisons++; 
      if (c.compareTo(d) == 0) { 
       for (Comparable e : test3) { 
        comparisons++; 
        if (d.compareTo(e) == 0) { 
         inCommon[counter] = c; 
         counter++; 
         break; 
        } 
       } 
      } 
     } 
    } 
    System.out.println(comparisons); 
    return inCommon; 

它返回顯示在通用於所有三組數字的可比性[],我已經寫了打印方法之後。即使當我將Object []傳遞給我的比較方法而不是特定數量的Comparable []時,我也希望能夠做到這一點。在我的主文件中,我可以通過N組數字創建0,我希望我的比較方法能夠找到常用數字,而不管有多少組傳遞給它。我試圖寫一個遞歸函數。這並沒有奏效,因爲當我嘗試使用櫃檯來保留我的位置時,它很快就變得非常混亂。我還嘗試編寫第二種方法,該方法一次只比較兩個集合,但沒有奏效。我們的目標是讓我的方法是這樣工作的:

Comparable[] test1 = {1, 2, 3}; 
Comparable[] test2 = {2, 3, 4}; 
Comparable[] test3 = {2, 3, 5}; 
Object[] sets = {test1, test2, test3}; 

public static Comparable[] compare3(Object[] sets){ 
    // I need to unravel the Object[] and then process however many 
    // Comparable[] are inside the Object[] 
} 
+0

遞歸方法將解決您的問題。 – Hector 2014-12-11 05:20:39

+0

有關如何開始遞歸方法的任何提示?我試圖編寫一種方法,每次都會調用自己並從集合[counter + 1]開始,但我不知道這是否正確。 – HandleThatError 2014-12-11 05:22:39

+0

你的意思是你想發送可比數組的數組而不是固定數組的數量? – Panther 2014-12-11 05:36:53

回答

0

你可以嘗試如下所示的結構:

private static Comparable[] compareRecur(Object[] sets, Comparable[] inCommon, int counter){ 
    // base condition 
    if (counter == inCommon.length) 
     return comparisions; 

    // I need to unravel the Object[] and then process however many 
    // call the method recursively 
    // increment counter at every run 
} 

public static Comparable[] compare3(Object[] sets) { 
    return compareRecur(sets, new Comparable[25], 0); 
} 
0

你需要這樣的事情: -

//這程序不是假設,你每個1 D數組的大小是一樣的。

public Comparable[] recursiveCompare(Comparable[][] comparable , int index , Comparable[] commonElements){ 
     //this index show till from which index comparison needs to start 
     if(index < comparable.length - 1){ 
     Comparabale[] comparable1 = comparable[index]; 
     Comparabale[] comparable1 = comparable[index +1 ]; 
      for(Comparable compare1 : comparable1){ 
       for(Comparable compare2 : comparable2){ 
        if(compare1.compareTo(compare2)){ 
            inCommon[counter] = c; 
        counter++; 
        break;[counter] = c; 
        counter++; 
        break; 
} 


      } 

      } 


     }else{ 
      return commonElements[]; 

    } 



} 

public static Comparable[] compare2(comparable[][]){ 

//最大公共元素可以是m * n。或者如果它相應地變化較小 Comparable [] inCommon = new Comparable [可比[] .length *可比較[1] .lenght];

System.out.println(inCommon.lenght); 

}

你可以簡單地實現通過iterationg環和比較連續的陣列。使用舊的索引循環。我沒有編碼

+0

我沒有與我的Java。所以請你自己編譯。對不起,因爲我在記事本上寫了這個拼寫錯誤。 – Panther 2014-12-11 06:14:22

0

我試過這種方法。此方法需要Object[]作爲輸入,並以您想要的方式返回常用Comparable[]。爲了簡化計算,我使用了retainAll

public static void main(String args[]){  
    Comparable[] setA = {1, 2, 3}; 
    Comparable[] setB = {2, 3, 4}; 
    Comparable[] setC = {2, 3, 5};  
    Object[] allSets = {setA, setB, setC}; 
    Comparable commonElements[] = compareAll(allSets); 
} 

public static Comparable[] compareAll(Object[] allSets){ 
    Comparable[] firstSet = (Comparable[]) allSets[0]; 
    List<Comparable> commonElements = new ArrayList<Comparable>(Arrays.asList(firstSet)); 
    for(Object setAsObject : allSets){ 
     Comparable[] set = (Comparable[]) setAsObject; 
     List<Comparable> thisSet = new ArrayList<Comparable>(Arrays.asList(set)); 
     commonElements.retainAll(thisSet); 
    } 
    return commonElements.toArray(new Comparable[]{}); 
}