2016-11-16 156 views
0

代码返回0,并且不止一次返回公共数字。我希望它返回一个数组,并且有一次共同的数字!那么,如何返回数组以及两个数组通用的数字。我想返回{2,7,4} - 就像这样。当我尝试返回一个数组时,我总是跳出界限异常。 谢谢, 巴里在两个整数数组中找到公共元素java

public class Test { 
    public int findCommonElement(int[] a, int[] b){ 
     int counter=0; 
     int temp= 0; 
     int tempCounter = 0; 
     for(int i=0; i<a.length; i++){ 
      temp=a[i]; 
      tempCounter=0; 
      for(int j=0; j<b.length; j++){ 
       if (temp==b[j]){ 
        tempCounter++; 
       } 

      } 

      if (tempCounter == 1) { 
       temp = a[i]; 

       counter++; 

       System.out.println(temp); 

      } 

     } 

     return 0; 
    } 

    public static void main(String []args){ 
     int myArray[] = {2,2,7,7,2,1,5,4,5,1,1}; 
     int myArray2[] = {2,3,4,7,10}; 


     Test hello = new Test(); 
     System.out.println(hello.findCommonElement(myArray, myArray2)); 

    } 
} 
+1

它因为你的方法被设置为返回这里0 – darkhouse

+1

多个问题返回0。首先,你的函数返回0.第二,如果你想返回多个数字,你必须改变findCommonElement以返回一个数组或列表。 – mdewit

+1

另外,findCommonElement中的计数器变量当前不执行任何操作。为什么if(tempCounter == 1)语句只检查tempCounter是否为1?不应该如果(tempCounter> 0)? – mdewit

回答

0

基本上共同元件从两个元件的数量将是动态的。因此,如果您尝试将常用元素放入数组中,那么它将不可能,因为您需要声明此数组的大小(在这种情况下将是动态的)。

考虑使用列表。我尽量保持逻辑尽可能简单以及全面的变量名称。

import java.util.ArrayList; 
    import java.util.Iterator; 
    import java.util.List; 

    public class Test { 

    public static void main(String[] args) { 

     int myArray[] = { 2, 2, 7, 7, 2, 1, 5, 4, 5, 1, 1 }; 
     int myArray2[] = { 2, 3, 4, 7, 10 }; 

     Test hello = new Test(); 
     System.out.println(hello.findCommonElement(myArray, myArray2)); 
    } 
    /** 
    * 
    * @param a 
    * @param b 
    * @return commonElements 
    */ 
    public List<Integer> findCommonElement(int[] a, int[] b) { 

     List<Integer> commonElements = new ArrayList<Integer>(); 

     for(int i = 0; i < a.length ;i++) { 
      for(int j = 0; j< b.length ; j++) { 
        if(a[i] == b[j]) { 
        //Check if the list already contains the common element 
         if(!commonElements.contains(a[i])) { 
          //add the common element into the list 
          commonElements.add(a[i]); 
         } 
        } 
      } 
     } 
     return commonElements; 
    } 
} 
+0

谢谢不错的简单解决方案! –

+0

谢谢巴里!继续探索! –

+1

这是一个nxn解决方案,它不是performatic – deFreitas

4

findCommonElement method

public int[] findCommonElement(int[] a, int[] b){ 
    List<Integer> array = new LinkedList<Integer>(); 
    Set<Integer> set = new HashSet<Integer>(); 
    for(int ele:a){ 
     set.add(ele); 
    } 

    for(int ele:b){ 
     if(set.contains(ele)){ 
      array.add(ele); 
     } 
    } 

    int[] arr = new int[array.size()]; 
    for(int i = 0; i < array.size();i++){ 
     arr[i] = array.get(i); 
    } 
    return arr; 
} 
+0

谢谢生病了,必须阅读在散列集 –

+0

我认为解决方案好多了,因为复杂性较低,是'n + n + n',比'nxn'好得多 – deFreitas

0

我看到你的代码以下问题的替代解决方案:

我希望它返回与普通的数字数组一旦!

所以你需要声明你的方法返回一个数组。添加方括号:

public int[] findCommonElement(int[] a, int[] b) { 

在您的方法中,您还必须跟踪到目前为止发现的所有常见元素。您可以使用新阵列或更方便地使用ArrayList或更方便使用HashSet(因为该设置会自动消除重复项,因此您只能得到每个常用号码一次)。我想你的意思是counter变量来跟踪新数组中元素的数量,只有数组还没有。

你检查:

if (tempCounter == 1) { 

这是不对的,如果发生数比b一次。相反,做

if (tempCounter > 0) { 

正如我所说的,你需要一种方法来过滤掉从a重复这样你就不会得到[2, 2, 7, 7, 2, 4]但只有[2, 7, 4]。正如我所提到的,您可以使用一组I,或者您可以使用ArrayList.contains()或引入另一个循环来检查数字是否已经在您的常用数组中。如果是的话,不要再添加它。

最后,打印的阵列的内容,使用Arrays.toString()

System.out.println(Arrays.toString(hello.findCommonElement(myArray, myArray2))); 
+0

感谢您的信息! –

0

这里是O(M + N)溶液:

static ArrayList<Integer> commonElements(int[] array1, int[] array2) { 
    int p1 = 0; 
    int p2 = 0; 
    ArrayList<Integer> common = new ArrayList<Integer>(); 

    while(true) { 
     if (array1[p1] == array2[p2]) { 
      common.add(array1[p1]); 
     } 
     if (p1 == array1.length - 1 || p2 == array2.length - 1) break; 
     if (array1[p1 + 1] < array2[p2 + 1]) { 
      p1++; 
     } else { 
      p2++; 
     } 
    } 
    return common; 
} 
+0

您是否在考虑将这两个数组进行预先排序。 – Akshay

相关问题