Java数组搜索
在java中,数组有三个单词Java数组搜索,java,arrays,string,search,Java,Arrays,String,Search,在java中,数组有三个单词{'bad'、'bat'、'hat'}。我必须写一个函数,它将像字典一样从数组中搜索 示例:如果我搜索at,则函数将返回其中包含at的所有单词(如bat和hat将显示在输出中),如果我搜索单个字符,则函数将返回包含该字符的所有单词。Java中有内置函数吗 e、 g:if(string.contains(item1)| | string.contains(item2)| | | string.contains(item3))可以对数组进行流式处理,并使用 String[
{'bad'、'bat'、'hat'}
。我必须写一个函数,它将像字典一样从数组中搜索
示例:如果我搜索at,则函数将返回其中包含at的所有单词(如bat
和hat
将显示在输出中),如果我搜索单个字符,则函数将返回包含该字符的所有单词。Java中有内置函数吗
e、 g:
if(string.contains(item1)| | string.contains(item2)| | | string.contains(item3))
可以对数组进行流式处理,并使用
String[]words={“bad”、“bat”、“hat”};
字符串过滤器=“at”;
列表=流(字)
.filter(word->word.contains(过滤器))
.collect(Collectors.toList());
系统输出打印项次(列表);
对于Java 8,最好使用,因为它可以使用多个处理器。对于Java的早期版本,可以使用以下方法。第一个使用contains
方法,第二个使用正则表达式,可能对您更有用
import java.util.ArrayList;
import java.util.List;
public class SearchTest
{
public static void main(String[] args)
{
final String[] words = {"bad", "bat", "hat", "hate"};
final String filter = "at";
final String pattern = ".*at$"; // regex matching only strings that end with "at"
System.out.println(".contains");
final String[] results1 = SearchTest.search(filter, words);
for (String match : results1)
{
System.out.println(match);
}
// regex
System.out.println("Regular Expression");
final String[] results2 = SearchTest.match(pattern, words);
for (String match : results2)
{
System.out.println(match);
}
}
public static String[] search(final String substring, final String[] words)
{
final List<String> matches = new ArrayList<String>();
for (String word : words)
{
if (word.contains(substring))
{
matches.add(word);
}
}
return matches.toArray(new String[0]);
}
public static String[] match(final String regex, final String[] words)
{
final List<String> matches = new ArrayList<String>();
for (String word : words)
{
if (word.matches(regex))
{
matches.add(word);
}
}
return matches.toArray(new String[0]);
}
}
import java.util.ArrayList;
导入java.util.List;
公共类搜索测试
{
公共静态void main(字符串[]args)
{
最后一个字符串[]单词={“bad”、“bat”、“hat”、“hate”};
最终字符串过滤器=“at”;
final String pattern=“.*at$”;//正则表达式只匹配以“at”结尾的字符串
System.out.println(“.contains”);
最终字符串[]results1=SearchTest.search(过滤器,单词);
for(字符串匹配:results1)
{
系统输出打印项次(匹配);
}
//正则表达式
System.out.println(“正则表达式”);
最终字符串[]results2=SearchTest.match(模式、单词);
for(字符串匹配:results2)
{
系统输出打印项次(匹配);
}
}
公共静态字符串[]搜索(最终字符串子字符串,最终字符串[]单词)
{
最终列表匹配项=新的ArrayList();
for(字符串字:字)
{
if(word.contains(子字符串))
{
匹配。添加(word);
}
}
返回matches.toArray(新字符串[0]);
}
公共静态字符串[]匹配(最终字符串正则表达式,最终字符串[]字)
{
最终列表匹配项=新的ArrayList();
for(字符串字:字)
{
if(word.matches(regex))
{
匹配。添加(word);
}
}
返回matches.toArray(新字符串[0]);
}
}
?到目前为止,你有什么问题?另外,请在外部形式和语义上正确地格式化你的问题。例如,单引号(“
)中的某些内容通常表示Java中的字符。对于字符串
s,应使用双引号(“
)。
import java.util.ArrayList;
import java.util.List;
public class SearchTest
{
public static void main(String[] args)
{
final String[] words = {"bad", "bat", "hat", "hate"};
final String filter = "at";
final String pattern = ".*at$"; // regex matching only strings that end with "at"
System.out.println(".contains");
final String[] results1 = SearchTest.search(filter, words);
for (String match : results1)
{
System.out.println(match);
}
// regex
System.out.println("Regular Expression");
final String[] results2 = SearchTest.match(pattern, words);
for (String match : results2)
{
System.out.println(match);
}
}
public static String[] search(final String substring, final String[] words)
{
final List<String> matches = new ArrayList<String>();
for (String word : words)
{
if (word.contains(substring))
{
matches.add(word);
}
}
return matches.toArray(new String[0]);
}
public static String[] match(final String regex, final String[] words)
{
final List<String> matches = new ArrayList<String>();
for (String word : words)
{
if (word.matches(regex))
{
matches.add(word);
}
}
return matches.toArray(new String[0]);
}
}