1. Trang chủ
  2. » Thể loại khác

Lớp Vector trong Java - 155 bài học Java miễn phí hay nhất PDF java vector class

8 261 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Lớp Vector trong Java

    • Ví dụ

Nội dung

http://vietjack.com/java/index.jsp Copyright © vietjack.com Lớp Vector Java Lớp Vector Java triển khai mảng động Nó tương tự ArrayList, với hai điểm khác biệt:  Vector đồng  Vector chứa phương thức legacy mà không phần Collection Framework Để hiểu sâu khái niệm trình bày chương này, mời bạn tham khảo loạt bài: Ví dụ Cấu trúc liệu (Data Structure) Java Lớp Vector Java chứng minh hữu ích bạn khơng biết kích cỡ mảng bạn cần thứ mà thay đổi kích cỡ thơng qua vòng đời chương trình Lớp Vector Java hỗ trợ constructor Form tạo vector mặc định, mà có kích cỡ khởi tạo 10: Vector( ) Form thứ hai tạo vector mà dung lượng khởi tạo xác định size: Vector(int size) Form thứ ba tạo vector mà dung lượng khởi tạo xác định size lượng gia xác định incr Lượng gia xác định số phần tử để cấp phát vector resize: Vector(int size, int incr) Form thứ tư tạo vector mà chứa phần tử collection c: Vector(Collection c) Ngoài phương thức kế thừa từ lớp cha, Vector định nghĩa phương thức sau: STT Phương thức Miêu tả void add(int index, Object element) http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com Chèn element xác định vị trí cho Vector boolean add(Object o) Phụ thêm phần tử cho vào cuối Vector boolean addAll(Collection c) Phụ thêm tất phần tử Collection xác định tới cuối Vector này, chúng trả Iterator Collection cho boolean addAll(int index, Collection c) Chèn tất phần tử Collection xác định vào Vector vị trí cho void addElement(Object obj) Thêm phần tử cuối Vector này, tăng kích cỡ thêm int capacity() Trả dung lượng Vector void clear() Gỡ bỏ tất phần tử từ Vector Object clone() Trả mô Vector boolean contains(Object elem) Kiểm tra đối tượng cho phần tử Vector 10 boolean containsAll(Collection c) Trả true Vector chứa tất phần tử Collection cho http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp 11 Copyright © vietjack.com void copyInto(Object[] anArray) Sao chép thành phần Vector vào mảng cho 12 Object elementAt(int index) Trả phần tử index cho 13 Enumeration elements() Trả liệt kê phần tử Vector 14 void ensureCapacity(int minCapacity) Tăng dung lượng Vector này, cần thiết, để bảo đảm giữ số phần tử xác định tham số minCapacity 15 boolean equals(Object o) So sánh Object cho với Vector cân 16 Object firstElement() Trả phần tử (tại mục 0) Vector 17 Object get(int index) Trả phần tử vị trí cho Vector 18 int hashCode() Trả giá trị hash code cho Vector 19 int indexOf(Object elem) Tìm kiếm xuất tham số cho, kiểm tra tính cân sử dụng phương thức equals http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp 20 Copyright © vietjack.com int indexOf(Object elem, int index) Tìm kiếm xuất tham số cho, bắt đầu tìm kiếm index, kiểm tra tính cân sử dụng phương thức equals 21 void insertElementAt(Object obj, int index) Chèn đối tượng cho phần tử vào Vector index cho 22 boolean isEmpty() Kiểm tra Vector phần tử 23 Object lastElement() Trả phần tử cuối Vector 24 int lastIndexOf(Object elem) Trả mục xuất cuối đối tượng cho Vector 25 int lastIndexOf(Object elem, int index) Tìm kiếm ngược sau cho đối tượng cho, index xác định, trả mục 26 Object remove(int index) Gỡ bỏ phần tử vị trí cho Vector 27 boolean remove(Object o) Gỡ bỏ xuất phần tử cho Vector Nếu Vector khơng chứa phần tử đó, không bị thay đổi 28 boolean removeAll(Collection c) Gỡ bỏ tất phần tử, mà chứa Collection cho, từ Vector http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp 29 Copyright © vietjack.com void removeAllElements() Gỡ bỏ tất phần tử từ Vector thiết lập kích cỡ 30 boolean removeElement(Object obj) Gỡ bỏ xuất (chỉ mục thấp nhất) tham số từ Vector 31 void removeElementAt(int index) removeElementAt(int index) 32 protected void removeRange(int fromIndex, int toIndex) Gỡ bỏ từ danh sách tất phần tử mà có index từ fromIndex tới toIndex 33 boolean retainAll(Collection c) Chỉ giữ lại phần tử, mà Collection cho, Vector 34 Object set(int index, Object element) Thay phần tử vị trí cho Vector với phần tử xác định 35 void setElementAt(Object obj, int index) Thiết lập phần tử index cho Vector thành đối tượng xác định 36 void setSize(int newSize) Thiết lập kích cỡ Vector 37 int size() Trả số phần tử Vector 38 List subList(int fromIndex, int toIndex) http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com Trả danh sách phụ từ fromIndex tới toIndex 39 Object[] toArray() Trả mảng chứa tất phần tử Vector theo thứ tự 40 Object[] toArray(Object[] a) Trả mảng chứa tất phần tử Vector theo thứ tự; kiểu runtime mảng trả mảng xác định 41 String toString() Trả biểu diễn chuỗi Vector này, chứa biểu diễn chuỗi phần tử 42 void trimToSize() Trim dung lượng Vector kích cỡ vector Ví dụ Chương trình sau minh họa số phương thức hỗ trợ lớp Vector Java: import java.util.*; public class VectorDemo { public static void main(String args[]) { // initial capacity is 3, increment is Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com v.addElement(new Integer(4)); System.out.println("Capacity after four additions: " + v.capacity()); v.addElement(new Double(5.45)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Double(6.08)); v.addElement(new Integer(7)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Float(9.4)); v.addElement(new Integer(10)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Integer(11)); v.addElement(new Integer(12)); System.out.println("First element: " + (Integer)v.firstElement()); System.out.println("Last element: " + (Integer)v.lastElement()); if(v.contains(new Integer(3))) System.out.println("Vector contains 3."); // enumerate the elements in the vector Enumeration vEnum = v.elements(); System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements()) System.out.print(vEnum.nextElement() + " "); System.out.println(); } } Nó cho kết sau: http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com Initial size: Initial capacity: Capacity after four additions: Current capacity: Current capacity: Current capacity: First element: Last element: 12 Vector contains Elements in vector: 5.45 6.08 9.4 10 11 12 http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... void trimToSize() Trim dung lượng Vector kích cỡ vector Ví dụ Chương trình sau minh họa số phương thức hỗ trợ lớp Vector Java: import java. util.*; public class VectorDemo { public static void main(String... cho, từ Vector http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com /java/ index.jsp 29 Copyright © vietjack.com void removeAllElements() Gỡ bỏ tất phần tử từ Vector thiết... lập kích cỡ Vector 37 int size() Trả số phần tử Vector 38 List subList(int fromIndex, int toIndex) http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com /java/ index.jsp

Ngày đăng: 03/12/2017, 00:19

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN