中查找元素" />
本文介绍了如何在 Java 中使用线性搜索算法比较两个字符串类型的 ArrayList,以判断一个列表(例如购物清单)中的所有元素是否都存在于另一个列表(例如食品储藏室清单)中。我们将探讨如何通过循环遍历和条件判断来实现此功能,并提供使用 HashSet 优化搜索效率的替代方案。
线性搜索是一种简单的搜索算法,它通过逐个比较列表中的元素与目标值来查找目标值。在我们的场景中,目标是检查 input 列表中的每个元素是否存在于 pantry 列表中。
以下是一个改进后的 linearSearch 方法的示例:
import java.util.ArrayList; import java.util.Scanner; import java.util.HashSet; import java.util.Set; public class TheList { public static String linearSearch(ArrayList<String> pantry, ArrayList<String> input) { for (String ingredient : input) { boolean found = false; for (String pantryItem : pantry) { if (ingredient.equals(pantryItem)) { found = true; break; // 如果找到,跳出内层循环 } } if (!found) { return "You still need " + ingredient + "!"; } } return "You got everything you need!"; } public static void main(String[] args) { // 创建食品储藏室列表 ArrayList<String> pantry = new ArrayList<>(); pantry.add("Bread"); pantry.add("Peanut Butter"); pantry.add("Chips"); pantry.add("Jelly"); // 创建用户输入列表 ArrayList<String> input = inputIngredients(); // 执行搜索并打印结果 System.out.println(linearSearch(pantry, input)); } private static ArrayList<String> inputIngredients() { Scanner ingredientScan = new Scanner(System.in); ArrayList<String> ingredients = new ArrayList<>(); System.out.println("Enter ingredients (type 'done' to finish):"); String ingredient; while (true) { System.out.print("Ingredient: "); ingredient = ingredientScan.nextLine(); if (ingredient.equalsIgnoreCase("done")) { break; } ingredients.add(ingredient); System.out.println(ingredient + " added."); } ingredientScan.close(); return ingredients; } }
代码解释:
运行示例:
Enter ingredients (type 'done' to finish): Ingredient: Bread Bread added. Ingredient: Peanut Butter Peanut Butter added. Ingredient: done You got everything you need!
或者
Enter ingredients (type 'done' to finish): Ingredient: Bread Bread added. Ingredient: Milk Milk added. Ingredient: done You still need Milk!
对于大型列表,线性搜索的效率较低,时间复杂度为 O(n*m),其中 n 和 m 分别是两个列表的长度。可以使用 HashSet 来优化搜索过程。HashSet 允许以接近恒定的时间复杂度 O(1) 查找元素。
以下是使用 HashSet 优化的代码示例:
import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class TheList { public static String search(Set<String> pantry, ArrayList<String> ingredients) { for (String ingredient : ingredients) { if (!pantry.contains(ingredient)) { return "You still need " + ingredient + "!"; } } return "Everything is available"; } public static void main(String[] args) { // 创建食品储藏室集合 Set<String> pantry = new HashSet<>(); pantry.add("Bread"); pantry.add("Peanut Butter"); pantry.add("Chips"); pantry.add("Jelly"); // 创建用户输入列表 ArrayList<String> ingredients = inputIngredients(); // 执行搜索并打印结果 System.out.println(search(pantry, ingredients)); } private static ArrayList<String> inputIngredients() { Scanner ingredientScan = new Scanner(System.in); ArrayList<String> ingredients = new ArrayList<>(); System.out.println("Enter ingredients (type 'done' to finish):"); String ingredient; while (true) { System.out.print("Ingredient: "); ingredient = ingredientScan.nextLine(); if (ingredient.equalsIgnoreCase("done")) { break; } ingredients.add(ingredient); System.out.println(ingredient + " added."); } ingredientScan.close(); return ingredients; } }
代码解释:
注意事项:
本文介绍了如何使用线性搜索算法和 HashSet 在 Java 中比较两个字符串类型的 ArrayList。线性搜索算法简单易懂,但效率较低。HashSet 可以显著提高搜索效率,特别是在处理大型列表时。选择哪种方法取决于具体的应用场景和性能要求。
以上就是使用线性搜索在两个 ArrayList<String> 中查找元素的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号