-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyList.java
More file actions
39 lines (29 loc) · 940 Bytes
/
MyList.java
File metadata and controls
39 lines (29 loc) · 940 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
package ArrayList;
import java.util.ArrayList;
import java.util.Collections;
//create generic MyList class with type parameter T
final class MyList<T extends Number>{
//create ArrayList of type T
ArrayList<T> list = new ArrayList<T>();
//create an add method that adds argument of type T to ArrayList
public void add( T new_element ){
list.add( new_element );
}
//create method 'largest' that returns largest value of 'list'
public T largest(){
Collections.sort( list );
//must return largest value after sorting
}
//create method 'smallest' that returns smallest value of 'list'
public T smallest(){
Collections.sort( list );
//must return smallest value after sorting
}
}
public final class GenericsHW{
public static void main(final String[] args) {
MyList<Number> list = new MyList<Number>();
list.add( new Integer( 10 ) );
list.add( new Double( 1.2 ) );
}
}