Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về ArrayList trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="uocmo_kchodoi" data-source="post: 177604" data-attributes="member: 165510"><p><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"><span style="font-size: 18px">Lớp List - ArrayList</span></a><span style="font-size: 18px"> trong C# biểu diễn một tập hợp được sắp xếp của một đối tượng mà có thể được lập chỉ mục cho từng</span><a href="https://vnkienthuc.com/forums/cong-nghe-thong-tin.375/" target="_blank"><span style="font-size: 18px"> item</span></a><span style="font-size: 18px"> riêng rẽ.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Về cơ bản, nó là một sự thay thế cho một mảng. Tuy nhiên, không giống như trong mảng, bạn có thể thêm và gỡ bỏ các item từ một list tại một vị trí đã xác định bởi sử dụng một chỉ mụcvà mảng chính nó có thể tự điều chỉnh kích cỡ một cách tự động. Nó cũng cho phép cấp phát bộ nhớ động, thêm, tìm kiếm và</span><a href="https://vnkienthuc.com/forums/web-development.379/" target="_blank"><span style="font-size: 18px"> sắp xếp các item</span></a><span style="font-size: 18px"> trong một list.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>Phương thức và Thuộc tính của lớp ArrayList trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>-Dưới đây là danh sách các thuộc tính được sử dụng phổ biến của lớp ArrayList trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">1. Capacity: </span>Lấy hoặc thiết lập số phần tử mà</span><a href="https://vnkienthuc.com/threads/tim-hieu-ve-lop-queue-trong-c.77383/" target="_blank"><span style="font-size: 18px"> ArrayList</span></a><span style="font-size: 18px"> có thể chứa</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">2. Count</span>: Lấy số phần tử thực sự được chứa trong ArrayList</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">3. IsFixedSize: </span>Lấy một giá trị chỉ rằng có hay không ArrayList là có một kích cỡ cố định</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">4. IsReadOnly</span>: Lấy một giá trị chỉ rằng có hay không ArrayList là read-only</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">5. Item</span>: Lấy hoặc thiết lập phần tử tại chỉ mục đã xác định</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>- Dưới đây là các phương thức được sử dụng phổ biến của lớp ArrayList trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">1 public virtual int Add(object value);</span></span></p><p><span style="font-size: 18px">Thêm một đối tượng vào phần cuối của ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">2 public virtual void AddRange(ICollection c);</span></span></p><p><span style="font-size: 18px">Thêm các phần tử của một ICollection vào phần cuối của ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">3 public virtual void Clear();</span></span></p><p><span style="font-size: 18px">Gỡ bỏ các phần tử từ ArrayList đó</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">4 public virtual bool Contains(object item);</span></span></p><p><span style="font-size: 18px">Xác định có hay không một phần tử là nằm trong ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">5 public virtual ArrayList GetRange(int index, int count);</span></span></p><p><span style="font-size: 18px">Trả về một ArrayList mà biểu diễn một tập con của các phần tử trong ArrayList nguồn</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">6 public virtual int IndexOf(object);</span></span></p><p><span style="font-size: 18px">Trả về chỉ mục (dựa trên cơ sở 0) của lần xuất hiện đầu tiên của một giá trị trong ArrayList hoặc một phần của nó</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">7 public virtual void Insert(int index, object value);</span></span></p><p><span style="font-size: 18px">Chèn một phần tử vào ArrayList tại chỉ mục đã xác định</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">8 public virtual void InsertRange(int index, ICollection c);</span></span></p><p><span style="font-size: 18px">Chèn một phần tử của một collection vào ArrayList tại chỉ mục đã xác định</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">9 public virtual void Remove(object obj);</span></span></p><p><span style="font-size: 18px">Gỡ bỏ lần xuất hiện đầu tiên của một đối tượng đã xác định tại chỉ mục đã xác định</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">10 public virtual void RemoveAt(int index);</span></span></p><p><span style="font-size: 18px">Gỡ bỏ phần tử tại chỉ mục đã xác định của ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">11 public virtual void RemoveRange(int index, int count);</span></span></p><p><span style="font-size: 18px">Gỡ bỏ một dãy phần tử từ ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">12 public virtual void Reverse();</span></span></p><p><span style="font-size: 18px">Đảo ngược thứ tự phần tử trong ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">13 public virtual void SetRange(int index, ICollection c);</span></span></p><p><span style="font-size: 18px">Sao chép các phần tử của một collection qua một dãy các phần tử trong ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">14 public virtual void Sort();</span></span></p><p><span style="font-size: 18px">Sắp xếp các phần tử trong ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">15 public virtual void TrimToSize();</span></span></p><p><span style="font-size: 18px">Thiết lập capacity tới số phần tử thực sự trong ArrayList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>Ví dụ</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Dưới đây là ví dụ minh họa các thuộc tính và phương thức của lớp ArrayList trong C#:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System.Collections;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa ArrayList trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("---------------------------------");</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ArrayList al = new ArrayList();</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("\nChen mot vai phan tu vao ArrayList: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Add(45);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Add(78);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Add(33);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Add(56);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Add(12);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Add(23);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Add(9);</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("\nDung luong cua ArrayList: {0} ", al.Capacity);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("So phan tu trong ArrayList: {0}", al.Count);</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("\nHien thi cac phan tu trong ArrayList: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (int i in al)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.Write(i + " ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("\nSap xep va hien thi ArrayList sau khi da sap xep: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> al.Sort();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (int i in al)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.Write(i + " ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.ReadKey();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px">Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="font-size: 18px"></span></p><p style="text-align: center"><span style="font-size: 18px"><img src="https://vietjack.com/csharp/images/arraylist.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: right"><span style="font-size: 18px">Nguồn: vietjack.com</span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177604, member: 165510"] [B][SIZE=5] [/SIZE][/B][URL='https://vnkienthuc.com/forums/code.377/'][SIZE=5]Lớp List - ArrayList[/SIZE][/URL][SIZE=5] trong C# biểu diễn một tập hợp được sắp xếp của một đối tượng mà có thể được lập chỉ mục cho từng[/SIZE][URL='https://vnkienthuc.com/forums/cong-nghe-thong-tin.375/'][SIZE=5] item[/SIZE][/URL][SIZE=5] riêng rẽ. Về cơ bản, nó là một sự thay thế cho một mảng. Tuy nhiên, không giống như trong mảng, bạn có thể thêm và gỡ bỏ các item từ một list tại một vị trí đã xác định bởi sử dụng một chỉ mụcvà mảng chính nó có thể tự điều chỉnh kích cỡ một cách tự động. Nó cũng cho phép cấp phát bộ nhớ động, thêm, tìm kiếm và[/SIZE][URL='https://vnkienthuc.com/forums/web-development.379/'][SIZE=5] sắp xếp các item[/SIZE][/URL][SIZE=5] trong một list. [COLOR=#0000ff][B]Phương thức và Thuộc tính của lớp ArrayList trong C#[/B][/COLOR] [B]-Dưới đây là danh sách các thuộc tính được sử dụng phổ biến của lớp ArrayList trong C#:[/B] [COLOR=#ff8000]1. Capacity: [/COLOR]Lấy hoặc thiết lập số phần tử mà[/SIZE][URL='https://vnkienthuc.com/threads/tim-hieu-ve-lop-queue-trong-c.77383/'][SIZE=5] ArrayList[/SIZE][/URL][SIZE=5] có thể chứa [COLOR=#ff8000]2. Count[/COLOR]: Lấy số phần tử thực sự được chứa trong ArrayList [COLOR=#ff8000]3. IsFixedSize: [/COLOR]Lấy một giá trị chỉ rằng có hay không ArrayList là có một kích cỡ cố định [COLOR=#ff8000]4. IsReadOnly[/COLOR]: Lấy một giá trị chỉ rằng có hay không ArrayList là read-only [COLOR=#ff8000]5. Item[/COLOR]: Lấy hoặc thiết lập phần tử tại chỉ mục đã xác định [B]- Dưới đây là các phương thức được sử dụng phổ biến của lớp ArrayList trong C#:[/B] [COLOR=#ff8000]1 public virtual int Add(object value);[/COLOR] Thêm một đối tượng vào phần cuối của ArrayList [COLOR=#ff8000]2 public virtual void AddRange(ICollection c);[/COLOR] Thêm các phần tử của một ICollection vào phần cuối của ArrayList [COLOR=#ff8000]3 public virtual void Clear();[/COLOR] Gỡ bỏ các phần tử từ ArrayList đó [COLOR=#ff8000]4 public virtual bool Contains(object item);[/COLOR] Xác định có hay không một phần tử là nằm trong ArrayList [COLOR=#ff8000]5 public virtual ArrayList GetRange(int index, int count);[/COLOR] Trả về một ArrayList mà biểu diễn một tập con của các phần tử trong ArrayList nguồn [COLOR=#ff8000]6 public virtual int IndexOf(object);[/COLOR] Trả về chỉ mục (dựa trên cơ sở 0) của lần xuất hiện đầu tiên của một giá trị trong ArrayList hoặc một phần của nó [COLOR=#ff8000]7 public virtual void Insert(int index, object value);[/COLOR] Chèn một phần tử vào ArrayList tại chỉ mục đã xác định [COLOR=#ff8000]8 public virtual void InsertRange(int index, ICollection c);[/COLOR] Chèn một phần tử của một collection vào ArrayList tại chỉ mục đã xác định [COLOR=#ff8000]9 public virtual void Remove(object obj);[/COLOR] Gỡ bỏ lần xuất hiện đầu tiên của một đối tượng đã xác định tại chỉ mục đã xác định [COLOR=#ff8000]10 public virtual void RemoveAt(int index);[/COLOR] Gỡ bỏ phần tử tại chỉ mục đã xác định của ArrayList [COLOR=#ff8000]11 public virtual void RemoveRange(int index, int count);[/COLOR] Gỡ bỏ một dãy phần tử từ ArrayList [COLOR=#ff8000]12 public virtual void Reverse();[/COLOR] Đảo ngược thứ tự phần tử trong ArrayList [COLOR=#ff8000]13 public virtual void SetRange(int index, ICollection c);[/COLOR] Sao chép các phần tử của một collection qua một dãy các phần tử trong ArrayList [COLOR=#ff8000]14 public virtual void Sort();[/COLOR] Sắp xếp các phần tử trong ArrayList [COLOR=#ff8000]15 public virtual void TrimToSize();[/COLOR] Thiết lập capacity tới số phần tử thực sự trong ArrayList [B]Ví dụ[/B] Dưới đây là ví dụ minh họa các thuộc tính và phương thức của lớp ArrayList trong C#: [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]using System.Collections;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestCsharp[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] static void Main(string[] args)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Vi du minh hoa ArrayList trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("---------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] ArrayList al = new ArrayList();[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("\nChen mot vai phan tu vao ArrayList: ");[/COLOR][/I] [I][COLOR=#5900b3] al.Add(45);[/COLOR][/I] [I][COLOR=#5900b3] al.Add(78);[/COLOR][/I] [I][COLOR=#5900b3] al.Add(33);[/COLOR][/I] [I][COLOR=#5900b3] al.Add(56);[/COLOR][/I] [I][COLOR=#5900b3] al.Add(12);[/COLOR][/I] [I][COLOR=#5900b3] al.Add(23);[/COLOR][/I] [I][COLOR=#5900b3] al.Add(9);[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("\nDung luong cua ArrayList: {0} ", al.Capacity);[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("So phan tu trong ArrayList: {0}", al.Count);[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("\nHien thi cac phan tu trong ArrayList: ");[/COLOR][/I] [I][COLOR=#5900b3] foreach (int i in al)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.Write(i + " ");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine();[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("\nSap xep va hien thi ArrayList sau khi da sap xep: ");[/COLOR][/I] [I][COLOR=#5900b3] al.Sort();[/COLOR][/I] [I][COLOR=#5900b3] foreach (int i in al)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.Write(i + " ");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine();[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [/SIZE] [CENTER][SIZE=5][IMG]https://vietjack.com/csharp/images/arraylist.PNG[/IMG] [/SIZE][/CENTER] [RIGHT][SIZE=5]Nguồn: vietjack.com[/SIZE][/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về ArrayList trong C#
Top