2014-12-11 51 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[]{}); 
}