Thứ Bảy, 21 tháng 4, 2012

Bài 15 - Vector (mảng không giới hạn số phần tử)


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
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 nhận xét

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© Download do an khoa luan tai lieu
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0
Posts RSSComments RSS
Back to top