c collections arraylist
Kolekcje C # to wyspecjalizowane klasy do przechowywania i modyfikowania danych. W tym samouczku dowiesz się o kolekcjach C #, takich jak ArrayList, HashTable i SortedList z przykładami:
Służą różnym celom, takim jak dynamiczna alokacja pamięci, ocena danych niesekwencyjnych itp.
Wszystkie te klasy używają klasy obiektów, która jest klasą bazową dla wszystkich innych typów danych.
=> Obejrzyj całą serię szkoleń C # tutaj
Czego się nauczysz:
wstawianie i usuwanie drzew binarnych w java
Kolekcje C # i ich użycie
C # ArrayList
ArrayList jest częścią kolekcji w języku C #. Służy do przechowywania danych dowolnego typu. Jest podobny do tablicy w C #, ale nie ma określonego rozmiaru. Jego rozmiar zwiększa się automatycznie w miarę dodawania kolejnych elementów.
Jak zainicjować ArrayList?
ArrayList można zainicjować za pomocą słowa kluczowego „ArrayList”.
ArrayList arrList = new ArrayList();
Jak dodać elementy do tablicy ArrayList?
Możesz dodać pojedynczy element do listy tablic przy użyciu metody Add () oraz zakresu elementów lub wielu elementów z innej kolekcji przy użyciu metody AddRange ().
Przykład:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); Console.ReadLine(); } } }
Tutaj widać, że użyliśmy metody ArrayList.Add (). Jak widać, dodaliśmy zarówno liczby całkowite, jak i łańcuchy do tej samej listy tablic. Jest to możliwe, ponieważ lista tablic ma nieogólny typ danych, tj. Może zawierać element dowolnego typu danych.
Jak uzyskać dostęp do elementu z ArrayList?
Dostęp do elementu listy tablicy można uzyskać podobnie jak w tablicy, tj. Za pomocą indeksu. Ale ponieważ nie jest to typ ogólny, musimy najpierw rzutować go na odpowiedni typ danych lub użyć słowa kluczowego var podczas uzyskiwania dostępu do wartości z listy tablic.
Przykład:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); Console.ReadLine(); } } }
W powyższym przykładzie pobraliśmy dane za pomocą indeksu listy tablic, a następnie przerzuciliśmy je na odpowiedni typ danych. Wyniki zostały następnie wydrukowane na konsoli jako dane wyjściowe.
Wynik dla powyższego programu będzie wyglądał następująco:
Wynik
Pierwsza wartość indeksu to: 7896
Druga wartość indeksu to: siedem
Jak wstawić element do tablicy ArrayList?
Aby wstawić element do ArrayList w określonym punkcie lub indeksie. używana jest metoda Insert ().
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); arrList.Insert(1, 'Eight'); second_index = (string)arrList(1); Console.WriteLine('The second index value is : ' + second_index); Console.ReadLine(); } } }
Dlatego wstawiliśmy nowy ciąg o indeksie 1 za pomocą metody insert (). Jeśli więc uruchomimy powyższy program, otrzymamy następujący wynik:
Wynik
Pierwsza wartość indeksu to: 7896
Druga wartość indeksu to: siedem
Druga wartość indeksu to: Osiem
Jak usunąć element z ArrayList?
Element można usunąć z tablicy ArrayList za pomocą metody Remove (). Metoda Remove przyjmuje parametr, czyli wartość, którą należy usunąć z tablicy.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); arrList.Insert(1, 'Eight'); second_index = (string)arrList(1); Console.WriteLine('The second index value is : ' + second_index); arrList.Remove(7896); var data = arrList(0); Console.WriteLine('The value at 0 index is : ' + data); Console.ReadLine(); } } }
Metoda remove usuwa daną wartość z listy. Gdy wartość jest usuwana z danego indeksu, kolejna wartość przesuwa się o jeden indeks w górę, aby wypełnić lukę. Ponieważ usuwamy 0 indeksów, wartość z indeksu 1 przesunie się i wypełni pustkę na 0.
Wynikiem tego programu będzie:
Wynik
Pierwsza wartość indeksu to: 7896
Druga wartość indeksu to: siedem
Druga wartość indeksu to: Osiem
Wartość przy indeksie 0 to: Osiem
Jak usunąć element listy za pomocą indeksu?
Użytkownik może usunąć element listy z określonego indeksu przy użyciu metody RemoveAt (). RemoveAt () akceptuje pojedynczy parametr o numerze indeksu, z którego element ma zostać usunięty. Podobnie jak w przypadku metody Remove, usunięcie elementu ze środka spowoduje przesunięcie kolejnego elementu o jeden stopień w górę, aby wypełnić lukę.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7896); arrList.Add('Seven'); //casted and retrieved data int first_index = (int)arrList(0); string second_index = (string)arrList(1); Console.WriteLine('The first index value is : ' + first_index); Console.WriteLine('The second index value is : ' + second_index); arrList.Insert(1, 'Eight'); second_index = (string)arrList(1); Console.WriteLine('The second index value is : ' + second_index); arrList.RemoveAt(1); var data = arrList(1); Console.WriteLine('The value at 1 index is : ' + data); Console.ReadLine(); } } }
W powyższym przykładzie użyliśmy RemoveAt do usunięcia indeksu 1. W związku z tym element o indeksie 2 powinien przejść do indeksu 1, aby zastąpić lukę.
Wynikiem tego programu będzie:
Wynik
Pierwsza wartość indeksu to: 7896
Druga wartość indeksu to: siedem
Druga wartość indeksu to: Osiem
Wartość przy 1 indeksie to: Siedem
Jak posortować i odwrócić ArrayList?
ArrayList oferuje dwie różne metody sortowania i operacji odwrotnych. Jest tylko jeden warunek, tj. Wszystkie elementy wewnątrz listy tablic powinny mieć ten sam typ danych do porównania z komparatorem, w przeciwnym razie wystąpi błąd w czasie wykonywania.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace ConsoleApp1 { class Program { static void Main(string() args) { ArrayList arrList = new ArrayList(); arrList.Add(7); arrList.Add(4); arrList.Add(5); arrList.Add(1); arrList.Add(3); Console.WriteLine('Original Array List'); foreach (var v in arrList) { Console.Write(v + ' '); } //sorting an array list Console.WriteLine(); Console.WriteLine('Sorted Array List'); arrList.Sort(); foreach (var srt in arrList) { Console.Write(srt + ' '); } //Reversing an array list Console.WriteLine(); Console.WriteLine('Reversed Array List'); arrList.Reverse(); foreach (var rvrs in arrList) { Console.Write(rvrs + ' '); } Console.ReadLine(); } } }
W powyższym programie najpierw utworzyliśmy listę tablic z danymi typu integer, a następnie użyliśmy metody sort do sortowania listy i metody odwrotnej do odwrócenia posortowanej listy.
Tak więc wynik poniższej listy będzie:
dobre miejsce do oglądania anime online za darmo
Wynik
Oryginalna lista tablic
7 4 5 1 3
Posortowana lista tablic
1 3 4 5 7
Lista odwróconych tablic
7 5 4 3 1
C # HashTable
Przestrzeń nazw System.Collections w języku C # zawiera Hashtable, który jest dość podobny do Dictionary. Hashtable przechowuje dane w postaci par klucz-wartość.
Robi to wewnętrznie, przypisując kod skrótu do klucza skrótu wewnętrznie, a za każdym razem, gdy uzyskuje się dostęp do danych, dopasowuje kod skrótu z kluczem skrótu w celu pobrania danych. Każdy element w tabeli będzie miał parę klucz-wartość
Jak zainicjować HashTable?
HashTable można zainicjować za pomocą słowa kluczowego HashTable, a następnie utworzyć jego wystąpienie.
Hashtable hashtbl = new Hashtable();
Jak dodać elementy do tablicy HashTable?
Elementy można dodawać do HashTable za pomocą metody Add (). Pozwala użytkownikom dodać element wraz z jego kluczem i wartością. Opisywanie typu danych klucza lub wartości nie jest ważne. Podczas dodawania elementów do HashTable należy pamiętać, że klucze nie mogą zawierać wartości null, której wartości mogą być null.
HashTable.Add (klucze, wartości);
Przykład:
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); } }
Jak widać, dodaliśmy różne typy danych jako klucze i wartości.
Jak uzyskać dostęp do elementów obecnych w tabeli z haszowaniem?
Wartość dowolnego klucza można pobrać z tablicy Hashtable za pomocą indeksatora. Ale indeksator wymaga również klucza, aby uzyskać dostęp i pobrać wartość z tabeli.
Dodajmy mały fragment kodu do powyższego programu, aby pobrać wartości:
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); Console.ReadLine(); } }
Teraz, jeśli to wykonamy, zostanie wygenerowany następujący wynik:
jeden
ferrari
osiem
W powyższym programie rzutowaliśmy wartości wszystkich kluczy dla danego typu danych, aby usunąć ewentualny błąd kompilacji. Dzieje się tak, ponieważ jako zbiór nieogólny Hashtable nie zawiera informacji o typie danych żadnej z jego zawartości, tj. Kluczy i wartości.
W związku z tym, jeśli spróbujemy bezpośrednio pobrać kompilator danych, będziemy zdezorientowani co do typu danych i wyrzucimy błąd.
Jak usunąć element z Hashtable?
Powiedzmy, że chcemy usunąć określoną parę klucz-wartość z tablicy z haszowaniem. Aby to osiągnąć, musimy skorzystać z metody Remove () oferowanej przez kolekcje. Metoda Remove trwale usuwa daną parę klucz-wartość z tablicy z haszowaniem.
Usuń (klucz);
Dodajmy metodę Remove do powyższego programu, aby uzyskać pomysł:
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); //now we remove a key hashtbl.Remove(8); //Lets try to find the remove key now String valueRemoved = (string)hashtbl(8); Console.WriteLine('The value of the given key is :'+valueRemoved); Console.ReadLine(); } }
Wynik powyższego programu będzie następujący.
jeden
Ferrari
osiem
Wartość podanego klucza to:
Nie, wartość jest wyświetlana na konsoli, ponieważ usunęliśmy klucz z tablicy z haszowaniem. W związku z tym wartość ciągu z valueRemoved ma wartość null.
Jeśli chcesz usunąć wszystko z tablicy hashy, C # udostępnia nam inną metodę o nazwie Clear (). Metoda Remove usuwa jedną parę klucz-wartość naraz, podczas gdy metoda Clear usuwa wszystko z tablicy mieszania.
Spójrzmy na poniższy program, aby to zrobić:
class Program { static void Main(string() args) { Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number',1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); //now we remove a key hashtbl.Remove(8); //Lets try to find the remove key now String valueRemoved = (string)hashtbl(8); Console.WriteLine('The value of the given key is :'+valueRemoved); // clearing all data from the HashTable hashtbl.Clear(); Console.ReadLine(); } }
Powyższy program usunie wszystkie elementy z tablicy hashy i uczyni ją pustą.
Inne ważne metody oferowane przez Hashtable to ContainsKey () i ContainsValue (). Obie te metody akceptują jeden argument, który jest kluczem lub wartością i zwracają wartość logiczną. Tak więc, jeśli parametr przekazany tą metodą jest obecny w tablicy hashy, zwróci on wartość true, a jeśli jej nie ma, zostanie zwrócony false.
C # SortedList
Jak sama nazwa wskazuje, SortedList zawiera posortowane dane w kolejności rosnącej. Jest podobny do Hashtable, ponieważ zawiera podobną parę klucz-wartość. Wszystkie klucze wstawione lub dodane do SortedList są automatycznie porządkowane w kolejności rosnącej.
Jak zainicjować posortowaną listę?
SortedList można zainicjować za pomocą słowa kluczowego SortedList i utworzyć dla niego instancję obiektu.
SortedList sortedList = new SortedList();
Obiekt może następnie służyć do wykonywania operacji przy użyciu właściwości i metod SortedList.
Jak dodać element do posortowanej listy?
Możesz dodać element do SortedList za pomocą metody Add (). SortedList wymaga dodania klucza i wartości. Możesz dodawać klucze i wartości w dowolnej kolejności, a posortowana lista uporządkuje wszystkie dodane elementy w kolejności rosnącej.
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); Console.ReadLine(); } }
W powyższym programie widać, że dodaliśmy liczbę całkowitą jako klucz, a następnie ciąg znaków jako wartości. Możesz dodać dowolny typ danych, jak chcesz i w dowolnej kolejności. SortedList uporządkuje je w kolejności rosnącej.
Podobnie jak w HashTable, klucze nie mogą mieć wartości null, a wszystkie klucze powinny mieć ten sam typ danych do porównania, w przeciwnym razie zostanie zgłoszony błąd kompilacji.
narzędzie do naprawy błędów dla systemu Windows 10
Lista Posortowane sortuje element za każdym razem, gdy je dodajesz. Tak więc, nawet jeśli dodasz jakiś element po zakończeniu sortowania, ponownie posortuje i doda element do odpowiedniego indeksu.
Jak uzyskać dostęp do elementów z posortowanej listy?
Dostęp do wartości na posortowanej liście można uzyskać za pomocą klucza.
Dodajmy prosty kod, aby pobrać wartość z SortedList opisanej w poprzednim przykładzie:
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); string i = (string)sortedList(1); string j = (string)sortedList(5); string k = (string)sortedList(3); Console.WriteLine(i); Console.WriteLine(j); Console.WriteLine(k); Console.ReadLine(); } }
Wynik powyższego fragmentu kodu będzie wyglądał następująco:
Jedno drzewo
Pięć mango
Trzy cytryny
W powyższym fragmencie kodu musimy rzutować wartości wszystkich kluczy dla danego typu danych, aby usunąć wszelkie błędy kompilacji, które mogą wystąpić przy typie danych wartości. Ma to na celu zapewnienie, że nie zostanie zgłoszony błąd kompilacji, nawet jeśli niektóre klucze zawierają różne typy danych.
Jak sprawdzić, czy podany klucz znajduje się na posortowanej liście?
Istnieją dwie wbudowane metody, tj. Zawiera () i ContainsKey () które pomagają nam określić, czy określony klucz istnieje wewnątrz sortowanej listy. ConstainsValue () to kolejna metoda używana do określenia, czy dana wartość jest obecna w kolekcji, czy nie.
Rzućmy okiem na prosty program, aby dowiedzieć się więcej o tych metodach.
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); //boolean value for key 5 bool key = sortedList.ContainsKey(5); //boolean value for vlaue 'One Tree' bool val = sortedList.ContainsValue('One Tree'); //Boolean value for unavailable key bool unKey = sortedList.ContainsKey(25); //Boolean value for unavailable value bool unVal = sortedList.ContainsValue('some randome value'); Console.WriteLine('The sorted list contains 5 key :' + key); Console.WriteLine('The sorted list contains One Tree value :' + val); Console.WriteLine('The sorted list contains 25 key :' +unKey); Console.WriteLine('The sorted list contains some random value:' + unVal); Console.ReadLine(); } }
Wynik powyższego programu będzie:
Posortowana lista zawiera 5 kluczy: True
Posortowana lista zawiera wartość One Tree: True
Posortowana lista zawiera 25 kluczy: Fałsz
Posortowana lista zawiera losową wartość: Fałsz
W powyższym programie można wyraźnie zobaczyć, że jeśli wartość lub klucz znajduje się w sortowanej liście, to zwracana jest wartość prawda, a jeśli jej nie ma, zwracana jest wartość fałszywa.
Jak usunąć element z sortowanej listy?
Posortowana lista oferuje metody Remove () i RemoveAt () umożliwiające usunięcie dowolnego elementu znajdującego się na posortowanej liście. Remove akceptuje pojedynczy argument z nazwą klucza, a RemoveAt akceptuje również pojedynczy argument, ale z indeksem.
Obie te metody usuwają dowolny element obecny na liście Sorted na podstawie argumentu.
Rzućmy okiem na prosty kod, aby lepiej go zrozumieć.
class Program { static void Main(string() args) { SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); //The Remove() method accepts key as argument and removes both the key and the value sortedList.Remove(1); //Now we will check if the key is present bool rmvKey = sortedList.ContainsKey(1); Console.WriteLine('The presence if the key is: ' + rmvKey); //The RemoveAt() method acceots index as argument and remove any key and value present at that index sortedList.RemoveAt(3); Console.ReadLine(); } }
Wynik powyższego programu będzie:
Obecność, jeśli klucz to: False
W powyższym programie najpierw użyliśmy metody Remove do usunięcia pary klucz-wartość za pomocą klucza. Spowoduje to usunięcie wszystkich par klucz-wartość pasujących do klucza podanego w argumencie. Następnie użyliśmy metody ContainsKey, aby sprawdzić, czy usunięty klucz nie istnieje już na liście Sorted.
W kolejnym wierszu użyliśmy metody RemoveAt, która usuwa elementy za pomocą indeksu. Tak więc, jak omówiliśmy wcześniej, gdy dany element zostanie usunięty z indeksu, inny element przesunie się w górę, aby zmienić kolejność i zachować posortowaną strukturę listy.
Wniosek
Kolekcje to dedykowane klasy obecne w języku programowania C # do przechowywania i obsługi danych. Służą one do wykonywania określonych czynności, np. Tworzenia dynamicznych list, odwracania, sortowania itp. Na zadanym zbiorze danych.
W tym samouczku dowiedzieliśmy się o tablicy ArrayList, która w niektórych aspektach jest podobna do tablicy, ale nie ma wstępnie zdefiniowanego rozmiaru. Dowiedzieliśmy się również o tablicy HashTable, która przechowuje dane w parach klucz-wartość. Za pomocą klucza można pobrać dowolną wartość.
Dowiedzieliśmy się również o posortowanej liście, która jest podobna do tablicy HashTable, ale automatycznie sortuje wszystkie dane znajdujące się w niej, w kolejności rosnącej na podstawie kluczy.
Dane wewnątrz listy sortowanej są zawsze w kolejności rosnącej, tj. Nawet jeśli usuniesz określony element ze środka lub dodasz nowy element do listy sortowanej, wszystkie dane zostaną automatycznie uporządkowane w kolejności rosnącej.
Przykładowy kod
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SeleniumFrameWork.FrameworkEssentials; using SeleniumFrameWork.FrameWorkSupportModules; namespace ConsoleApp1 { class Program { static void Main(string() args) { /* Array List Code */ ArrayList arrList = new ArrayList(); arrList.Add(7); arrList.Add(4); arrList.Add(5); arrList.Add(1); arrList.Add(3); Console.WriteLine('Original Array List'); foreach (var v in arrList) { Console.Write(v + ' '); } //sorting an array list Console.WriteLine(); Console.WriteLine('Sorted Array List'); arrList.Sort(); foreach (var srt in arrList) { Console.Write(srt + ' '); } //Reversing an array list Console.WriteLine(); Console.WriteLine('Reversed Array List'); arrList.Reverse(); foreach (var rvrs in arrList) { Console.Write(rvrs + ' '); } /* HashTable Code */ Hashtable hashtbl = new Hashtable(); hashtbl.Add('Number', 1); hashtbl.Add('Car', 'Ferrari'); hashtbl.Add(8, 'eight'); int value1 = (int)hashtbl('Number'); String value2 = (string)hashtbl('Car'); String value3 = (string)hashtbl(8); Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); //now we remove a key hashtbl.Remove(8); //Lets try to find the remove key now String valueRemoved = (string)hashtbl(8); Console.WriteLine('The value of the given key is :' + valueRemoved); // clearing all data from the HashTable hashtbl.Clear(); /* Sorted List Code */ SortedList sortedList = new SortedList(); sortedList.Add(2, 'Two Pineapples'); sortedList.Add(4, 'Four Apples'); sortedList.Add(3, 'Three Lemons'); sortedList.Add(5, 'Five Mangoes'); sortedList.Add(1, 'One Tree'); string i = (string)sortedList(1); string j = (string)sortedList(5); string k = (string)sortedList(3); Console.WriteLine(i); Console.WriteLine(j); Console.WriteLine(k); //boolean value for key 5 bool key = sortedList.ContainsKey(5); //boolean value for vlaue 'One Tree' bool val = sortedList.ContainsValue('One Tree'); //Boolean value for unavailable key bool unKey = sortedList.ContainsKey(25); //Boolean value for unavailable value bool unVal = sortedList.ContainsValue('some randome value'); Console.WriteLine('The sorted list contains 5 key :' + key); Console.WriteLine('The sorted list contains One Tree value :' + val); Console.WriteLine('The sorted list contains 25 key :' +unKey); Console.WriteLine('The sorted list contains some randome value:' + unVal); //The Remove() method accepts key as argument and removes both the key and the value sortedList.Remove(1); //Now we will check if the key is present bool rmvKey = sortedList.ContainsKey(1); Console.WriteLine('The presence if the key is: ' + rmvKey); //The RemoveAt() method acceots index as argument and remove any key and value present at that index sortedList.RemoveAt(3); Console.ReadLine(); } } }
=> Poszukaj łatwego przewodnika szkoleniowego języka C # tutaj
rekomendowane lektury
- Python DateTime Tutorial z przykładami
- Polecenie Cut w systemie Unix z przykładami
- Składnia poleceń Unix Cat, opcje z przykładami
- Wykorzystanie kursora w MongoDB z przykładami
- Polecenie Ls w systemie Unix z przykładami
- Metoda MongoDB Sort () z przykładami
- Polecenie Grep w systemie Unix z prostymi przykładami
- Użycie ObjectId () w MongoDB z przykładami