比较两个字符串数组而不知道哪个包含更多值

我必须比较 Java 中两个字符串数组的值并保存不同的字符串。我已经知道如何比较相同大小的字符串数组。但问题是,这些字符串数组的值计数一开始是未知的。因此,尚不清楚哪个 String Array 更大。


所以我必须处理以下场景:


场景一 (大小相同,没有区别):


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

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

场景2(大小相同,但不同):


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

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

场景 3(不同的大小 - 第一个字符串数组包含比第二个更多的值):


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

String[] test2 = {Test1, Test2}

场景 4(不同的大小 - 第二个字符串数组包含比第一个更多的值):


String[] test = {Test1, Test2}

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

实施场景一:


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


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


    } else {

        System.out.println("Scenario 1");

    }

}

实施场景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);

但是如果您不知道第一个字符串数组包含的元素比第二个多,或者第二个字符串数组包含的元素比第一个多,如何处理场景 3 和 4?


动漫人物
浏览 166回答 2
2回答

守着星空守着你

我已经实现了 compareArrays 方法来处理你的场景尝试像下面这样可能会解决你的问题&nbsp; public class Test&nbsp; {&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; compareArrays(new String[]{"Test1", "Test2", "Test3"},new String[]{"Test1", "Test2", "Test3"});&nbsp; &nbsp; &nbsp; compareArrays(new String[]{"Test1", "Test2", "Test3"},new String[]{"Test1", "Test2", "Test4"});&nbsp; &nbsp; &nbsp; compareArrays(new String[]{"Test1", "Test2", "Test3"},new String[]{"Test1", "Test2"});&nbsp; &nbsp; &nbsp; compareArrays(new String[]{"Test1", "Test2"},new String[]{"Test1", "Test2", "Test3"});&nbsp; &nbsp; }&nbsp; &nbsp; private static void compareArrays(String[] test,String[] test2){&nbsp; &nbsp; &nbsp; if(test.length > test2.length){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Scenario 3");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else if(test2.length > test.length){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Scenario 4");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; boolean same = true;&nbsp; &nbsp; &nbsp; &nbsp; for (int a=0;a<test.length;a++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!test[a].equalsIgnoreCase(test2[a])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; same = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (same){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Scenario 1");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Scenario 2");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }

守候你守候我

使用布尔数组来跟踪重复的字符串,只需检查一个数组的所有元素以查看另一个数组是否包含它。第二个数组的未使用字符串在第一个数组中也丢失,因此无论数组大小如何,您都可以将它们放在差异上。String[] array1;String[] array2;ArrayList<String> diff = compare_arrays(array1, array2);public ArrayList<String> compare_arrays(String[] a1, String[] a2){&nbsp; &nbsp; ArrayList<String> diff = new ArrayList<String>();&nbsp; &nbsp; boolean[] rep = new boolean[a2.length];&nbsp; &nbsp; Arrays.fill(a2, false);&nbsp; &nbsp; for(String str : a1){&nbsp; &nbsp; &nbsp; &nbsp; if(!Arrays.asList(a2).contains(str)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diff.add(str);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rep[Arrays.asList(a2).indexOf(str)] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for(int i = 0; i < a2.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; if(!rep[i]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diff.add(a2[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java