Question: JAVA L4A2 Write a public static method named searchSecond which implements a modified version of the linear search algorithm on an ArrayList of String objects.
JAVA L4A2
Write a public static method named searchSecond which implements a modified version of the linear search algorithm on an ArrayList of String objects. Instead of returning the index of the first appearance of the target String in the ArrayList your method should return the index of the second appearance of this String. If the target String is not in the ArrayList or only appears once, your method should return -1.
Write your searchSecond method in the U7_L4_Activity_Two class, and ensure that the parameters for the method are an ArrayList of String objects followed by a String. Use the runner class to test your method but do not add a main method to your U7_L4_Activity_Two.java file or your code will not be scored correctly.
Sample Run 1:
Please enter words, enter STOP to stop the loop. apple bird cat apple apple STOP Enter String to search for. apple searchLast returns: 3
Sample Run 2:
Please enter words, enter STOP to stop the loop. apple bird apple cat apple STOP Enter String to search for. bird searchSecond returns: -1
Sample Run 3:
Please enter words, enter STOP to stop the loop. apple bird apple cat apple STOP Enter String to search for. dog searchSecond returns: -1
We have provided a couple of sample runs that can be used to test your code, but you do not need to include a Scanner and ask for user input when submitting your code for grading because the grader will test these inputs automatically.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
