考慮這樣的代碼:
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開始的索引)創建索引。
嗯,你爲什麼不要輸出'(1,6)(3,6)'?有多場比賽時你在找什麼?什麼時候沒有任何比賽? – 2014-09-26 12:13:04
用轉換前的字節做這件事比處理字符串要容易。 – 2014-09-26 12:13:49
@ chiastic-security,是的,實際的字符串包含多對,我需要我的輸出顯示,因爲你已經提到。 (1,6)和(3,6)。 – vbenthu 2014-09-26 12:17:30