...oo Verizon LTE 19% D 10:58 PM Done 7 of 7 O Quiz 16 Quiz: 16 x p cs-46A -s17
ID: 3812202 • Letter: #
Question
...oo Verizon LTE 19% D 10:58 PM Done 7 of 7 O Quiz 16 Quiz: 16 x p cs-46A -s17 (2 unread) x C Homework 8 x C Secure https://wileydcs.engagelms.com/learn/mod/quiz/attempt.php?attempt 338905 Quiz 16 ut of 5.00 Find the positions of all strings equal to a given string in an array list of strings. Adapt the algorithm from Section 77.Instead of ques on Complete the following file Array ListUtiljava 1 import javo.util. ArrayList 3 public class Array istUtil Finds the positions of all strings equal to a given string in an array list of strings. eparom words an array list of strings eparan searched ord the word to search for ereturn an array list of all matching positions 12 public static ArrayListExplanation / Answer
ArrayListUtil.java
import java.util.ArrayList;
public class ArrayListUtil
{
/**
Finds the positions of all strings equal to a given string
in an array list of strings.
@param words an array list of strings
@param searchedWord the word to search for
@return an array list of all matching positions
*/
public static ArrayList<Integer> findAll(ArrayList<String> words, String searchedWord)
{
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0; i< words.size();i++){
if(words.get(i).equalsIgnoreCase(searchedWord)){
list.add(i);
}
}
return list;
}
}
Tester.java
import java.util.ArrayList;
public class Tester
{
public static void main(String[] args)
{
ArrayList<String> words = new ArrayList<String>();
words.add("how");
words.add("much");
words.add("wood");
words.add("would");
words.add("a");
words.add("wood");
words.add("chuck");
words.add("chuck");
words.add("if");
words.add("a");
words.add("wood");
words.add("chuck");
words.add("could");
words.add("chuck");
words.add("wood");
System.out.println(ArrayListUtil.findAll(words, "wood"));
System.out.println("Expected: [2, 5, 10, 14]");
System.out.println(ArrayListUtil.findAll(words, "a"));
System.out.println("Expected: [4, 9]");
System.out.println(ArrayListUtil.findAll(words, "the"));
System.out.println("Expected: []");
}
}
Output:
[2, 5, 10, 14]
Expected: [2, 5, 10, 14]
[4, 9]
Expected: [4, 9]
[]
Expected: []
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.