写一段Java代码,a数组的值在b数组里面进行模糊匹配,并过滤掉匹配出来的值,将数组剩下的值返回出来
时间: 2023-06-04 10:04:12 浏览: 95
下面是 Java 代码实现:
```
public static int[] fuzzyMatch(int[] a, int[] b) {
List<Integer> resultList = new ArrayList<>();
for (int i = 0; i < a.length; i++) {
boolean matched = false;
for (int j = 0; j < b.length; j++) {
if (b[j] == a[i]) {
matched = true;
break;
}
}
if (!matched) {
resultList.add(a[i]);
}
}
int[] resultArray = new int[resultList.size()];
for (int i = 0; i < resultList.size(); i++) {
resultArray[i] = resultList.get(i);
}
return resultArray;
}
```
这段代码会将两个数组中匹配的值过滤掉,将结果存储在一个数组中返回出来。
相关问题
写一段Java代码,a数组是查询出来的对象,b数组也是查询出来的对象,a数组的值在b数组里面进行模糊匹配,并过滤掉匹配出来的值,将数组剩下的值返回出来
以下是Java代码:
public static List<Object> filterArray(Object[] a, Object[] b) {
List<Object> resultList = new ArrayList<>();
Set<Object> filterSet = new HashSet<>(Arrays.asList(b));
for (Object obj : a) {
boolean match = false;
for (Object filterObj : filterSet) {
if (filterObj.toString().contains(obj.toString())) {
match = true;
break;
}
}
if (!match) {
resultList.add(obj);
}
}
return resultList;
}
调用示例:
Object[] a = { "apple", "banana", "pear", "orange", "grape" };
Object[] b = { "apple juice", "banana bread", "watermelon", "grapefruit" };
List<Object> resultList = filterArray(a, b);
System.out.println(resultList); // 输出 [pear, orange]
阅读全文