gpt4 book ai didi

java - 使用 boolean 值来声明重复单词的 true 或 false,而不使用循环结构

转载 作者:行者123 更新时间:2023-11-30 01:58:14 24 4
gpt4 key购买 nike

public class Main                                       
{
public static void main(String[] args)
{
String[] find2 = {"cool","NOT","NOT,"Cool"};
String[] nofind = {"Fly","poke","ok";
System.out.println(Test.out(find);
System.out.println(Test.out(nofind));
}
}

+

public class Test                                   
{
public static boolean Out(String[] input) {
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < input.length; j++) {
if (input[i].equals(input[j]) && i != j) {
return true;
}
}
}
return false;
}

我已经成功地运行了一个代码,如果字符串数组中存在重复的单词系列,它会告诉我它是真还是假,但是,如果没有同时查找大写和小写的循环结构,我如何才能实现这一点骗子?

最佳答案

简而言之,如果没有任何循环就无法做到这一点。但是,如果您正在寻找隐藏循环,则可以使用以下命令:

public static boolean out(String[] input) {
return input.length != new HashSet<String>(Arrays.asList(input)).size();
}

如果您需要忽略大小写,那么:

public static boolean out(String[] input) {
return input.length != Arrays.stream(input).map(String::toLowerCase).collect(Collectors.toSet()).size();
}

关于java - 使用 boolean 值来声明重复单词的 true 或 false,而不使用循环结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com