Skip to content

Commit f4f8a7b

Browse files
committed
Today I Learned Gernerics about Java
1 parent 5cab9f1 commit f4f8a7b

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎generics/PointVectorEx.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package generics;
2+
3+
import java.util.Vector;
4+
5+
class Point {
6+
private int x, y;
7+
public Point(int x, int y){
8+
this.x = x;
9+
this.y = y;
10+
}
11+
public String toString() {
12+
return "(" + x + ", " + y + ")";
13+
}
14+
}
15+
public class PointVectorEx {
16+
public static void main(String[] args) {
17+
18+
Vector<Point> v = new Vector<Point>();
19+
20+
v.add(new Point(2, 3));
21+
v.add(new Point(-5, 20));
22+
v.add(new Point(30, -8));
23+
24+
v.remove(1);
25+
26+
for(int i = 0; i < v.size(); i++) {
27+
Point p = v.get(i);
28+
System.out.println(p);
29+
}
30+
}
31+
}

‎generics/VectorEx.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package generics;
2+
3+
import java.util.Vector;
4+
5+
public class VectorEx {
6+
public static void main(String[] args) {
7+
Vector<Integer> v = new Vector<Integer>();
8+
v.add(5);
9+
v.add(4);
10+
v.add(-1);
11+
12+
v.add(2, 100);
13+
14+
System.out.println("백터 내의 요소 객체 수 : " + v.size()); // 크기 3
15+
System.out.println("백터의 현재 용량 : " + v.capacity());
16+
17+
for(int i = 0; i<v.size(); i++) {
18+
int n = v.get(i);
19+
System.out.println(n);
20+
}
21+
22+
int sum = 0;
23+
for(int i = 0; i<v.size(); i++) {
24+
int n = v.elementAt(i);
25+
sum += n;
26+
}
27+
28+
System.out.println("벡터에 있는 정수 합 : " + sum);
29+
}
30+
}

0 commit comments

Comments
 (0)