比较两个字符串值并在java中返回索引
我有两个包含一些十六进制值的字符串。比较两个字符串值并在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)。 任何帮助或建议,将不胜感激。
考虑这样的代码:
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开始的索引)创建索引。
这是很好的支持证据,用原始值比用十六进制字符串更容易做到这一点!如果可以在转换之前获取字节值,那么修改此方法以直接使用它们将更容易,而不必将字符串转换回“int []”。但是如果只有字符串可以使用,这种转换为int []的方法是一个好方法。 – 2014-09-26 12:28:31
@przemek hertel,非常感谢您的帮助。真的很感谢你的时间,努力和解释。 – vbenthu 2014-09-26 12:44:53
希望对您有所帮助!
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.");
}
更有效的方法是不给每个时间搜索第二串,而是它转变成更合适的数据结构,诸如哈希映射,其中关键是字符串对和值是位置的列表时此对出现。这种方法的复杂性是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);
}
}
}
}
}
嗯,你为什么不要输出'(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