178k views
4 votes
Write a method called listSearch() that takes in a target string and a list of other strings. This method returns a (possibly shorter) list containing all of the strings from the original list that themselves contain the target string you are searching for. Check for the target string as a case-sensitive substring of every member of the list. You can either modify the provided list or create a new one.

1 Answer

5 votes

Answer:

public static List<String> listSearch(String searchFor, List<String> list){

List<String> foundList = new ArrayList<String>();

for(String s:list){

if(s.contains(searchFor)){

foundList.add(s);

}

}

return foundList;

}

Step-by-step explanation:

Create a method named listSearch that takes two parameters, a String named searchFor and a String list named list

Inside the method, initialize a new String list named foundList, this will be used to hold the strings that contains the target string. Create a for loop that iterates through the list. Check if a string in the list contains the searchFor using the contains method. If it does, add the string to the foundList. When the loop is done, return the foundList.

User Yakubu
by
4.3k points