-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTest.java
More file actions
43 lines (30 loc) · 855 Bytes
/
Test.java
File metadata and controls
43 lines (30 loc) · 855 Bytes
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
import java.util.ArrayList;
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<String>();
list.add("cat");
list.add("cats");
list.add("dog");
list.add("dogs");
System.out.println(list);
removedEvenLength(list);
System.out.println("Linked list after...");
System.out.println(list);
}
// remove all string of even length from a list
// we use array list // linked
//not efficient call get and remove many times
public static void removedEvenLength(LinkedList<String> list){ //ARRAY AND LINKED
int i =0;
while(i < list.size()){
String element = list.get(i);
if(element.length()%2 == 0){//even
list.remove(i);
}
else{
i++; // skip to the next element
}
}
}
}