0

I want to compare two arrays and store the difference in another array

For example the two arrays might be

String[] a1 = { "cat" , "dog" };
String[] a2 = { "cat" , "rabbit" };

The resultant array would be like this

{ "rabbit" }

I use this code, but it does not work

int n = 0;
for (int k = 0; k <= temp.length; k++)
{
    for (int u = 0; u <= origenal.length; u++)
    {
        if (temp[k] != origenal[u] && origenal[u] != temp[k])
        {
            temp2[n] = temp[k];
            System.out.println(temp[u]);
            n++;
        }
    }
}
4
  • 7
    What have you tried? Commented Dec 20, 2012 at 15:18
  • 4
    Easy: String result = "rabbit"; Commented Dec 20, 2012 at 15:18
  • How did you only get "rabbit" in the result? We need much more information to help you. Commented Dec 20, 2012 at 15:27
  • @DanW I assume he is storing the difference between a2 and a1: a2\a1 is "rabbit". Commented Dec 20, 2012 at 15:28

2 Answers 2

1

This should do the trick.

String[] result = new String[100];
Int k = 0;
Boolean test = true;
for(i=0; i < a1.length; i++){
   for(j=0; j < a2.length; j++){
      if(a2[i].equals(a1[i])) continue;
      test = false
   }
   if(test == false) result[k++] = a1[i];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user902383 duh, yes figured that out just after posting it
1

I think that this may be what you are looking for. Note that it will only add to the third 'array' if the value exist in second array but not in first. In your example only rabbit will be stored, not dog (even though dog does not exist in both). This example could possibly be shortened but I wanted to keep it like this so it is easier to see what is going on.

First import:

import java.util.ArrayList;
import java.util.List;

Then do the following to populate and analyze the arrays

String a1[] = new String[]{"cat" , "dog"};    // Initialize array1
String a2[] = new String[]{"cat" , "rabbit"}; // Initialize array2

List<String> tempList = new ArrayList<String>();
for(int i = 0; i < a2.length; i++)
{
    boolean foundString = false; // To be able to track if the string was found in both arrays
    for(int j = 0; j < a1.length; j++)
    {
        if(a1[j].equals(a2[i]))
        {
            foundString = true; 
            break; // If it exist in both arrays there is no need to look further
        }
    }
    if(!foundString) // If the same is not found in both..
        tempList.add(a2[i]); // .. add to temporary list
}

tempList will now contain 'rabbit' as according to the specification. If you necessary need it to be a third array you can convert it to that quite simply by doing the following:

String a3[] = tempList.toArray(new String[0]); // a3 will now contain rabbit

To print the content of either the List or Array do:

// Print the content of List tempList
for(int i = 0; i < tempList.size(); i++)
{
    System.out.println(tempList.get(i));
}

// Print the content of Array a3
for(int i = 0; i < a3.length; i++)
{
    System.out.println(a3[i]);
}

11 Comments

there are error in this statement for(String s2: a2), the error is "expected ; " is there any import should I do?
You should only need java.util.ArrayList and import java.util.List to use the list. I can reconstruct the loop to not use the for-each variant.
@user1888020 you can change from for(String s2 : a2) to for(int i = 0; i < a2.length; i++) and then in the boddy where you have s2, change it to a2[i], same for s1 and a1, using another int variable like j.
I updated the example to not use the for-each style loop. Does that work better?
when I want to print the content of tempList, the error arise index 0, size 0,
|

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.