IV.5.1 ArrayList
In C#, the ArrayList is a non-generic collection of objects whose size increases dynamically. It is the same as Array except that its size increases dynamically.
An ArrayList can be used to add unknown data where you don't know the types and the size of the data.
Create an ArrayList
The ArrayList class included in the System.Collections namespace. Create an object of the ArrayList using the new keyword.
Example: Create an ArrayList
using System.Collections;
ArrayList arlist = new ArrayList();
// or
var arlist = new ArrayList(); // recommended
Example:
Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections; //Add this line manually
namespace arraylist
{
public partial class Form1 : Form
{
ArrayList arr = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
arr.Add(txtItem.Text);
this.IstItem.DataSource = arr.ToArray();
txtItem.Clear();
txtItem.Focus();
}
private void btnSort_Click(object sender, EventArgs e)
{
arr.Sort();
this.IstItem.DataSource = arr.ToArray();
}
private void btnRemove_Click(object sender, EventArgs e)
{
arr.RemoveAt(this.IstItem.SelectedIndex);
this.IstItem.DataSource = arr.ToArray();
}
}
}
IV.5.2 Queue Collection
Queue is a special type of collection that stores the elements in FIFO style (First In First Out), exactly opposite of the Stack<T> collection. It contains the elements in the order they were added. C# includes generic Queue<T> and non-generic Queue collection. It is recommended to use the generic Queue<T> collection.
IV.5.3 Sorted List
In C#, SortedList is a collection of key/value pairs which are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic type of collection. The generic SortedList is defined in System.Collections.Generic namespace whereas non-generic SortedList is defined under System.Collections namespace, here we will discuss non-generic type SortedList.
Important Points:
- The SortedList class implements the IEnumerable, ICollection, IDictionary and ICloneable interfaces.
- In SortedList, an element can be accessed by its key or by its index.
- A SortedList object internally maintains two arrays to store the elements of the list, i.e, one - array for the keys and another array for the associated values.
- Here, a key cannot be null, but a value can be.
- The capacity of a SortedList object is the number of key/value pairs it can hold.
- In SortedList, duplicate keys are not allowed.
- In SortedList, you can store values of the same type and of the different types due to the non-generic collection. If you use a generic SortedList in your program, then it is necessary that the type of the values should be the same.
- In SortedList you cannot store keys of different data types in the same SortedList because the compiler will throw an exception. So, always add the key in your SortedList of the same type.
- You can also cast key/value pair of SortedList into DictionaryEntry.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace SortList
{
public partial class SortList : Form
{
SortList arr = new SortList();
public SortList()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if(arr.ContainsKey(txtKey.Text)== true)
{
MessageBox.Show("This Key is already exist.");
return;
}
arr.Add(txtKey.Text, txtItem.Text);
txtCount.Text = arr.txtCount.ToString();
//Clear TextBox
txtKey.Clear();
txtItem.Clear();
txtItem.Focus();
}
private void btnSearch_Click(object sender, EventArgs e)
{
if(arr.ContainsKey(txtKey.Text)== true)
{
int i = arr.IndexofKey(txtKey.Text);
txtKey_Result.Text = arr.GetKey(i).Tostring();
txtKey_Result.Text = arr.GetByIndex(i).Tostring();
}
}
}
}
IV.5.4 Stack Collection
Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. This class comes under System.Collections namespace.
Characteristics of Stack Class:
The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.
If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count. Pop is an O(1) operation.
Stack accepts null as a valid value and allows duplicate elements.
Example:
// C# code to create a Stack
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("1st Element");
myStack.Push("2nd Element");
myStack.Push("3rd Element");
myStack.Push("4th Element");
myStack.Push("5th Element");
myStack.Push("6th Element");
// Displaying the count of elements
// contained in the Stack
Console.Write("Total number of elements in the Stack are : ");
Console.WriteLine(myStack.Count);
// Displaying the top element of Stack
// without removing it from the Stack
Console.WriteLine("Element at the top is : " + myStack.Peek());
// Displaying the top element of Stack
// without removing it from the Stack
Console.WriteLine("Element at the top is : " + myStack.Peek());
// Displaying the count of elements
// contained in the Stack
Console.Write("Total number of elements in the Stack are : ");
Console.WriteLine(myStack.Count);
}
}
0 Comments