4. Collections, List, and Java's Built-In APIs
Activity 1: Searching for Multiple Occurrences in an Array
Solution
- Create the
text
array.String[] text = {"So", "many", "books", "so", "little", "time"};
- Create the variable that contains the word to be searched for: so
String toSearch = "so";
- Initialize the variable occurrence to
-1
.int occurrences = -1;
- Create a
for
loop to iterate through the array to check for the occurrence.for(int i = 0; i < 5; i++) { if (text[i].compareToIgnoreCase(toSearch) == 0) { System.out.println("Found query at :" + i); occurrences++; ...