2012-03-21 369 views
0

我希望能夠將一個字符串與所有其他字符串按順序進行比較,然後向下移動到下一個字符串,並將該字符串與其下面的所有其他字符串進行比較。將字符串與其他字符串進行排序比較

這是我的代碼。它比較第一個字符串並找不到任何匹配。然而,當另一個for循環移動到下一個值時,everystring = 1,我也是這樣比較自己。每當我經過另一個循環時,我該如何去移動內循環的一個值?

if (i == everything) continue; 

它將跳過內部循環的每次迭代:

for (int everystring = 0; everystring < children.length; everystring++) { 

    String current = children[everystring].substring(0, 
     children[everystring].indexOf("_")); 

    for (int i = 1; i < children.length; i++) { 
     String innercurrent = children[i].substring(0, 
      children[i].indexOf("_")); 

     if (current.equalsIgnoreCase(innercurrent)) { 

     System.out.println("Match"); 
     } else 
     System.out.println("No Match"); 
    } 
    System.out.println(); 
} 
+0

這將比較字符串本身。 – user541597 2012-03-21 19:52:26

+0

我通過在outer for循環中添加i = everystring + 1來修復它。 – user541597 2012-03-21 19:56:17

+0

你能否包含一些樣本數據?如果我們知道你的意圖,這可能會有所幫助。無論如何,當你在一個循環中時,你總是可以增加一個循環計數器。只是說我++;或者everystring ++; – JohnnyK 2012-03-21 19:56:38

回答

1

環總是從0到children.length [在內環],和在內部循環的開頭添加以下條件i == everything,所以你只會檢查不相等的字符串。

然而要注意你會檢查每個2串兩次(例如:你會檢查i == 1everything == 2i == 2, everything == 1
如果你不希望它:迭代從everything + 1內循環,直到children.length

+0

OP要求「...將字符串與其下面的所有其他字符串進行比較......」。你的答案只是避免將字符串與自身匹配,而不是跳過所有以前檢查過的字符串。 – sharakan 2012-03-21 19:59:22

+0

@sharakan:「在它下面」並不是一個明確的定義,我給出了兩種可能性並且包含了兩種解釋,他可以選擇他喜歡的。 – amit 2012-03-21 20:01:04

0
This. 
for (int everystring = 0; everystring < children.length - 1; everystring++) { 

    String current = children[everystring].substring(0, 
     children[everystring].indexOf("_")); 

    for (int i = everystring + 1; i < children.length; i++) { 
     String innercurrent = children[i].substring(0, 
      children[i].indexOf("_")); 

     if (current.equalsIgnoreCase(innercurrent)) { 

     System.out.println("Match"); 
     } else 
     System.out.println("No Match"); 
    } 
    System.out.println(); 
} 
1

如果我正確理解你的問題,所有你需要做的就是在你的內部循環的初始使用的everystring值:

for (int everystring = 0; everystring < children.length; everystring++) { 
    String current = children[everystring].substring(0, 
    children[everystring].indexOf("_")); 
    for (int i = everystring+1; i < children.length; i++) { 
     String innercurrent = children[i].substring(0, 
     children[i].indexOf("_")); 

     if (current.equalsIgnoreCase(innercurrent)) { 

      System.out.println("Match"); 
     } else 
      System.out.println("No Match"); 
     } 
    System.out.println(); 
} 

這將比較每個字符串與它出現在數組中的所有字符串。

+1

這將導致一個arrayindexoutofbounds當everystring == children.length - 1. – 2012-03-21 20:05:32

+0

@RalfHoppen它不會。 'i == children.length' – amit 2012-03-21 20:15:56

+0

爲true時,我 2012-03-21 20:20:14

0
int i = 0;  

    for (int everystring = 0; everystring<children.length; everystring++){ 

     String current = children[everystring].substring(0,children[everystring].indexOf("_")); 

     i = everystring+1; 

    for (;i<children.length; i++){ 
     String innercurrent = children[i].substring(0,children[i].indexOf("_")); 



     if(current.equalsIgnoreCase(innercurrent)){ 

     System.out.println("Match"); 
     }else System.out.println("No Match"); 

     /* if (current.substring(0,current.indexOf("_")).equalsIgnoreCase(innercurrent.substring(0,innercurrent.indexOf("_")))){ 




      System.out.println("Match"); 


     }*/ 

    } 
    System.out.println(); 
    } 
} 
0

這是一個奇怪的代碼,你在那裏.__。但我會做這樣的事情:

for (int i = 0; i < children.length - 1; i++) { 
    String current = children[i].substring(0,children[i].indexOf("_")); 

    for (int j = i + 1; j < children.length; j++) { 
     String compareTo = children[j].substring(0,children[j].indexOf("_")); 

     if (current.equalsIgnoreCase(compareTo)) 
      System.out.format("%s matches %s\n", current, compareTo); 
     else 
      System.out.format("%s doesn't matches %s\n", current, compareTo); 
    } 
} 

這樣你比較一切只有一次,你不要在同一位置比較本身。