搜索字符数组以查看字符是否匹配

我有两个 char 数组,它们将两个字符串作为输入。如果两侧的 char 具有匹配的 char,例如,将 char 数组转换为字符串 char A 和 B 都至少有一个 H 或 R,则它将返回 true。如果不是,则返回 false。


>char[] A = foo(A).toCharArray();

>

>char[] B = foo(B).toCharArray();

>

>System.out.println("String "+A+": "+Arrays.toString(A));

>

>System.out.println("String "+B+": "+Arrays.toString(B));


>String A: [H,  , R,  ]

>

>String B: [R,  , R, R]

>>This will return true


>String A: [ , H,  , R]

>

>String B: [H, H,  , H]

>>This will return true


>String A: [H,  , H,  ]

>

>String B: [R,  , R,  ]

>>This will return false

我很困惑如何制定这样的规则?


尚方宝剑之说
浏览 84回答 5
5回答

天涯尽头无女友

您可以在此处使用,这将在一次迭代中给出结果。使用进一步减少执行时间,因为它消除了重复java.util.Setjava.util.TreeSetpublic static void main(String[] args) {&nbsp; &nbsp; char[] A = "HR".toCharArray();&nbsp; &nbsp; char[] B = "RRR".toCharArray();&nbsp; &nbsp; Set<Character> set = new TreeSet<>();&nbsp; &nbsp; boolean flag = false;&nbsp; &nbsp; for(char c : A) {&nbsp; &nbsp; &nbsp; &nbsp; set.add(c);&nbsp; &nbsp; }&nbsp; &nbsp; for(char c : B) {&nbsp; &nbsp; &nbsp; &nbsp; if(set.contains(c)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(!flag) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(false);&nbsp; &nbsp; }}

明月笑刀无情

您可以做的是使用 For 在 matriz 中迭代,并验证当前项是“R”还是“H”。boolean returnedValue = false;for(int i = 0; i< B.length; i++){&nbsp; char currentItem = B[i];&nbsp; if(currentItem == 'R' || currentItem == 'H'){&nbsp; &nbsp; returnedValue = true;&nbsp;}}return returnedValue;

潇湘沐

好吧,它很简单,你所要做的就是添加一个嵌套的循环for(int i = 0; i < A.length; i++){&nbsp; &nbsp; &nbsp; for(int j = 0; j < B.length; j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(if B[j] ==A [i]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; return false;

冉冉说

使用Java 1.8,您可以执行以下操作://@Momir SaracString text1 = "b9H ello";String text2 ="avn1c fk";// 32 is for empty space character here, you could do i != ' ' and it would be the same//turn text1 into intstream each int corresponding to it's char value//filter empty space ints//get only distinct out of them//take a look for any match if some int is contained within text2boolean result = text1.chars().filter(i->i != 32).distinct().anyMatch(character->text2.contains(String.valueOf(character)) || text2.lastIndexOf(character) != -1);//print it on screenSystem.out.printf("Two texts %s same letter(s).", result ? "have" : "don't have");

子衿沉夜

使用第一个循环从第一个数组中获取每个元素。使用第二个循环检查第二个数组中的第一个元素。检查第一个数组中的当前值是否等于 H 或 R。如果是,请检查它是否在第二个数组中并返回 true。public static boolean check() {&nbsp; &nbsp; &nbsp; &nbsp; String s1 = "ABHEEF", s2 = "RDDFVW";&nbsp; &nbsp; &nbsp; &nbsp; char[] arr1 = s1.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; char[] arr2 = s2.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < arr1.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < arr2.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(arr1[i] == 'H' || arr1[i] == 'R') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(arr1[i] == arr2[j])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java