Verified for the 2025 AP Computer Science A exam•Citation:
Need to find something in your data? That's what searching is all about! In Java programming, we often need to locate specific items within an ArrayList or array. This guide covers how to search through data step-by-step, focusing on how programmers use loops (iteration) and if-statements (selection) to find what they're looking for.
Searching is just finding a specific item within a collection of data. Think of it like trying to find a particular book on a bookshelf. In Java, we search through arrays and ArrayLists to:
There are different ways to search, but for this unit, we focus on:
Algorithm Type | How It Works | Speed | When to Use |
---|---|---|---|
Sequential/Linear Search | Checks each item one by one until it finds what you're looking for | Slower for large collections | Works on any data, sorted or unsorted |
Binary Search | Divides the collection in half repeatedly to narrow down the search | Much faster for large collections | Only works on sorted data |
Sequential search (also called linear search) is the simplest way to search. It works like this:
public static int linearSearch(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (arr[i] == target) { return i; // Found it! Return the position } } return -1; // Didn't find it, return -1 }
public static int linearSearch(ArrayList<Integer> list, Integer target) { for (int i = 0; i < list.size(); i++) { if (list.get(i).equals(target)) { return i; // Found it! Return the position } } return -1; // Didn't find it, return -1 }
Tip: When searching for objects in an ArrayList, use .equals()
instead of ==
to compare them correctly. The ==
only checks if they're the exact same object in memory, while .equals()
checks if they have the same content.
Good news! Java's ArrayList already has methods that can search for you:
indexOf(Object o)
: Tells you the position of the first match (or -1 if not found)contains(Object o)
: Tells you if the item exists in the list (true or false)ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); // Using indexOf int position = names.indexOf("Bob"); // Returns 1 (the second position) // Using contains boolean hasAlice = names.contains("Alice"); // Returns true boolean hasDavid = names.contains("David"); // Returns false
When searching for your own custom objects (like Students or Products), you need to:
equals()
methodpublic class Student { private String name; private int id; // Constructor and other methods... @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Student student = (Student) obj; return id == student.id; // We're saying students are the same if they have the same ID } } // Now you can search for students ArrayList<Student> students = new ArrayList<>(); // ... add students to the list Student searchTarget = new Student("Unknown", 12345); boolean found = students.contains(searchTarget); // Will find any student with ID 12345
Here are typical things you might want to do:
public static ArrayList<Integer> findAllMatches(ArrayList<Integer> list, int target) { ArrayList<Integer> positions = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { if (list.get(i) == target) { positions.add(i); // Remember this position } } return positions; // Return all the positions where we found matches }
Sequential search has these characteristics:
Sequential search works well for:
==
with objects: Remember to use .equals()
when comparing objects<
with length
or size()
)Searching is like finding a needle in a haystack - you need a strategy! Sequential search gives us a simple way to find items in our arrays and ArrayLists by checking each element one by one. While not the fastest for huge datasets, it works for all types of data and is straightforward to understand. Mastering search techniques is essential for working with collections of data in your Java programs.