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ề lớp SortedList 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: 177606" data-attributes="member: 165510"><p><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"><span style="font-size: 18px">Lớp SortedList</span></a><span style="font-size: 18px"> trong C# sử dụng một key cũng như một chỉ mục để truy cập các item trong một list.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Một danh sách đã được sắp xếp là sự tổ hợp của một mảng và một Hash Table. Nó chứa một danh sách các item mà có thể được truy cập bởi sử dụng một key hoặc một chỉ mục. Nếu bạn truy cập item bởi sử dụng một chỉ mục, nó là một ArrayList, và nếu bạn truy cập item bởi sử dụng key, nó là một HashTable. Tập hợp các item luôn luôn được sắp xếp bởi giá trị key.</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 SortedList trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>* Dưới đây liệt kê các thuộc tính được sử dụng phổ biến của lớp SortedList trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>1. Capacity</em>:</strong> Lấy hoặc thiết lập dung lượng của SortedList</span></p><p><span style="font-size: 18px"><em><strong>2. Count</strong>:</em> Lấy số phần tử được chứa trong SortedList</span></p><p><span style="font-size: 18px"><em><strong>3. IsFixedSize</strong>:</em> Lấy một giá trị chỉ dẫn có hay không SortedList là có kích cỡ cố định</span></p><p><span style="font-size: 18px"><em><strong>4. IsReadOnly</strong>:</em> Lấy một giá trị chỉ dẫn có hay không SortedList là read-only</span></p><p><span style="font-size: 18px"><em><strong>5. Item</strong></em>: Lấy và thiết lập giá trị được liên kết với một key cụ thể trong SortedList</span></p><p><span style="font-size: 18px"><em><strong>6. Keys</strong>:</em> Lấy các key trong SortedList</span></p><p><span style="font-size: 18px"><strong><em>7. Values:</em></strong> Lấy các value trong SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>* Dưới đây liệt kê các phương thức được </strong></span><a href="https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/" target="_blank"><span style="font-size: 18px"><strong>sử dụng phổ biến </strong></span></a><span style="font-size: 18px"><strong>của lớp SortedList trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>1. public virtual void Add(object key, object value);</strong></em></span></p><p><span style="font-size: 18px">Thêm một phần tử với key và value đã xác định vào trong SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>2. public virtual void Clear();</em></strong></span></p><p><span style="font-size: 18px">Gỡ bỏ tất cả phần tử từ SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>3. public virtual bool ContainsKey(object key);</strong></em></span></p><p><span style="font-size: 18px">Xác định có hay không SortedList là chứa một key cụ thể</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>4. public virtual bool ContainsValue(object value);</em></strong></span></p><p><span style="font-size: 18px">Xác định có hay không SortedList là chứa một value cụ thể</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>5. public virtual object GetByIndex(int index);</strong></em></span></p><p><span style="font-size: 18px">Lấy value tại chỉ mục đã xác định của SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>6. public virtual object GetKey(int index);</strong></em></span></p><p><span style="font-size: 18px">Lấy key tại chỉ mục đã xác định của SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>7. public virtual IList GetKeyList();</strong></em></span></p><p><span style="font-size: 18px">Lấy các key trong SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>8. public virtual IList GetValueList();</em></strong></span></p><p><span style="font-size: 18px">Lấy các value trong SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>9. public virtual int IndexOfKey(object key);</strong></em></span></p><p><span style="font-size: 18px">Trả về chỉ mục (dựa trên cơ sở 0) của key đã xác định trong SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>10. public virtual int IndexOfValue(object value);</strong></em></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 </span><a href="https://vnkienthuc.com/threads/tim-hieu-ve-phuong-thuc-nac-danh-trong-c.77386/" target="_blank"><span style="font-size: 18px">value </span></a><span style="font-size: 18px">đã xác định trong SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>11. public virtual void Remove(object key);</em></strong></span></p><p><span style="font-size: 18px">Gỡ bỏ phần tử với key đã xác định từ SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>12. public virtual void RemoveAt(int index);</strong></em></span></p><p><span style="font-size: 18px">Gỡ bỏ phần tử tại chỉ mục đã xác định của SortedList</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>13. public virtual void TrimToSize();</em></strong></span></p><p><span style="font-size: 18px">Thiết lập capacity tới số phần tử thực sự trong SortedList</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à </span><a href="https://vnkienthuc.com/threads/tim-hieu-ve-lop-bitarray-trong-c.77384/" target="_blank"><span style="font-size: 18px">phương thức của lớp SortedList trong C#:</span></a></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 SortedList 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"><em><span style="color: #5900b3"> SortedList ht = new SortedList();</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("001", "Tran Nam");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("002", "Minh Chinh");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("003", "Thu Phuong");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("004", "Hong Ngoc");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("005", "Lam Truong");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("006", "Thuy Hang");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("007", "Minh Hieu");</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> if (ht.ContainsValue("Phuong Thanh"))</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("Sinh vien co ten tren da ton tai trong danh sach");</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"> else</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"> ht.Add("008", "Phuong Thanh");</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"> // lay tap hop cac key</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ICollection key = ht.Keys;</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (string k in key)</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(k + ": " + ht[k]);</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.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/sortedlist.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p><p></p><p style="text-align: right"><span style="font-size: 18px">Nguồn: vietjack.com</span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177606, member: 165510"] [URL='https://vnkienthuc.com/forums/code.377/'][SIZE=5]Lớp SortedList[/SIZE][/URL][SIZE=5] trong C# sử dụng một key cũng như một chỉ mục để truy cập các item trong một list. Một danh sách đã được sắp xếp là sự tổ hợp của một mảng và một Hash Table. Nó chứa một danh sách các item mà có thể được truy cập bởi sử dụng một key hoặc một chỉ mục. Nếu bạn truy cập item bởi sử dụng một chỉ mục, nó là một ArrayList, và nếu bạn truy cập item bởi sử dụng key, nó là một HashTable. Tập hợp các item luôn luôn được sắp xếp bởi giá trị key. [COLOR=#0000ff][B]Phương thức và Thuộc tính của lớp SortedList trong C#[/B][/COLOR] [B]* Dưới đây liệt kê các thuộc tính được sử dụng phổ biến của lớp SortedList trong C#:[/B] [B][I]1. Capacity[/I]:[/B] Lấy hoặc thiết lập dung lượng của SortedList [I][B]2. Count[/B]:[/I] Lấy số phần tử được chứa trong SortedList [I][B]3. IsFixedSize[/B]:[/I] Lấy một giá trị chỉ dẫn có hay không SortedList là có kích cỡ cố định [I][B]4. IsReadOnly[/B]:[/I] Lấy một giá trị chỉ dẫn có hay không SortedList là read-only [I][B]5. Item[/B][/I]: Lấy và thiết lập giá trị được liên kết với một key cụ thể trong SortedList [I][B]6. Keys[/B]:[/I] Lấy các key trong SortedList [B][I]7. Values:[/I][/B] Lấy các value trong SortedList [B]* Dưới đây liệt kê các phương thức được [/B][/SIZE][URL='https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/'][SIZE=5][B]sử dụng phổ biến [/B][/SIZE][/URL][SIZE=5][B]của lớp SortedList trong C#:[/B] [I][B]1. public virtual void Add(object key, object value);[/B][/I] Thêm một phần tử với key và value đã xác định vào trong SortedList [B][I]2. public virtual void Clear();[/I][/B] Gỡ bỏ tất cả phần tử từ SortedList [I][B]3. public virtual bool ContainsKey(object key);[/B][/I] Xác định có hay không SortedList là chứa một key cụ thể [B][I]4. public virtual bool ContainsValue(object value);[/I][/B] Xác định có hay không SortedList là chứa một value cụ thể [I][B]5. public virtual object GetByIndex(int index);[/B][/I] Lấy value tại chỉ mục đã xác định của SortedList [I][B]6. public virtual object GetKey(int index);[/B][/I] Lấy key tại chỉ mục đã xác định của SortedList [I][B]7. public virtual IList GetKeyList();[/B][/I] Lấy các key trong SortedList [B][I]8. public virtual IList GetValueList();[/I][/B] Lấy các value trong SortedList [I][B]9. public virtual int IndexOfKey(object key);[/B][/I] Trả về chỉ mục (dựa trên cơ sở 0) của key đã xác định trong SortedList [I][B]10. public virtual int IndexOfValue(object value);[/B][/I] 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 [/SIZE][URL='https://vnkienthuc.com/threads/tim-hieu-ve-phuong-thuc-nac-danh-trong-c.77386/'][SIZE=5]value [/SIZE][/URL][SIZE=5]đã xác định trong SortedList [B][I]11. public virtual void Remove(object key);[/I][/B] Gỡ bỏ phần tử với key đã xác định từ SortedList [I][B]12. public virtual void RemoveAt(int index);[/B][/I] Gỡ bỏ phần tử tại chỉ mục đã xác định của SortedList [B][I]13. public virtual void TrimToSize();[/I][/B] Thiết lập capacity tới số phần tử thực sự trong SortedList [B]Ví dụ[/B] Dưới đây là ví dụ minh họa các thuộc tính và [/SIZE][URL='https://vnkienthuc.com/threads/tim-hieu-ve-lop-bitarray-trong-c.77384/'][SIZE=5]phương thức của lớp SortedList trong C#:[/SIZE][/URL] [SIZE=5] [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 SortedList trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("-------------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] SortedList ht = new SortedList();[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("001", "Tran Nam");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("002", "Minh Chinh");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("003", "Thu Phuong");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("004", "Hong Ngoc");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("005", "Lam Truong");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("006", "Thuy Hang");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("007", "Minh Hieu");[/COLOR][/I] [I][COLOR=#5900b3] if (ht.ContainsValue("Phuong Thanh"))[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Sinh vien co ten tren da ton tai trong danh sach");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] else[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("008", "Phuong Thanh");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] // lay tap hop cac key[/COLOR][/I] [I][COLOR=#5900b3] ICollection key = ht.Keys;[/COLOR][/I] [I][COLOR=#5900b3] foreach (string k in key)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine(k + ": " + ht[k]);[/COLOR][/I] [I][COLOR=#5900b3] }[/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/sortedlist.PNG[/IMG][/SIZE][/CENTER] [SIZE=5][/SIZE] [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ề lớp SortedList trong C#
Top