Các method trong bài này nằm ở 2 class java.util.Vector và java.util.Enumeration
Khai báo
Vector vt = new Vector();
Nhập dữ liệu cho một Vector (class Console nằm trong gói corejava)
Lưu ý là mỗi phần tử của Vector đều phải là một đối tượng, nên ta phải có new Integer(n) khi muốn đưa vào một biến kiểu int. Tương tự với Byte, Long, Float, ...
do { int n = Console.readInt(""); if(n!=0) vt.addElement(new Integer(n)); } while(n!=0);
In ra các phần tử của một Vector
for(int i=0;i<vt.size();i++) System.out.println(vt.elementAt(i));
Để đưa Vector về kiểu mảng cho dễ thao tác, ta đưa về kiểu Enumeration (một kiểu mảng)
Enumeration e = vt.elements();
Như vậy ta có mảng e kiểu Enumeration sao chép y khuôn Vector vt để dễ xử lí, không đụng đến Vector vt
In ra các phần tử của một Enumeration
while(e.hasMoreElements()) System.out.println(e.nextElement());
nguồn: http://forum.cuasotinhoc.vn
0 nhận xét