Skip to content

Commit ef9713a

Browse files
author
Rocky Bhai
committed
Committing two additional files
1 parent 63fe38d commit ef9713a

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

0 commit comments

Comments
 (0)