简体   繁体   English

Java | 比较char数组中的char字

[英]Java | compare char word in a char array

How do I get the index of a single word (represents in a char array) which can be found in a paragraph (again represents in a char array). 如何获取可在段落中找到的单个单词的索引(表示在char数组中)(再次表示在char数组中)。

the char represents the word char代表这个词

char word[] = new char[]{'w','o','r','d'};

and here's the paragraph 这是段落

char para[] = new char[]{'f','g','q','z','y','i','o','p','w','o','r','d'};

I would like to get the index of the first letter in this case 8th. 我希望在这种情况下得到第一个字母的索引8。 I used binary search by when sorting the words get scrambled. 我在排序单词scrambled时使用了二进制搜索。

Thanks. 谢谢。

A bit inefficient theoretically, but rather practical and simple: 理论上有点低效,但实用而简单:

int position = new String(paragraph).indexOf(new String(word));

If you want to understand how this works - check the static int indexOf(..) method of java.lang.String 如果你想了解它是如何工作的 - 检查java.lang.Stringstatic int indexOf(..)方法

Binary search won't help you in this case. 在这种情况下,二进制搜索不会帮助您。 You have to search linearly. 你必须线性搜索。 The easiest solution would be to search linearly for the first character and, when found, check if the remaining word follows. 最简单的解决方案是线性搜索第一个字符,找到后检查剩下的字是否跟随。

A more elaborate solution would be to use a KMP algorithm . 更精细的解决方案是使用KMP算法

The simplest method is just to try all possibilities, by looping through each starting point and testing if all characters match. 最简单的方法就是尝试所有可能性,循环遍历每个起始点并测试所有字符是否匹配。 By the fact you've already mentioned binary search, this is probably simple enough for you to already know, though let me know if that's what you're looking for. 事实上你已经提到了二元搜索,这可能很简单,你已经知道了,但请告诉我你是否正在寻找它。

If you're looking for the best method, see http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm . 如果您正在寻找最佳方法,请参阅http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

You can convert the character arrays to strings. 您可以将字符数组转换为字符串。 The result of the search in the string is the same as if you had searched the arrays. 在字符串中搜索的结果与搜索数组时的结果相同。

String needle = new String(word);
String haystack = new String(para);
int i = haystack.indexOf(needle);

Result: 结果:

8

This can be much faster than a naive O(n*m) search because the string function indexOf is optimized. 这可能比天真的O(n * m)搜索快得多,因为字符串函数indexOf已经过优化。

If you want to do it without creating the temporary strings you can implement a string searching algorithm for byte arrays. 如果要在不创建临时字符串的情况下执行此操作,则可以为字节数组实现字符串搜索算法 You could for example choose the Boyer-Moore algorithm which has worst case O(n). 例如,您可以选择具有最差情况O(n)的Boyer-Moore算法。

Quick answer and I suppose others will be more elaborate. 快速回答,我想其他人会更精细。 Initially, I'd do something like this (pseudocode is better for thinking out algorithms): 最初,我会做这样的事情(伪代码更适合思考算法):

boolean nonmatchingchar
integer i, j
for each i of word until endof word
    for each j of para until endof para
      if word i isnotequalto para i set nonmatchingchar true     
    end for
end for


if nonmatchingchar is true print "character sequence not found"

Edit: To make this more efficient in the case where you'd have several words to search for, you could constitute a two-dimensional array with words sorted according to their first letter. 编辑:为了在你有几个要搜索的单词的情况下提高效率,你可以构成一个二维数组,其中的单词按照第一个字母排序。 From there on, you could go through the second array letter by letter and test a subset of words according to that letter. 从那以后,你可以逐字逐句地查看第二个数组,并根据该字母测试一个单词的子集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM