Array List 1) Write A Simple Program To Show The Following Output in Array List
Array List 1) Write A Simple Program To Show The Following Output in Array List
using System.Collections;
using System;
class Program
{
static void Main()
{
//System.Console.WriteLine(list);
System.Console.ReadLine();
}
}
2) Write a simple program using Array List to display the following output
class Program
{
static void Main()
{
//
// Create an ArrayList with two values.
//
ArrayList list = new ArrayList();
list.Add(5);
list.Add(7);
//
// Second ArrayList.
//
ArrayList list2 = new ArrayList();
list2.Add(10);
list2.Add(13);
//
// Add second ArrayList to first.
//
list.AddRange(list2);
//
// Display the values.
//
foreach (int i in list)
{
Console.WriteLine(i);
}
System.Console.ReadLine();
}
}
3) Write a program to clear the Array List (condition: you have to show the output before clearing
and after clearing)
using System;
using System.Collections;
class Program
{
static void Main()
{
//
// Create an ArrayList with two values.
//
ArrayList list = new ArrayList();
list.Add(9);
list.Add(10);
//
// Show number of elements in ArrayList.
//
Console.WriteLine(list.Count);
//
// Clear the ArrayList.
//
list.Clear();
//
// Show count again.
//
Console.WriteLine(list.Count);
Console.ReadLine();
}
}
4) Write a program for removeAt,Insert,removeRange,GetRange Properties in Array List
using System;
using System.Collections;
class Program
{
static void Main()
{
// Create an ArrayList with three strings.
ArrayList list = new ArrayList();
list.Add("Dot");
list.Add("Net");
list.Add("Perls");
// Remove middle element in ArrayList.
list.RemoveAt(1); // It becomes [Dot, Perls]
// Insert word at beginning of ArrayList.
list.Insert(0, "Carrot"); // It becomes [Carrot, Dot, Perls]
// Remove first two words from ArrayList.
list.RemoveRange(0, 2);
// Display the result ArrayList.
// Demonstrate ArrayList.
using System;
using System.Collections;
Console.WriteLine();
Console.WriteLine("Adding 6 elements");
// Add elements to the array list
al.Add('C');
al.Add('A');
al.Add('E');
al.Add('B');
al.Add('D');
al.Add('F');
Console.WriteLine("Removing 2 elements");
// Remove elements from the array list.
al.Remove('F');
al.Remove('A');
HASH TABLE
1) Write a simple program to display the key and values for the following
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";
}
Console.ReadLine();
}
}
IsReadOnly
Is Synchronized
Item []
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");
Console.Write("Count is " + h.Count.ToString());
Console.WriteLine();
Console.Write("Is ReadOnly set to :" + h.IsReadOnly.ToString());
Console.WriteLine();
Console.Write("IsSynchronized set to: " +
h.IsSynchronized.ToString());
Console.WriteLine();
Console.Write("Second item in hashtable: " + (string)h["key2"]);
Console.WriteLine();
Console.ReadLine();
}
}
Contains
using System;
using System.Collections;
class Program
{
static Hashtable GetHashtable()
{
// Create and return new Hashtable.
Hashtable hashtable = new Hashtable();
hashtable.Add("Area", 1000);
hashtable.Add("Perimeter", 55);
hashtable.Add("Mortgage", 540);
return hashtable;
}
using System;
using System.Collections;
class Program
{
static Hashtable GetHashtable()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(300, "Carrot");
hashtable.Add("Area", 1000);
return hashtable;
}
Console.ReadLine();
}
}
5) Write a Simple program to use Array List in Hash Table
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable.Add(400, "Blaze");
hashtable.Add(500, "Fiery");
hashtable.Add(600, "Fire");
hashtable.Add(800, "Immolate");
1) Write a simple program to display the key and values for the following
mySL1.Add("A", "a");
mySL1.Add("B", "b");
mySL1.Add("C", "c");
using System;
using System.Collections;
using System.Globalization;
2) Write a simple program using sorted list to sort the key and values based
on the method
GetByIndex()
using System;
using System.Collections;
public class SamplesSortedList
{
public static void Main()
{
// Creates and initializes a new SortedList.
SortedList mySL = new SortedList();
mySL.Add("one", "The");
mySL.Add("two", "quick");
mySL.Add("three", "brown");
mySL.Add("four", "fox");
list.Remove("G");
list.RemoveAt(2);
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create new Dictionary with four keys.
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("carrot", 1);
dictionary.Add("pear", 4);
dictionary.Add("apple", 6);
dictionary.Add("kiwi", 3);
// Count the keys.
int count1 = dictionary.Count;
// Remove one key.
dictionary.Remove("pear");
// Count the keys again.
int count2 = dictionary.Count;
// Clear the Dictionary contents.
dictionary.Clear();
// Count the keys again.
int count3 = dictionary.Count;
// Write the counts of the Dictionary.
Console.WriteLine(count1);
Console.WriteLine(count2);
Console.WriteLine(count3);
Console.ReadLine();
}
}
5) Write a Simple program in SortList. using if Condition.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Use a dictionary with an int key.
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(100, "Bill");
dict.Add(200, "Steve");
// You can lookup the int in the dictionary.
if (dict.ContainsKey(200))
{
Console.WriteLine(true);
}
}
}