简体   繁体   中英

comparing two String Arrays without knowing which one contains more values

I have to compare the values of two String Arrays in Java and save the different String. I already know how to compare String Arrays with the same size. But the problem is, that the count of values of these String Arrays is unknown in the beginning. So, it's unclear which String Array is larger.

So I have to handle following scenarios:

Scenario 1 (same size and no difference):


String[] test = {Test1, Test2, Test3}
String[] test2 = {Test1, Test2, Test3}

Scenario 2 (same size, but difference):


String[] test = {Test1, Test2, Test3}
String[] test2 = {Test1, Test2, Test4}

Scenario 3 (different size - first String Array contains more values, than second one):


String[] test = {Test1, Test2, Test3}
String[] test2 = {Test1, Test2}

Scenario 4 (different size - second String Array contains more values, than first one):


String[] test = {Test1, Test2}
String[] test2 = {Test1, Test2, Test3}

Implementation Scenario 1:


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

    if(! ( Arrays.asList(test).contains(test2[i]) ) ) {

    } else {
        System.out.println("Scenario 1");
    }
}

Implementation Scenario 2:


ArrayList<String> compare_String = new ArrayList<>();

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

    if(! ( Arrays.asList(test).contains(test2[i]) ) ) {

        compare_String.add(test2[i]);
        System.out.println("Scenario2");

    } else {
        System.out.println("Scenario 1");
    }
} System.out.println(compare_String);

But how to handle Scenario 3 and 4 if you don't know whether the first String Array contains more elements than the second one, or the second String Array contains more elements than the first one?

Many Thanks.

Update: thanks a lot for your answers. This works for me:

ArrayList<String> difference = new ArrayList<>();
int j = 0;

if (test > test2) {
  try {
     for (int i = 0; i < test; i++) {
         if (!(Arrays.asList(test2).contains(test1[i]))) {
             difference.add(test[i]);
         }

         j++;

     }

 } catch (ArrayIndexOutOfBoundsException ar) {
     difference.add(test[j]);
 }

Use a boolean array to keep track of repeated Strings, and simply check all elements of one array to see if the other contains it. The unused Strings of the second array are also missing on the first, so you can put them on the difference, regardless of array sizes.

String[] array1;
String[] array2;

ArrayList<String> diff = compare_arrays(array1, array2);

public ArrayList<String> compare_arrays(String[] a1, String[] a2){
    ArrayList<String> diff = new ArrayList<String>();

    boolean[] rep = new boolean[a2.length];

    Arrays.fill(a2, false);

    for(String str : a1){
        if(!Arrays.asList(a2).contains(str)){
            diff.add(str);
        }
        else{
            rep[Arrays.asList(a2).indexOf(str)] = true;
        }
    }

    for(int i = 0; i < a2.length; i++){
        if(!rep[i]){
            diff.add(a2[i]);
        }
    }
}

I have implement compareArrays method to handle your scenario try like following this may solve your problem

  public class Test
  {
    public static void main(String[] args){
      compareArrays(new String[]{"Test1", "Test2", "Test3"},new String[]{"Test1", "Test2", "Test3"});
      compareArrays(new String[]{"Test1", "Test2", "Test3"},new String[]{"Test1", "Test2", "Test4"});
      compareArrays(new String[]{"Test1", "Test2", "Test3"},new String[]{"Test1", "Test2"});
      compareArrays(new String[]{"Test1", "Test2"},new String[]{"Test1", "Test2", "Test3"});
    }
    private static void compareArrays(String[] test,String[] test2){
      if(test.length > test2.length){
        System.out.println("Scenario 3");
      }
      else if(test2.length > test.length){
        System.out.println("Scenario 4");
      }
      else {
        boolean same = true;
        for (int a=0;a<test.length;a++){
          if(!test[a].equalsIgnoreCase(test2[a])){
            same = false;
          }
        }
        if (same){
          System.out.println("Scenario 1");
        }
        else {
          System.out.println("Scenario 2");
        }
      }
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM