Vector ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
import java.util.Vector;
public class VectorEx {
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>(4); //์ฌ์ด์ฆ ๋ฏธ์
๋ ฅ ์ ๊ธฐ๋ณธ ์ฌ์ด์ฆ 10
v.add(10);
v.add(new Integer(20));
v.add(30);
v.add(40);
v.add(50);
v.add(0,5); //0๋ฒ ์์น์ 5 ์ฝ์
v.remove(0); //0๋ฒ ์์น ์ญ์
for(int i = 0 ; i<v.size(); i++) //v.size(): ๊ธธ์ด
System.out.print(v.get(i)+" "); //v.get(): ๊ดํธ ์์ ์ซ์์ ํด๋นํ๋ ๊ฐ ๊ฐ์ ธ์ด
System.out.println();
System.out.println("size: "+v.size()+", capacity: "+v.capacity());
Vector<String> v2 = new Vector<String>();
String[] str = {"python", "java", "database"};
for(String s:str)
v2.add(s);
for(int i = 0; i<v2.size(); i++) {
System.out.print(v2.get(i)+"|");
}
}
}
Iterator it = v2.iterator(); //์ํํ๋ ๊ฑฐ
while(it.hasNext())
System.out.print(it.next()+" ");
-
import java.util.Vector;
class Point{
private int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString() { //๊ฐ์ฒด๊ฐ ๊ฐ์ง ์ ๋ณด๋ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ง๋ค์ด ๋ฆฌํด
return "("+x+"<"+y+")";
}
}
public class PointVector {
static <E> void print(Vector<E> v) { //generic
for(int i = 0 ; i<v.size(); i++)
System.out.println(v.get(i));
}
public static void main(String[] args) {
Vector<Point> v = new Vector<Point>();
Vector<Integer> iv = new Vector<Integer>(4);
Vector<String> sv = new Vector<String>();
v.add(new Point(1,2));
v.add(new Point(2,3));
v.add(new Point(3,4));
String[] str = {"python", "java", "database"};
for(String s:str)
sv.add(s);
for(int i=1; i<=5; i++)
iv.add(i);
print(v);
print(sv);
print(iv);
}
}
-
728x90
๋ฐ์ํ
0