比较两个String []数组并打印出不同的字符串

时间:2016-03-21 03:34:58

标签: java arrays

我已经获得了一个文件夹中的所有文件名列表以及一个已经手动"检查过的文件列表"由开发人员。我将如何比较两个数组,以便我们只打印出那些未包含在主列表中的数组。

public static void main(String[] args) throws java.lang.Exception {
        String[] list = {"my_purchases", "my_reservation_history", "my_reservations", "my_sales", "my_wallet", "notifications", "order_confirmation", "payment", "payment_methods", "pricing", "privacy", "privacy_policy", "profile_menu", "ratings", "register", "reviews", "search_listings", "search_listings_forms", "submit_listing", "submit_listing_forms", "terms_of_service", "transaction_history", "trust_verification", "unsubscribe", "user", "verify_email", "verify_shipping", "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu", "main_searchbar", "primary_navbar"};
        String[] checked = {"404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu"};

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

            for(int i = 0; i < checked.length; i++)
                {
                    if(!Arrays.asList(list).contains(checked[i]))
                    ar.add(checked[i]);
                }
    }

4 个答案:

答案 0 :(得分:6)

将您的循环更改为:

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

for(int i = 0; i < checked.length; i++) {
      if(!Arrays.asList(list).contains(checked[i]))
      ar.add(checked[i]);
}

ArrayList ar应位于 for 循环之外。否则每当ar中存在checked数组元素时,都会创建list

修改

if(!Arrays.asList(list).contains(checked))

使用此语句,您将检查checked引用是否不是list的元素。应该checked[i]检查checkedlist的元素是否存在。

如果您要在list中打印不在checked中的元素。然后使用:

for(int i = 0; i < list.length; i++) {
      if(!Arrays.asList(checked).contains(list[i]))
      ar.add(list[i]);
}
System.out.println(ar);

答案 1 :(得分:2)

您的更新解决方案对我来说似乎有点奇怪,不知道为什么要将list [i]添加到结果列表中。通常,这听起来像是为以下内容制作的哈希集:

String[] list = { "my_purchases", "my_reservation_history","my_reservations","my_sales", "my_wallet", "notifications", "order_confirmation", "payment", "payment_methods", "pricing", "privacy", "privacy_policy", "profile_menu", "ratings", "register", "reviews", "search_listings", "search_listings_forms", "submit_listing", "submit_listing_forms", "terms_of_service", "transaction_history", "trust_verification", "unsubscribe", "user", "verify_email", "verify_shipping", "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu", "main_searchbar", "primary_navbar"};
String[] checked = { "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu"};

HashSet<String> s1 = new HashSet<String>(Arrays.asList(checked));
s1.removeAll(Arrays.asList(list));
System.out.println(s1);

答案 2 :(得分:1)

for (String s: checked) {      // go through all in second list
    if (! list.contains(s)) {  // if string not in master list
        System.out.println(s); // print that string
    }
}

答案 3 :(得分:1)

首先,我认为您的代码有一些错误:

  • s1未定义
  • 未定义
  • 您的意思是使用Arrays.toString而不是Array.toString

所以我修复了你的代码(使用Java 8),它应该是这样的:

public static void main(String[] args) throws java.lang.Exception {
    String[] list = {"my_purchases", "my_reservation_history", "my_reservations", "my_sales", "my_wallet", "notifications", "order_confirmation", "payment", "payment_methods", "pricing", "privacy", "privacy_policy", "profile_menu", "ratings", "register", "reviews", "search_listings", "search_listings_forms", "submit_listing", "submit_listing_forms", "terms_of_service", "transaction_history", "trust_verification", "unsubscribe", "user", "verify_email", "verify_shipping", "404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu", "main_searchbar", "primary_navbar"};
    String[] checked = {"404", "account_menu", "auth", "base", "dashboard_base", "dashboard_menu", "fiveohthree", "footer", "header", "header_menu", "listings_menu"};

    final List<String> result = Stream.of(list)
            .filter(listEntry -> Stream.of(checked)
                    .filter(checkedEntry -> checkedEntry.equals(listEntry)).findFirst().orElse(null) == null)
            .collect(Collectors.toList());

    System.out.println(result);
}

如果您不想使用Java 8,则必须替换Streams和过滤器的使用并使用Java 7中的相应函数进行收集(例如,参见Satya的帖子)。

无论如何,我应该提到有更好的(关于性能)实现来解决你的问题,例如

  • 您可以在搜索重复项之前对列表进行排序,
  • 你可以使用,例如,基于散列的实现来提高搜索重复项的速度,
  • 你可以将代码移到内循环之外,
  • 以及更多