// List ADT - Using Generic List Collection
// Written by : Jun Y. Ercia
// Date created : 6 September 2016
using System;
using System.Collections.Generic;
namespace Generic_List
{
class Program
{
static void Main(string[] args)
{
List<string> fruits = new List <string> (5);
fruits.Add("Banana"); //add an item at the end
fruits.Add("Apple"); //add an item at the end
fruits.Insert(1, "Grape"); //insert at position 1
foreach (string fruit in fruits) //print Apple, Grape, Banana
Console.Write("{0} ", fruit);
fruits.AddRange(new string [] {"Guava", "Orange"}); //add two items at the end
Console.WriteLine();
Console.WriteLine(string.Join(", ", fruits)); //print Banana, Grape, Apple, Guava, Orange
Console.WriteLine(fruits[0]); //print "Banana"
Console.WriteLine(fruits[fruits.Count-1]); //print "Orange"
fruits.RemoveAt(0); //remove "Banana"
Console.WriteLine(string.Join(", ", fruits)); //print Grape, Apple, Guava, Orange
fruits.Remove("Grape"); //remove "Grape"
Console.WriteLine(string.Join(", ", fruits)); //print Apple, Guava, Orange
fruits.Reverse(); //reverse the items
Console.WriteLine(string.Join(", ", fruits)); //print Orange, Guava, Apple
fruits.Sort(); //sort the items
Console.WriteLine(string.Join(", ", fruits)); //print Apple, Guava, Orange
fruits.Clear(); //make the list empty
Console.WriteLine(string.Join(", ", fruits)); //print nothing
Console.WriteLine("\nPress any key to continue..");
Console.ReadKey();
}
}
}
No comments:
Post a Comment