2016-11-23 68 views
-1

我有兩個不同大小的數組。 方案1.兩個不同大小的數組通過索引來索引匹配比較

A=[1,2,3] 
b=[1,3,2,4] 

場景2.

a=[1,3,2,4] 
b=[1,3,2,] 

如何都通過索引陣列匹配索引,並且如果存在任何差異顯示結果。 當兩個數組的長度相同時,我能夠得到結果。但是當數組大小A比B短或者反之時,那麼得到IndexOUTofBoundexception明顯的原因是它試圖訪問不存在的元素。

for (int j=i;j<=i;j ++) 
if (A[i].equals(B[i])) { 
System.out.println(A[i] + "\t " + B[i]); 
} else { 
NumberOfDifference++; 
System.out.println(arrayLines1[i] + "--" + arrayLines2[i]); 
       } 

如果兩個數組之間存在差異,只需要顯示元素即可。方案2中的 案例Array a具有新元素4,但Array b具有空值。

+2

做什麼,如果大小不同?那麼匹配標準是什麼呢? –

+1

當尺寸不相等時你想要什麼?有什麼要求?假設缺失元素爲零? – developer

+1

你還沒有說過你想要的結果是與不存在的元素進行比較。 –

回答

0

顯然你只能比較匹配索引直到最短索引。這個問題沒有指定你應該用剩餘的元素做什麼,但是,爲了便於討論,讓我們說,我們只是把它們打印:

int sharedLength = Math.min(a.length, b.length); 
for (int i = 0; i < sharedLength; ++i) { 
    if (a[i].equals(b[i])) { 
     System.out.println(a[i] + "\t" + b[i]); 
    } else { 
     System.out.println(a[i] + "--" + b[i]); 
} 

// Here, you could either use an if statement to find the longer array 
// Or, more elegantly(?), just loop over both of them. 
// One of these loops will not execute because the array is too short: 
for (int i = sharedLength; i < a.length; ++i) { 
    System.out.println(a[i] + "--"); 
} 
for (int i = sharedLength; i < b.length; ++i) { 
    System.out.println("\t--" + b[i]); 
} 
0
int m = A.size() 
int n = B.size() 

i =0 
NumberOfDifference = 0 

while (i< m && i<n) 
    if (a[i]==b[i]){   
      System.out.println(A[i] + "\t " + B[i]); 
    }else{ 

     NumberOfDifference++; 
     System.out.println(A[i] + "--" + B[i]); 
     } 
     i++; 
     } 
while (i<m) { 
      NumberOfDifference++; 
      System.out.println(A[i] + "--" + "NO Element");  
      i++ 
} 
while (i<n) { 
      NumberOfDifference++; 
      System.out.println("No Element" + "--" + B[i]);  
      i++; 
} 
+0

此代碼不起作用nullPointerexception –

+0

您需要初始化A&B – Kajal

0

這種方式可以讓你找出不同元素:

for (int i=0; i < Math.max(A.length, B.length); i++) { 
    if (i >= A.length) { 
    NumberOfDifference++; 
    System.out.println("--" + B[i]); 
    } else if (i >= B.length) { 
    NumberOfDifference++; 
    System.out.println(A[i] + "--"); 
    } else if (A[i].equals(B[i])) { 
    System.out.println(A[i] + "\t " + B[i]); 
    } else { 
    NumberOfDifference++; 
    System.out.println(A[i] + "--" + B[i]); 
    } 
} 

這是最簡單的方法:

Arrays.equals(A,B) 
+0

這不是解決方案,因爲匹配是索引到索引。如果兩個數組索引中的值不匹配,或者一個數組有值而另一個數組有空,則應計算差異。 –

+0

這發生在第3行和第6行。 – mm759

0

你可以只嘗試通過陣列我們遍歷最短陣列的長度爲最大值。

例如:

A=[1,2,3]; 
B=[1,3,2,4]; 
// Assign the shorter length to the temporary Integer minimum length 
int minimum_length = (A.length <= B.length) ? A.length : B.length 
for (int count=0; count < minimum_length; count++) { 
    if (A[count).equals(B[count]) { 
     System.out.println(A[i] + "\t " + B[i]); 
    } else { 
     NumberOfDifference++; 
     System.out.println(A[i] + "--" + B[i]); 
} 
//Now you can just add all other integers as differences to the NumberOfDifferences-Object 
for (int others = A.length; others < B.length; others++) { 
    NumberOfDifferences++; 
    System.out.println("null" + "--" + B[i]); 
} 

這應爲一個選項,現在我想你可能只是轉身此實現可以添加對A大於B.

長的情況下相同的選項做的工作

希望這是有用的!

0

這個問題不像其他人所說的那麼清楚 所以我做出我的假設,你需要通過索引比較來做索引並顯示額外的元素。

所以,你可以這樣做如下:

  1. 先找陣列使用數組比較小的索引和循環: 比方說,A = [1,2,3] B = [1,3 ,2,4] 所以我們循環A的長度。

    int size = A.length() < B.length() ? A.length() : B.length(); for (int i=0;i < size; i++) { if (A[i].equals(B[i])) { System.out.println(A[i] + "\t " + B[i]); } else { NumberOfDifference++; }

  2. 現在你可以遍歷了較大的陣列和表演的區別: 決定以同樣的方式來遍歷該數組: if(A.length() < B.length()) {
    for(int i=size; i < A.length(); i++) { System.out.println(A[i]); } } else { for(int i=size; i < B.length(); i++) { System.out.println(B[i]); } }

0
  1. 您好,您可以將用戶陣列列表,但如果你堅持使用陣列 這裏是我的解決方案:

    //你有兩個數組: 「一[]」 和 「B []」

    if (a.length == b.length){ // the code that you say have have written 
        } 
        else if (a.length > b.length) 
        { 
         int temp[] = new int[a.length]; // initialize a int[] with length of array a[] 
         for (int i = 0 ;b.length>i ; i++){ 
          temp[i] = b[i]; 
         } 
    
         for (int i = b.length; a.length> i ; i++) 
         { 
          temp[i] = -1 ; // some vaue you know wont be in your array 
         } // now you have two array with same length 
         int NumberOfDifference = 0 ; 
         for (int i=0 ; i<a.length;i++){ 
          if (a[i]==(b[i])) { 
           System.out.println("equal occured at index : " + i +" : "+ a[i] + "\t " + b[i]); 
          } 
          else 
          { 
           NumberOfDifference++; 
           System.out.println("equal occured at index : " + i +" : "+ a[i] + "\t " + b[i])); 
          } 
         } 
    
        } // a.length < b.length 
        else { 
    
         int temp[] = new int[b.length]; // initialize a int[] with length of array b[] 
         for (int i = 0 ;a.length>i ; i++){ 
          temp[i] = a[i]; 
         } 
    
         for (int i = a.length; b.length> i ; i++) 
         { 
          temp[i] = -1 ; // some vaue you know wont be in your array 
         } // now you have two array with same length 
         int NumberOfDifference = 0 ; 
         for (int i=0 ; i<b.length;i++){ 
          if (b[i]==(a[i])) { 
           System.out.println("equal occured at index : " + i +" : "+ a[i] + "\t " + b[i]); 
          } 
          else 
          { 
           NumberOfDifference++; 
           System.out.println("equal occured at index : " + i +" : "+ a[i] + "\t " + b[i])); 
          } 
         } 
    
        } 
    
相關問題