Skip to content

Commit 60bdbea

Browse files
committed
Chapter 19 Ex: 4 and 5
1 parent 132bf31 commit 60bdbea

File tree

4 files changed

+92
-32
lines changed

4 files changed

+92
-32
lines changed

ch_19/Exercise19_03.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ch_19;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
/**
7+
* 19.3 (Distinct elements in ArrayList) Write the following method that returns a new
8+
* ArrayList.
9+
* <p>
10+
* The new list contains the non-duplicate elements from the original list. <br>
11+
* {@code public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) }
12+
*/
13+
public class Exercise19_03 {
14+
15+
public static void main(String[] args) {
16+
//Test the method
17+
ArrayList<String> testList = new ArrayList<>(Arrays.asList("party", "Tomorrow", "Friday", "cereal", "14", "real",
18+
"pizza", "party", "Friday", "Thursday", "weekends", "14", "party"));
19+
System.out.println(removeDuplicates(testList));
20+
}
21+
22+
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
23+
if (list.size() == 0 || list.size() == 1) {
24+
return list;
25+
}
26+
ArrayList<E> filteredList = new ArrayList<>();
27+
for (int i = 0; i < list.size(); i++) {
28+
if (!filteredList.contains(list.get(i))) {
29+
filteredList.add(list.get(i));
30+
}
31+
}
32+
return filteredList;
33+
}
34+
}

ch_19/Exercise19_04.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ch_19;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 19.4 (Generic linear search) Implement the following generic method for linear search.
7+
* <p>
8+
* {@code public static <E extends Comparable<E>>int linearSearch(E[] list, E key) }
9+
*/
10+
public class Exercise19_04 {
11+
public static void main(String[] args) {
12+
Double searchKey = 100.8;
13+
Double[] values = new Double[]{100.1, 100.3, 100.0, 100.8, 100.6};
14+
System.out.println("Generic LinearSearch returned index: " + linearSearch(values,
15+
searchKey) + ". When search key is " + searchKey + " and an Array of Double values is: \n" + Arrays.toString(values));
16+
}
17+
18+
19+
public static <E extends Comparable<E>> int linearSearch(E[] list, E key) {
20+
for (int i = 0; i < list.length; i++) {
21+
if (list[i].compareTo(key) == 0) {
22+
return i;
23+
}
24+
25+
}
26+
return -1;
27+
28+
}
29+
}

ch_19/Exercise19_05.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ch_19;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 19.5 (Maximum element in an array) Implement the following method that returns the
7+
* maximum element in an array.
8+
* <p>
9+
* {@code public static <E extends Comparable<E>> E max(E[] list) }
10+
*/
11+
public class Exercise19_05 {
12+
public static void main(String[] args) {
13+
//Test the method
14+
Double[] values = new Double[]{4400.1, 1203.3, 3100.0, 18700.8, 4100.6};
15+
System.out.println("Generic Max method returned: " + max(values) + ". When passed this Array of values: " +
16+
" \n" + Arrays.toString(values));
17+
}
18+
19+
public static <E extends Comparable<E>> E max(E[] list) {
20+
E maxValue = list[0];
21+
for (int i = 1; i < list.length; i++) {
22+
if (list[i].compareTo(maxValue) > 0) {
23+
maxValue = list[i];
24+
}
25+
}
26+
return maxValue;
27+
}
28+
29+
}

ch_19/GenericStack.java

-32
This file was deleted.

0 commit comments

Comments
 (0)