2

I have two Array as below :

String a[]={"book","pen"};
String b[]={"pen","pencil","bottle","book","bag"};

I want to check, same value between array b and array a, i have try with this :

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
            }

so my question,how to check same value between two arraylist android?

4
  • you don't have to use 2 for loops for contains... since only 1 array is looping.. for ArrayList it is pretty much the same.. also contains function should do the trick Commented Apr 2, 2014 at 4:06
  • @Coderji because lenght between them is different Commented Apr 2, 2014 at 4:13
  • you are not moving a array( you are not using a[j].contains..).. you are checking the b value with whole a a.length time.. the contains function loop through a by default. Commented Apr 2, 2014 at 4:20
  • check @Hariharan arraylist contains.. he didn't do a for loop for b_array.. because the contains function loops it by default. hope you got the idea. Commented Apr 2, 2014 at 4:22

5 Answers 5

9

Try this..

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
            }
        }
}

Or

ArrayList<String> A_arraylist = new ArrayList<String>(Arrays.asList(a));
ArrayList<String> B_arraylist = new ArrayList<String>(Arrays.asList(b));

for (int i=0; i < A_arraylist.size(); i++){
     if(B_arraylist.contains(A_arraylist.get(i))){
          //do something for equals              
     }else{
          //do something for not equals
          int index = B_arraylist.indexOf(A_arraylist.get(i));
          B_arraylist.remove(index);
     }
}

EDIT:

B_arraylist.retainAll(A_arraylist);
Sign up to request clarification or add additional context in comments.

16 Comments

Why not just write B_arrayList.retainAll(A_arraylist); ? Don't reinvent wheels!
@Hariharan now its not delete anything
@NenMa just use B_arraylist.retainAll(A_arraylist); see my edit.
where do i must to put B_arraylist.retainAll(A_arraylist);?
@NenMa did you get the result.
|
2

You can do it this way. Use .equals() method in String API

for (int i=0; i < b.length; i++){

    for (int j=0; j < a.length; j++){
         if(a[j].equals(b[i])){
             // a[j] matches b[i]. Perform operation when they are equal                            
         }else{
             // it doesn't match
         }
    }
}

2 Comments

how if i want to remove if b!=a ?
You can create a temp list where you can put only values which satisfy the condition. Instead of converting the arrays to list and create a resultant list after checking the conditions, you can create the resultant list from the arrays itself.
1

try this . I used it for int array you can modified it for 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]);
                }
            }
        }

Or if your using ArrayList then try below

public static void main(String[] args) {
        List<String> list_One = new ArrayList<String>();
        List<String> list_two = new ArrayList<String>();

        list_One.add("A");
        list_One.add("B");
        list_One.add("C");
        list_One.add("D");

        list_two.add("E");
        list_two.add("F");
        list_two.add("C");
        list_two.add("D");


        System.out.println("is list_two Containts List_one Elements ?");

        Iterator<String> itr =list_two.iterator();

        while(itr.hasNext()){
            String list_two_element = itr.next();

            if(list_One.contains(list_two_element)){
                System.out.println("Matching Element found in list two : " + list_two_element);
            }

        }
    }

Hope this will help you.Thanks

Comments

1

You could try using Collections now renames to Guava

Get the jar file from here

Since the length of the array list is different what you can do is:

1.Get the lengh of the array with the largest size

int sizeOfArrayOne = array.length;
int sizeOfArrayTwo = array2.length;

compare the sizes

2.Once you obtain which array is the largest you could then use Guava to find for you the values from another array like:

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
}

Run this block inside a loop of the smaller array (the i represents the i for the loop).

3.Write the search predicate class like this:

class SearchForString implements Predicate<String> {
     // override the methods here
     // also ensure that you implement the correct predicate
     // one that belongs to the guava collection
}

Hope this clue suffices!

Comments

1
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;
}
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.