2014-09-26 89 views
0

我有兩個包含一些十六進制值的字符串。比較兩個字符串值並在java中返回索引

String One = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b"; 
String Two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4"; 

請注意,在十六進制之間的空間由被轉換二進制hex.Furthermore功能生成,十六進制值是在對例如治療,F9 D4被視爲一對。

我的問題是 - 我怎樣才能返回兩個字符串中的兩個類似對的索引(在這種情況下,子字符串值)? 上面的例子在索引1中有f9 d4,而另一個字符串在索引6上有它。所以我希望我的輸出爲(1,6)和(3,6)。 任何幫助或建議,將不勝感激。

+0

嗯,你爲什麼不要輸出'(1,6)(3,6)'?有多場比賽時你在找什麼?什麼時候沒有任何比賽? – 2014-09-26 12:13:04

+0

用轉換前的字節做這件事比處理字符串要容易。 – 2014-09-26 12:13:49

+0

@ chiastic-security,是的,實際的字符串包含多對,我需要我的輸出顯示,因爲你已經提到。 (1,6)和(3,6)。 – vbenthu 2014-09-26 12:17:30

回答

1

考慮這樣的代碼:

public static void main(String[] args) { 
    String One = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b"; 
    String Two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4"; 

    int[] x = convert(One); 
    int[] y = convert(Two); 

    for (int i = 0; i < x.length; i++) { 
     int number = x[i]; 
     int index = find(number, y); 
     if (index > 0) { 
      System.out.println("found (" + (i + 1) + "," + index + ")"); 
     } 
    } 

} 

private static int find(int number, int[] array) { 
    for (int i = 0; i < array.length; i++) { 
     if (array[i] == number) { 
      return i + 1; 
     } 
    } 
    return 0; 
} 

private static int[] convert(String str) { 

    String[] tokens = str.split("\\s"); 
    int[] result = new int[tokens.length/2]; 

    for (int i = 0; i < tokens.length; i += 2) { 
     String hex = tokens[i] + tokens[i + 1]; 
     result[i/2] = Integer.parseInt(hex, 16); 
    } 

    return result; 
} 

輸出:

found (1,6) 
found (3,6) 

正如你可以看到convert(str)方法每4個十六進制數字轉換爲1點的整數並返回這樣的整數數組。所以,轉換(一)僅僅是INT []等於:

System.out.println(Arrays.toString(x)); 
[63956, 25258, 63956, 25258, 29712, 39307] 

接下來,您可以實現的輔助方法find()方法返回其中number是在給定的數組(1開始的索引)創建索引。

+2

這是很好的支持證據,用原始值比用十六進制字符串更容易做到這一點!如果可以在轉換之前獲取字節值,那麼修改此方法以直接使用它們將更容易,而不必將字符串轉換回「int []」。但是如果只有字符串可以使用,這種轉換爲int []的方法是一個好方法。 – 2014-09-26 12:28:31

+0

@przemek hertel,非常感謝您的幫助。真的很感謝你的時間,努力和解釋。 – vbenthu 2014-09-26 12:44:53

1

希望對您有所幫助!

String one = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b"; 
String two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4"; 

String[] oneArr = one.split(" "); 
String[] twoArr = two.split(" "); 

ArrayList<String> results = new ArrayList<>();  

for(int i = 0, countOne = 0 ; i < oneArr.length - 1 ; i = i + 2,countOne++) { 

    String hexCoupleOne = oneArr[i] + " " + oneArr[i + 1]; 

    if(two.contains(hexCoupleOne)) { 
     //searching index in two . . . 
     for(int j = 0, countTwo = 0 ; j < twoArr.length - 1 ; j = j + 2, countTwo++) { 
      String hexCoupleTwo = twoArr[j] + " " + twoArr[j + 1]; 
      if(hexCoupleOne.equals(hexCoupleTwo)) 
       results.add((countOne + 1) + "," + (countTwo + 1)); 
     }   
    }  
} 

System.out.println("total pair : "+results.size()); 
for(String res : results) { 
    System.out.println("Found a pair at index="+res.split(",")[0]+" in String one and at index="+res.split(",")[1]+" in String two."); 
} 
1

更有效的方法是不給每個時間搜索第二串,而是它轉變成更合適的數據結構,諸如哈希映射,其中關鍵是字符串對和值是位置的列表時此對出現。這種方法的複雜性是O(n),與每次搜索第二個字符串時的O(n )相比較。當你有更大的輸入字符串時,這種差異會很大。

/** 
    * Splits argument into substrings of lengths 6 and puts them into Map 
    * where key is substring and value is the list of positions where substring appears 
    * in original string. 
    * @param str string to split 
    * @return Map of positions 
    */ 
    private static Map<String, List<Integer>> indexMapOfPairs(String str) { 
     Map<String, List<Integer>> result = new HashMap<String, List<Integer>>(); 
     for (int i = 0; i < str.length(); i += 6) { 
      String pair = str.substring(i, i + 5); 
      List<Integer> indexList = result.get(pair); 
      if (indexList == null) { 
       indexList = new ArrayList<Integer>(4); 
       result.put(pair, indexList); 
      } 
      indexList.add(i/6 + 1); 
     } 
     return result; 
    } 

    public static void main(String[] args) { 
     String one = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b"; 
     String two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4"; 

     Map<String, List<Integer>> oneAsMap = indexMapOfPairs(one); 
     Map<String, List<Integer>> twoAsMap = indexMapOfPairs(two); 

     for (Map.Entry<String, List<Integer>> oneEntry : oneAsMap.entrySet()) { 
      String pair = oneEntry.getKey(); 
      List<Integer> twoIndices = twoAsMap.get(pair); 
      if (twoIndices != null) { 
       for (Integer oneIndex : oneEntry.getValue()) { 
        for (Integer twoIndex : twoIndices) { 
         System.out.printf("(%d, %d)%n", oneIndex, twoIndex); 
        } 
       } 
      } 
     } 
    }