android循环比较两个单词数组,输出不一样的单词
时间: 2024-05-13 15:15:56 浏览: 6
你可以使用两个嵌套的 for 循环来比较两个单词数组中的单词是否相同。如果发现一个单词在第一个数组中存在但在第二个数组中不存在,或者在第二个数组中存在但在第一个数组中不存在,那么就说明这个单词不相同。
下面是一个示例代码:
```java
String[] words1 = {"apple", "banana", "cherry", "orange"};
String[] words2 = {"apple", "banana", "pear", "watermelon"};
for (String word1 : words1) {
boolean found = false;
for (String word2 : words2) {
if (word1.equals(word2)) {
found = true;
break;
}
}
if (!found) {
System.out.println(word1);
}
}
for (String word2 : words2) {
boolean found = false;
for (String word1 : words1) {
if (word2.equals(word1)) {
found = true;
break;
}
}
if (!found) {
System.out.println(word2);
}
}
```
输出结果为:
```
cherry
orange
pear
watermelon
```
其中,第一个循环用来找出在第一个数组中存在但在第二个数组中不存在的单词,第二个循环用来找出在第二个数组中存在但在第一个数组中不存在的单词。