FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaProgrammingHacktoberFest/Generics/WildcardExample.java at main · basukinath/JavaProgrammingHacktoberFest · GitHub
basukinath
/
JavaProgrammingHacktoberFest
Public
forked from
karterhhgg/JavaProgramming
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaProgrammingHacktoberFest
/
Generics
/
WildcardExample.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (38 loc) · 1.38 KB
Breadcrumbs
JavaProgrammingHacktoberFest
/
Generics
/
WildcardExample.java
Copy path
File metadata and controls
47 lines (38 loc) · 1.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
44
45
46
47
package
Generics
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
public
class
WildcardExample
{
// Upper-bounded wildcard: accepts a list of Number or its subclasses
public
static
double
sumOfList
(
List
<?
extends
Number
>
list
) {
double
sum
=
0.0
;
for
(
Number
n
:
list
) {
sum
+=
n
.
doubleValue
();
}
return
sum
;
}
// Unbounded wildcard: accepts a list of any type
public
static
void
printList
(
List
<?>
list
) {
for
(
Object
elem
:
list
) {
System
.
out
.
print
(
elem
+
" "
);
}
System
.
out
.
println
();
}
// Lower-bounded wildcard: accepts a list of Integer or its superclasses
public
static
void
addIntegers
(
List
<?
super
Integer
>
list
) {
list
.
add
(
10
);
list
.
add
(
20
);
}
public
static
void
main
(
String
[]
args
) {
List
<
Integer
>
intList
=
Arrays
.
asList
(
1
,
2
,
3
);
System
.
out
.
println
(
"Sum of integers: "
+
sumOfList
(
intList
));
List
<
Double
>
doubleList
=
Arrays
.
asList
(
1.1
,
2.2
,
3.3
);
System
.
out
.
println
(
"Sum of doubles: "
+
sumOfList
(
doubleList
));
System
.
out
.
print
(
"Integer list: "
);
printList
(
intList
);
System
.
out
.
print
(
"Double list: "
);
printList
(
doubleList
);
List
<
Number
>
numList
=
Arrays
.
asList
(
1
,
2.5
,
3L
);
addIntegers
(
numList
);
System
.
out
.
println
(
"List after adding integers: "
+
numList
);
}
}
Back
|
FazBrowse Home
|
New Git URL