File tree Expand file tree Collapse file tree 2 files changed +92
-0
lines changed
Expand file tree Collapse file tree 2 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 1+ package sample ;
2+
3+ import java .util .ArrayList ;
4+
5+ // Using generics with wildcard
6+
7+ class Machine1 {
8+ @ Override
9+ public String toString () {
10+ return "This is a Machine." ;
11+ }
12+
13+ }
14+
15+ class Camera extends Machine1 {
16+ @ Override
17+ public String toString () {
18+ return "This is a Camera." ;
19+ }
20+ }
21+
22+ public class GenericsWildcard {
23+
24+ public static void showList (ArrayList <?> list ) {
25+ for (Object value : list ) {
26+ System .out .println (value );
27+ }
28+ }
29+
30+ public static void main (String [] args ) {
31+ ArrayList <Machine1 > list1 = new ArrayList <Machine1 >();
32+
33+ list1 .add (new Machine1 ());
34+ list1 .add (new Machine1 ());
35+
36+ ArrayList <Camera > list2 = new ArrayList <Camera >();
37+
38+ list2 .add (new Camera ());
39+ list2 .add (new Camera ());
40+
41+ showList (list1 );
42+ showList (list2 );
43+ }
44+
45+ }
Original file line number Diff line number Diff line change 1+ package sample ;
2+
3+ import java .util .ArrayList ;
4+
5+ public class UsingGenerics {
6+
7+ public static void showStringList (ArrayList <String > strings )
8+ {
9+ for (int i = 0 ; i < strings .size (); i ++) {
10+ System .out .println ("Animal name is: " + strings .get (i ));
11+ }
12+ }
13+
14+ public static void showDoubleList (ArrayList <Double > dbValue )
15+ {
16+ for (int i = 0 ; i < dbValue .size (); i ++) {
17+ System .out .println ("Double value is: " + dbValue .get (i ));
18+ }
19+ }
20+
21+ public static void main (String [] args ) {
22+ ArrayList <String > strings = new ArrayList <String >();
23+
24+ strings .add ("Cat" );
25+ strings .add ("Dogs" );
26+ strings .add ("Elephants" );
27+ strings .add ("Donkeys" );
28+
29+ showStringList (strings );
30+
31+ strings .add (2 , "Monkeys" );
32+ strings .add (4 ,"Hippos" );
33+
34+ System .out .println ("****************************" );
35+ showStringList (strings );
36+
37+ System .out .println ("****************************" );
38+ ArrayList <Double > dbValue = new ArrayList <Double >();
39+ dbValue .add (32.65 );
40+ dbValue .add (3243.54354 );
41+ dbValue .add (654.0154 );
42+ dbValue .add (983940.2132 );
43+
44+ showDoubleList (dbValue );
45+ }
46+
47+ }
You can’t perform that action at this time.
0 commit comments