Skip to content

Latest commit

 

History

History
239 lines (192 loc) · 5.69 KB

03 - Collections.md

File metadata and controls

239 lines (192 loc) · 5.69 KB

Collections

1. Objectives

  • understand the advantages of using List<T> instead of ArrayList
  • understand how to use the List<T> class
  • have an overview of the available collection types
  • know how to implement a custom collection with the help of IEnumerator<T> and IEnumerable<T>

2. ArrayList

Similar data can often be handled more efficiently when stored and manipulated as a collection MSDocs. You can find a guide for choosing the right type of collection at: https://docs.microsoft.com/en-us/dotnet/standard/collections/ . The document also discusses the algorithmic complexity of various operations.

Assignment

:octocat: Sample code available – Check the “StandardCollections” Sample

  1. Create a new project with the name “StandardCollections”

  2. Add the following method in the “Program” class and call it from the Main method

     private static void ArrayListExample()
    {
    	var words = new ArrayList();
    	words.Add("melon");
    	words.Add("avocado");
    	string first = (string)words[0];
    
    	//int first = (int)words[0];
    }

3. List<T>

  1. Add the following method in the “Program” class and call it from the Main method

    private static void ListExample()
    {
    	// New string-typed list
    	var words = new List<string>();
    	words.Add("melon");
    	words.Add("avocado");
    	words.AddRange(new[] { "banana", "plum" });
    	
    	// Insert at start
    	words.Insert(0, "lemon"); 
    	
    	// Insert at start
    	words.InsertRange(0, new[] { "peach", "nashi" }); 
    	words.Remove("melon");
    	
    	// Remove the 4th element
    	words.RemoveAt(3); 
    	
    	// Remove first 2 elements
    	words.RemoveRange(0, 2);
    	
    	// Remove all strings starting in 'n':
    	words.RemoveAll(x => x.StartsWith("n"));
    	
    	for (var i=0; i<words.Count; i++)
    	{
    		Console.WriteLine(words[i]);
    	}
    
    	foreach (var word in words)
    	{
    		Console.WriteLine(word);
    	}
    }
  2. Add the following “Person” class

    internal class Person
    {
    	#region Properties
    	public string Name { get; set; }
    	public int Age { get; set; }
    	#endregion
    
    	public Person(string name, int age)
    	{
    		Name = name;
    		Age = age;
    	}
    }
  3. Add the following method in the Program class and call it from the Main method

    private static void ListPersonExample()
    {
    	var personList = new List<Person>();
    
    	var rnd = new Random();
    	for (var i = 0; i < 10; i++)
    	{
    		personList.Add(new Person("Persoana " + i, rnd.Next(100)));
    	}
    
    	//Which interface is needed for Array.Sort(personList)
    
    	foreach (var p in personList) //equivalent to foreach (Person p in personList)
    		Console.WriteLine(p);
    }

4. Queues, Stacks, and Sets

5. Custom Collections

Assignment

:octocat: Sample code available – Check the “CustomCollections” Sample

  1. Add the following PersonCollection class

    internal class PersonCollection :IEnumerable<Person>
    {
    	private Person[] _personArray;
    
    	//Indexer [int]
    	public Person this[int index]
    	{
    		get { return _personArray[index]; }
    		set { _personArray[index] = value; }
    	}
    
    	//Indexer [string]
    	public Person this[string name]
    	{
    		get { return _personArray.FirstOrDefault(x=>x.Name == name); }
    	}
    
    	public int Length
    	{
    		get { return _personArray.Length; }
    	}
    
    	public PersonCollection()
    	{
    		_personArray = new []
    		{
    			new Person("name1", 1),
    			new Person("name2", 2),
    			new Person("name3", 3)
    		};
    	}
    	
    	public IEnumerator<Person> GetEnumerator()
    	{
    		return new PersonEnumerator(this);
    	}
    
    	IEnumerator IEnumerable.GetEnumerator()
    	{
    		return GetEnumerator();
    	}
    }
  2. Add the following PersonEnumerator class

    internal class PersonEnumerator : IEnumerator<Person>
    {
    	private int _nIndex;
    	private PersonCollection _personCollection;
    
    	public PersonEnumerator(PersonCollection personCollection)
    	{
    		_personCollection = personCollection;
    		_nIndex = -1;
    	}
    
    	public bool MoveNext()
    	{
    		_nIndex++;
    		return _nIndex < _personCollection.Length;
    	}
    
    	public void Reset()
    	{
    		_nIndex = -1;
    	}
    
    	public Person Current
    	{
    		get { return _personCollection[_nIndex]; }
    	}
    
    	object IEnumerator.Current
    	{
    		get { return Current; }
    	}
    
    	public void Dispose()
    	{
    		
    	}
    }
  3. Add the following method in the Program class and call it from the Main method

    private static void PersonCollectionExample()
    {
    	var personList = new PersonCollection();
    
    	foreach (var p in personList)
    		Console.WriteLine(p);
    }

6. Bibliography