-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphismEx5.java
More file actions
33 lines (24 loc) · 956 Bytes
/
Copy pathPolymorphismEx5.java
File metadata and controls
33 lines (24 loc) · 956 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
package com.study.polymorphism;
import java.util.*;
public class PolymorphismEx5 {
static int getSum(List<Integer> list) {
int sum = 0;
for(Integer i : list) sum = sum + i;
return sum;
}
static void addRandomValue(List<Integer> list, int count) {
Random random = new Random();
for(int i=0; i < count; ++i) list.add(random.nextInt(20));
}
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
LinkedList<Integer> list2 = new LinkedList<>();
Vector<Integer> list3 = new Vector<>();
addRandomValue(list1, 10);
addRandomValue(list2, 10);
addRandomValue(list3, 10);
System.out.printf("%s 합계: %d\n", list1.toString(), getSum(list1));
System.out.printf("%s 합계: %d\n", list2.toString(), getSum(list2));
System.out.printf("%s 합계: %d\n", list3.toString(), getSum(list3));
}
}