-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion199_ArrayList_removeAll.java
More file actions
44 lines (29 loc) · 1.13 KB
/
Question199_ArrayList_removeAll.java
File metadata and controls
44 lines (29 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package week7_Array_2D_ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
public class Question199_ArrayList_removeAll {
public static void main(String[] args) {
// olcay // Jul 30, 2020
/* Create a static method that:
is called removeAll
returns nothing
takes two parameters: an ArrayList of Strings called wordList, and a String called targetWord
This method should go through every element of wordList and remove every instance of targetWord from the ArrayList.
Example:
ArrayList<String> wordList = new ArrayList<String>(Arrays.asList("el","hey","hop","yon"));
removeAll(wordList,"hid");
now wordList is ["hey","yo"]
*/
ArrayList<String> wordList = new ArrayList<String>(Arrays.asList("hi","hey","hi","yo","hire"));
String targetWord = "hey";
removeAll(wordList, targetWord);
}
public static void removeAll(ArrayList<String> wordList, String targetWord) {
for(int i=0; i<wordList.size(); i++) {
if(wordList.contains(targetWord)) {
wordList.remove(targetWord);
}
}
System.out.println(wordList.toString());
}
}