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

java arraylist class

5 175 0

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

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Lớp ArrayList trong Java

    • Ví dụ

Nội dung

http://vietjack.com/java/index.jsp Copyright © vietjack.com Lớp ArrayList Java Lớp ArrayList Java kế thừa AbstractList triển khai List Interface Lớp ArrayList hỗ trợ mảng động mà tăng kích cỡ cần Các mảng Java chuẩn có độ dài cố định Sau mảng tạo, chúng khơng thể tăng giảm kích cỡ, nghĩa bạn phải có phần tử mà mảng giữ Danh sách mảng tạo với kích cỡ ban đầu Khi kích cỡ bị vượt, collection tự động tăng Khi đối tượng bị gỡ bỏ, mảng bị giảm kích cỡ Để 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ụ Collection Java Lớp ArrayList hỗ trợ constructor Constructor xây dựng danh sách mảng trống ArrayList( ) Constructor thứ hai xây dựng Array List mà khởi tạo với phần tử collection c ArrayList(Collection c) Constructor xây dựng Array List mà có dung lượng ban đầu xác định Dung lượng kích cỡ mảng mà sử dụng để lưu phần tử Dung lượng tự động tăng phần tử thêm vào Array List ArrayList(int capacity) Ngoài phương thức kế thừa từ lớp cha, lớp ArrayList định nghĩa số phương thức sau: STT Phương thức Miêu tả void add(int index, Object element) Chèn phần tử cho index xác định list Ném IndexOutOfBoundsException index bên dãy (index < || index > http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com size()) boolean add(Object o) Phụ thêm phần tử xác định tới cuối list boolean addAll(Collection c) Phụ thêm tất phần tử collection xác định tới cuối list này, thứ tự mà chúng trả Iterator collection cho Ném NullPointerException collection cho null boolean addAll(int index, Collection c) Chèn tất phần tử collection cho vào list này, bắt đầu vị trí cho Ném NullPointerException collection cho null void clear() Gỡ bỏ tất phần tử từ list Object clone() Trả shallow copy ArrayList boolean contains(Object o) Trả true list chứa phần tử cho Chính thức hơn, trả true list chứa phần tử e (o==null ? e==null : o.equals(e)) void ensureCapacity(int minCapacity) Tăng dung lượng ArrayList này, cần thiết, để bảo đảm giữ tối thiểu số phần tử xác định tham số minCapacity Object get(int index) Trả phần tử index cho Ném IndexOutOfBoundsException index cho http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com bên ngồi dãy (index < || index >= size()) 10 int indexOf(Object o) Trả index list xuất phần tử cho, -1 List không chứa phần tử 11 int lastIndexOf(Object o) Trả index list xuất cuối phần tử cho, -1 List không chứa phần tử 12 Object remove(int index) Gỡ bỏ phần tử index cho Ném IndexOutOfBoundsException index dãy (index < || index >= size()) 13 protected void removeRange(int fromIndex, int toIndex) Gỡ bỏ từ list tất phần tử mà có index fromIndex toIndex 14 Object set(int index, Object element) Thay phần tử vị trí cho list với phần tử xác định Ném IndexOutOfBoundsException index dãy (index < || index >= size()) 15 int size() Trả số phần tử list 16 Object[] toArray() Trả mảng chứa tất phần tử list thứ tự xác Ném NullPointerException mảng null 17 Object[] toArray(Object[] a) Trả mảng chứa tất phần tử list thứ tự xác; kiểu http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com runtime mảng trả mảng xác định 18 void trimToSize() Trim dung lượng ArrayList kích cỡ list Ví dụ Chương trình sau minh họa phương thức hỗ trợ lớp ArrayList Java: import java.util.*; public class ArrayListDemo { public static void main(String args[]) { // create an array list ArrayList al = new ArrayList(); System.out.println("Initial size of al: " + al.size()); // add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); al.add(1, "A2"); System.out.println("Size of al after additions: " + al.size()); // display the array list System.out.println("Contents of al: " + al); // Remove elements from the array list al.remove("F"); al.remove(2); System.out.println("Size of al after deletions: " + al.size()); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com System.out.println("Contents of al: " + al); } } Nó cho kết sau: Initial size of al: Size of al after additions: Contents of al: [C, A2, A, E, B, D, F] Size of al after deletions: Contents of al: [C, A2, E, B, D] http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... thức hỗ trợ lớp ArrayList Java: import java. util.*; public class ArrayListDemo { public static void main(String args[]) { // create an array list ArrayList al = new ArrayList( ); System.out.println("Initial... http://vietjack.com /java/ index.jsp Copyright © vietjack.com runtime mảng trả mảng xác định 18 void trimToSize() Trim dung lượng ArrayList kích cỡ list Ví dụ Chương trình sau minh họa phương thức hỗ trợ lớp ArrayList. .. phần tử e (o==null ? e==null : o.equals(e)) void ensureCapacity(int minCapacity) Tăng dung lượng ArrayList này, cần thiết, để bảo đảm giữ tối thiểu số phần tử xác định tham số minCapacity Object

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

TỪ KHÓA LIÊN QUAN

w