Fiveable
Fiveable
Fiveable
Fiveable

💻AP Computer Science A

7.5 Searching

1 min readLast Updated on June 18, 2024

A

Avanish Gupta

M

Milo Chang

A

Avanish Gupta

M

Milo Chang

One of the main applications of ArrayLists is to search, that is to see if an element is actually in an ArrayList and to return its index if it is. 

The simplest searching algorithm, and the only one we will learn in this unit, is linear/sequential search. This algorithm goes through the list element by element (thus the names sequential and linear) until the element is found. If the element is not found, -1 is returned. 

ArrayList Implementation

Here is its implementation for an ArrayList:

public static int linearSearch(ArrayList array, int n) { for (int i = 0; i < array.size(); i++) { if (array.get(i) == n) { return i; } } return -1; } 

Integer Array Implementation

Here is the implementation for a regular integer array:

public static int linearSearch(int[] array, int n) { for (int i = 0; i < array.length; i++) { if (array[i] == n) { return i; } } return -1; } 

There are other searching algorithms, but the only other important one to know is binary search covered in Unit 10.

Key Terms to Review (3)

ArrayList: ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.
Binary Search: Binary search is an efficient algorithm used to locate a target value within a sorted array by repeatedly dividing the search interval in half.
Linear/Sequential Search: Linear search is a simple searching algorithm that checks each element in a list one by one until it finds a match or reaches the end of the list. It is commonly used when there is no specific order or structure to the data.
ArrayList
See definition

ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Term 1 of 3

What is the significance of returning -1 in a linear search algorithm, and how does this relate to the algorithm's efficiency?

1 of 2

Key Terms to Review (3)

ArrayList
See definition

ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Term 1 of 3

ArrayList
See definition

ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Term 1 of 3

What is the significance of returning -1 in a linear search algorithm, and how does this relate to the algorithm's efficiency?

1 of 2


© 2025 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2025 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.