2013-10-16 43 views
-4

我有以下任務: 在-20000000和20000000之間有2個整數數組。包含在第一個數組中的一些數字也包含在第二陣列。我必須找到第一個數組中包含的所有數字,但不包含在第二個數組中。我必須使用Java作爲一種語言使用Java查找兩個數組之間的非重複項目

這裏是陣列

[1,652,5,15,385,如圖4所示,55,666,13]

[2,4658,9, 55,-588,10,1083,17]

任何想法如何找到它?

編輯:

下面是最終代碼:

import java.util.ArrayList; 
import java.util.List; 
public class Values { 
public static void main (String[] argv) { 

int[] Array1 = new int[] {1,652,5,15,385,4,55,666,13}; 
int[] Array2 = new int[] {2, 4658, 9, 55, -588, 10, 1083, 17}; 
int calculateResult = 0; 
boolean contains = false; 
int mod = 123456789; 
int modSum = 0; 

List<Integer> results = new ArrayList<Integer>(); 
    for(int i=0; i<Array1.length; i++) { 
     for(int j=0; j<Array2.length; j++) { 
      if(Array1[i]==Array2[j]) { 
       contains = true; 
       break; 
      } 
     } 
     if(!contains) { 
      results.add(Array1[i]); 
     } 
     else { 
      contains = false; 
     } 
    } 
    // calculate the result 
    for (int i : results) { 
     calculateResult += i; 
    } 
    // Print Results 
    System.out.println(results); 
    System.out.println(calculateResult); 
}} 

現在我試圖加載從.csv文件陣列。有任何想法嗎 ?

+0

到目前爲止你有什麼? 你嘗試過什麼嗎? 請給我們展示一些代碼 –

回答

1

這是一個可能的解決方案:

int[] Array1 = new int[] {1,652,5,15,385,4,55,666,13}; 
    int[] Array2 = new int[] {2,4658,9,55,-588,10,1083,17}; 
    boolean contains = false; 
    List<Integer> results = new ArrayList<Integer>(); 


    for(int i=0; i<Array1.length; i++) { 
     for(int j=0; j<Array2.length; j++) { 
      if(Array1[i]==Array2[j]) { 
       contains = true; 
       break; 
      } 
     } 
     if(!contains) { 
      results.add(Array1[i]); 
     } 
     else{ 
      contains = false; 
     } 
    } 

    System.out.println(results); 

輸出:

[1, 652, 5, 15, 385, 4, 666, 13] 

我希望這是你在尋找的。

+0

您應該在'contains = true;'後打破循環,因此可以節省將第二個循環的其他元素與父循環元素進行比較的時間。 – commit

+0

是的,我編輯它。 – kai

+0

是的,現在我試圖得到結果的總和 – wkg86

0

我不明白爲什麼這個問題有反對票,它實際上是非常有趣的。

看看下面這可能爲你工作,你只需要採取列表,而不是陣列

 List<Integer> l1 = new ArrayList<Integer>(); 
     List<Integer> l2 = new ArrayList<Integer>(); 

     l1.add(1); 
     l1.add(3); 
     l1.add(5); 
     l1.add(7); 
     l1.add(8); 

     l2.add(2); 
     l2.add(3); 
     l2.add(4); 
     l2.add(7); 

     l2.retainAll(l1); //Now l2 have only common elements of both list this is an optional this will work well when there are thousands of element otherwise only do remove all 
     l1.removeAll(l2); //Here magic happens this will remove common element from l1 so l1 will have only elements what are not in l2 

     for(Integer v: l1){ 
      System.out.println(v); 
     } 

輸出:

1 
5 
8 
+0

這個問題可能有負面的投票,因爲OP沒有顯示他想要完成的任何理解。在技​​術上,他的問題的正確答案是「是」...;) –

0

在第一個陣列,併爲每個元素它,如果這不是第二個數組的元素,那麼把它作爲好的,否則丟棄它。

現在你只需要學習如何用java語言編寫它!

0

你可以這樣做。

Integer[] array1 = {1, 652 ,5, 15, 385, 4 , 55, 666, 13}; 
Integer[] array2 ={2, 4658, 9, 55, -588, 10, 1083, 17}; 
List<Integer> list= new ArrayList<Integer>(Arrays.asList(array1)); 
TreeSet<Integer> set = new TreeSet<Integer>(list); 
set.removeAll(Arrays.asList(array2)); 

System.out.println(set); 
相關問題