Method main

Method main (line 20–51) allows the user to search an array. Lines 25–28 create an array of 10 ints and populate it with random ints from 1099. Then, line 30 displays the array’s contents using Arrays static method toString, which returns a String representation of the array with the elements in square brackets ([and]) and separated by commas.

Lines 33–34 prompt the user for and store the search key. Line 39 calls method linearSearch to determine whether searchInt is in the array data. If it’s not, linearSearch returns -1 and the program notifies the user (lines 41–42). If searchInt is in the array, linearSearch returns the position of the element, which the program outputs in lines 44–45. Lines 48–49 get the next search key from ...

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.