Method linearSearch

Method linearSearch (lines 10–18) performs the linear search. The method receives as parameters the array to search (data) and the searchKey. Lines 13–15 loop through the elements in the array data. Line 14 compares each with searchKey. If the values are equal, line 15 returns the index of the element. If there are duplicate values in the array, linear search returns the index of the first element in the array that matches the search key. If the loop ends without finding the value, line 17 returns -1.

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.