Java 检查两个数组之间的相同值
我有两个Java 检查两个数组之间的相同值,java,android,arrays,Java,Android,Arrays,我有两个数组,如下所示: String a[]={"book","pen"}; String b[]={"pen","pencil","bottle","book","bag"}; 我想检查数组b和数组a之间的值是否相同,我尝试了以下方法: for (int i=0; i < b.length; i++){ for (int j=0; j < a.length(); j++){ if(!(a.contains(b[i])))
数组
,如下所示:
String a[]={"book","pen"};
String b[]={"pen","pencil","bottle","book","bag"};
我想检查数组b
和数组a
之间的值是否相同,我尝试了以下方法:
for (int i=0; i < b.length; i++){
for (int j=0; j < a.length(); j++){
if(!(a.contains(b[i]))){
//delete if not same
}
for(int i=0;i
所以我的问题是,如何在两个arraylist android之间检查相同的值?您可以这样做。在API中使用
.equals()
方法
for(int i=0;i
试试这个
for (int i=0; i < b.length; i++){
for (int j=0; j < a.length(); j++){
if(!(a[j].equals(b[i]))){
//do something for not equals
}else{
//do something for equals
}
}
}
试试这个。我用它来表示int数组,你可以修改它来表示string
int[] arr1 = {4,7,3,9,2};
int[] arr2 = {3,2,12,9,40,32,4};
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
if(arr1[i]==arr2[j]){
System.out.println(arr1[i]);
}
}
}
int[]arr1={4,7,3,9,2};
int[]arr2={3,2,12,9,40,32,4};
对于(int i=0;i您可以尝试使用Collections
现在重命名为Guava
从中获取jar文件
由于数组列表的长度不同,因此可以执行以下操作:
1.获取最大大小数组的长度
int sizeOfArrayOne = array.length;
int sizeOfArrayTwo = array2.length;
比较大小
2.获得最大数组后,您可以使用Guava从另一个数组中查找值,如:
String value = Iterables.find(arrayWithLargerSize, new SearchForString(arrayOfSmallerSize[i]),null);
if(value == null) {
// the string is not present in the array
}else {
// the string is present in the array
}
在较小数组的循环中运行此块(i表示循环的i)
3.编写如下搜索谓词类:
class SearchForString implements Predicate<String> {
// override the methods here
// also ensure that you implement the correct predicate
// one that belongs to the guava collection
}
类SearchForString实现谓词{
//重写此处的方法
//还要确保实现了正确的谓词
//番石榴属的一种
}
希望这条线索足够了!字符串a[]={“书”,“笔”};
String a[]={"book","pen"};
String b[]={"pen","pencil","bottle","book","bag"};
for(int i=0;i<b.lenght;i++){
String firstValue=b[i];
for(int j=0;j<a.lenght;j++){
String secondValue=a[j];
if(firstValue.equalsIgnoreCase(secondValue)){
// item matches;
}
}
}
字符串b[]={“笔”、“铅笔”、“瓶子”、“书”、“包”};
对于(int i=0;i对于contains
,您不必为循环使用2,因为只有1个数组在循环。ArrayList
几乎相同。contains
函数也应该执行以下操作trick@Coderji因为它们之间的长度不同,所以您没有移动a
数组(您没有使用a[j].包含..
)…您正在用整a
a.length
时间检查b
值。默认情况下,通过a
执行contains函数循环。check@HARIHARARAN arraylist contains。他没有为b_数组执行for循环。因为contains函数默认情况下会循环它。希望您能想到。如果我想删除if b!=a,您可以n创建一个临时列表,您可以在其中仅放置满足条件的值。您可以从数组本身创建结果列表,而不是将数组转换为列表并在检查条件后创建结果列表。为什么不直接编写B_arrayList.retainal(a_arrayList);
?不要重新发明轮子!@Hariharan现在它不能被删除anything@NenMa只需使用B_arraylist.retainal(A_arraylist);
查看我的编辑。我必须将B_arraylist.retainal(A_arraylist);
?@NenMa您得到结果了吗。
int sizeOfArrayOne = array.length;
int sizeOfArrayTwo = array2.length;
String value = Iterables.find(arrayWithLargerSize, new SearchForString(arrayOfSmallerSize[i]),null);
if(value == null) {
// the string is not present in the array
}else {
// the string is present in the array
}
class SearchForString implements Predicate<String> {
// override the methods here
// also ensure that you implement the correct predicate
// one that belongs to the guava collection
}
String a[]={"book","pen"};
String b[]={"pen","pencil","bottle","book","bag"};
for(int i=0;i<b.lenght;i++){
String firstValue=b[i];
for(int j=0;j<a.lenght;j++){
String secondValue=a[j];
if(firstValue.equalsIgnoreCase(secondValue)){
// item matches;
}
}
}