0% found this document useful (0 votes)
48 views14 pages

Array List 1) Write A Simple Program To Show The Following Output in Array List

This document provides examples of using various .NET collection classes like ArrayList, HashTable, and SortedList. It includes 5 examples for ArrayList covering adding/removing/inserting elements, 2 examples for HashTable demonstrating basic usage and properties, and 1 example for SortedList. The examples demonstrate basic operations like adding/retrieving elements as well as properties of the collections.

Uploaded by

29prabakaran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views14 pages

Array List 1) Write A Simple Program To Show The Following Output in Array List

This document provides examples of using various .NET collection classes like ArrayList, HashTable, and SortedList. It includes 5 examples for ArrayList covering adding/removing/inserting elements, 2 examples for HashTable demonstrating basic usage and properties, and 1 example for SortedList. The examples demonstrate basic operations like adding/retrieving elements as well as properties of the collections.

Uploaded by

29prabakaran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

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()
{

ArrayList list = new ArrayList();


list.Add("One");
list.Add("Two");
list.Add("Three");
foreach (Object obj in list)
{
System.Console.Write("{0}\n", obj);
}
System.Console.WriteLine();

//System.Console.WriteLine(list);
System.Console.ReadLine();
}
}

2) Write a simple program using Array List to display the following output

(Condition: declare 2 Array List to achieve the 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.

foreach (string value in list)


{
Console.WriteLine(value); // <-- "Perls"
}
Console.ReadLine();
}
}
5)Using the Array List concept display the following output:

// Demonstrate ArrayList.

using System;
using System.Collections;

public class ArrayListDemo {


public static void Main() {
// create an array list
ArrayList al = new ArrayList();

Console.WriteLine("Initial capacity: " +


al.Capacity);
Console.WriteLine("Initial number of elements: " +
al.Count);

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("Current capacity: " +


al.Capacity);
Console.WriteLine("Number of elements: " +
al.Count);
// Display the array list using array indexing.
Console.Write("Current contents: ");
for(int i=0; i < al.Count; i++)
Console.Write(al[i] + " ");
Console.WriteLine("\n");

Console.WriteLine("Removing 2 elements");
// Remove elements from the array list.
al.Remove('F');
al.Remove('A');

Console.WriteLine("Current capacity: " +


al.Capacity);
Console.WriteLine("Number of elements: " +
al.Count);

// Use foreach loop to display the list.


Console.Write("Contents: ");
foreach(char c in al)
Console.Write(c + " ");
Console.WriteLine("\n");

Console.WriteLine("Adding 20 more elements");


// Add enough elements to force al to grow.
for(int i=0; i < 20; i++)
al.Add((char)('a' + i));
Console.WriteLine("Current capacity: " +
al.Capacity);
Console.WriteLine("Number of elements after adding 20: " +
al.Count);
Console.Write("Contents: ");
foreach(char c in al)
Console.Write(c + " ");
Console.WriteLine("\n");

// Change contents using array indexing.


Console.WriteLine("Change first three elements");
al[0] = 'X';
al[1] = 'Y';
al[2] = 'Z';
Console.Write("Contents: ");
foreach(char c in al)
Console.Write(c + " ");
Console.WriteLine();
Console.ReadLine();
}
}

HASH TABLE
1) Write a simple program to display the key and values for the following

Hash table [1] = "One";


Hash table [2] = "Two";
Hash table [13] = "Thirteen";
using System;
using System.Collections;

class Program
{
static void Main()
{
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";

foreach (DictionaryEntry entry in hashtable)


{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);

}
Console.ReadLine();
}
}

2) Write a simple program to show the following properties in hash table:


Count

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();

}
}

3) Write a simple program to express the properties of


ContainsKey

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;
}

static void Main()


{
Hashtable hashtable = GetHashtable();
// See if the Hashtable contains this key.
Console.WriteLine(hashtable.ContainsKey("Perimeter"));

// Test the Contains method. It works the same way.


Console.WriteLine(hashtable.Contains("Area"));

// Get value of Area with indexer.


int value = (int)hashtable["Area"];

// Write the value of Area.


Console.WriteLine(value);
Console.ReadLine();
}
}

4)Write a Program to show the type of casting in the hash table

using System;
using System.Collections;

class Program
{
static Hashtable GetHashtable()
{
Hashtable hashtable = new Hashtable();

hashtable.Add(300, "Carrot");
hashtable.Add("Area", 1000);
return hashtable;
}

static void Main()


{
Hashtable hashtable = GetHashtable();

string value1 = (string)hashtable[300];


Console.WriteLine(value1);

int value2 = (int)hashtable["Area"];


Console.WriteLine(value2);

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");

// Display the keys.


foreach (int key in hashtable.Keys)
{
Console.WriteLine(key);
}

// Display the values.


foreach (string value in hashtable.Values)
{
Console.WriteLine(value);
}

// Put keys in an ArrayList.


ArrayList arrayList = new ArrayList(hashtable.Keys);
foreach (int key in arrayList)
{
Console.WriteLine(key);
}
Console.ReadLine();
}
}
Sorted List

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;

public class SamplesSortedList


{
public static void Main()
{
SortedList mySL1 = new SortedList();
Console.WriteLine("mySL1 (default):");
mySL1.Add("A", "a");
mySL1.Add("B", "b");
mySL1.Add("C", "c");
try
{
mySL1.Add("first", "aaaa");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
}

public static void PrintKeysAndValues(SortedList myList)


{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.ReadLine();
}
}

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");

Console.WriteLine("The SortedList contains the following:");


PrintKeysAndValues(mySL);
}

public static void PrintKeysAndValues(SortedList myList)


{
Console.WriteLine("\t-KEY-\t-VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i),
myList.GetByIndex(i));
}
Console.WriteLine();
Console.ReadLine();
}
}

3)write a simple program for RemoveAt and Remove properties in SortList


using System;
using System.Collections;

public class MainClass


{
public static void Main()
{
SortedList list = new SortedList();
list.Add("G", "B");
list.Add("Ae", "Sm");
list.Add("Ay", "G");
list.Add("Ab", "S");
list.Add("W", "C");

list.Remove("G");
list.RemoveAt(2);

foreach (DictionaryEntry d in list)


{
Console.WriteLine("{0}, {1}", d.Key, d.Value);
}
Console.ReadLine();
}
}
4) write a simple program for Remove,Clear,Count properties in
SortList

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);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy