Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Visual Programming

Lecture 7

Jawad Rafeeq
Jawadrafeeq@ciitvehari.edu.pk
Outline
1D Array
• Array as a collection of variables of the same type stored at
contiguous memory locations.
• In C# the allocation of memory for the arrays is done dynamically but
can explicitly define the size.
• Arrays are kind of objects, therefore it is easy to find their size using
the predefined functions.

• int[] intArray;
• Int Array = new int[5];
It is a reference, which does not have a value (it points to null heap).
1D Array
• Array as a collection of variables of the same type stored at
contiguous memory locations.
• In C# the allocation of memory for the arrays is done dynamically but
can explicitly define the size.
• Arrays are kind of objects, therefore it is easy to find their size using
the predefined functions.

• int[] intArray;
• Int Array = new int[5];

Dynamic memory (heap) an area of 6 integer numbers is allocated


Initialize an array
• int[] ArrayA = new int[3] {1, 3, 5}; ///Fixed Sized Initialization

You can directly assign values (without defining size of array)


• int[] ArrayA = { 1, 3, 5 };
Same
You can initialize a dynamic length array as follows (without defining
size of array)
• int[] ArrayA = new int[] {1, 3, 5};
Multi-dimensional arrays (Sized)
• // four rows and two columns.
• int[ , ] intarray = new int[4, 2];

• //creates an array of three dimensions, 4, 2, and 3


• int[,, ] intarray1 = new int[4, 2, 3];
Multi-dimensional arrays (Dynamic)
• int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
class ABC {

static void Result(int[] arr) {


for(int i = 0; i < arr.Length; i++)
{
Console.WriteLine("Array Element: "+arr[i]);
}
}
public static void Main() {
int[] arr = {1, 2, 3, 4, 5};
1. Passing Arrays in Functions
2. Array with loops
// callling the method
Result(arr);
}
using System;
class ABC {

static public void Main()


{
Console.WriteLine("Print array:");
C# Foreach Loop
int[] a_array = new int[] { 1, 2, 3, 4, 5, 6, 7 };

// foreach loop begin it will run till the last element of the array

foreach(int items in a_array)


{
Console.WriteLine(items);
}
}
}
C# Lists
Lists
• List<T> class represents the list of objects which can be accessed by
index.
• It comes under the System.Collection.Generic namespace.
• List class can be used to create a collection of different types like
integers, strings etc.
• List<T> class also provides the methods to search, sort, and
manipulate lists.
Characteristics:
• It is different from the arrays. A List<T> can be resized dynamically but
arrays cannot.
• List<T> class can accept null as a valid value for reference types and it
also allows duplicate elements.
• If the Count becomes equals to Capacity, then the capacity of the List
increased automatically
• List<T> class is not sorted by default and elements are accessed by
zero-based index.
• For very large List<T> objects, you can increase the maximum capacity
to 2 billion elements on a 64-bit system by setting the enabled attribute
of the configuration element to true in the run-time environment.
C# List<T> Class
• List<T> provides functionality to create a list of objects, find list items,
sort list, search list, and manipulate list items.
What is the ‘T’ in List<T>? 
• In List<T>, T is the type of objects. 
Creating List<T> object
• List<T> is a generic class and is defined in the
System.Collections.Generic namespace. You must import this
namespace in your project to access the List<T> class.

• using System.Collections.Generic;
Creating List<T> object
• Generic Form:
• List<T> Abc = new List<T>();

• // List with default capacity  
• List<Int16> Abc = new List<Int16>();  

• // List with capacity = 5  
• List<string> authors = new List<string>(5); 

• //Initialization of integer values in a List


• List<int> authors = new List<int> { 12, 34, 54, 12, 354, 45, 623, 20 };  
• 
• string[] animals = { "Cow", "Camel", "Elephant" };  

• List<string> animalsList = new List<string>(animals); 
Adding Items to a C# List 
• // Dynamic ArrayList with no size limit   // List of string  
• List<int> numberList = new List<int>();   List<string> authors = new List<string>(5);  
• numberList.Add(32);   authors.Add("Bruce Hed");  
authors.Add("Chris Love");  
• numberList.Add(21);   authors.Add("Allen O'neill");  
• numberList.Add(45);   authors.Add("Alice mark");  
• numberList.Add(11);   authors.Add("SD wood");  
• numberList.Add(89);   authors.Add("David McCarter");
Adding Items to a C# List 
// Collection of string  
string[] animals = { "Cow", "Camel", "Elephant" }; 
 
// Create a List and add a collection  
List<string> animalsList = new List<string>();  
animalsList.AddRange(animals);  

foreach (string a in animalsList)  
Console.WriteLine(a);  

// Insert an item at index = 3


numbers.Insert(2, 22);
numbers.Insert(3, 33);
Remove Items of a C# List
• The following code snippet removes the first occurrence of ‘New Author1’.  
• authors.Remove("New Author1"); 

• The RemoveAt method removes an item at the given position. The following
code snippet removes the item at the 3rd position.
• authors.RemoveAt(3);

• The RemoveRange method removes a list of items from the starting index to
the number of items. The following code snippet removes two items starting
at 3rd position. 
• authors.RemoveRange(3, 2);  

• The Clear method removes all items from a List<T>. The following code
snippet removes all items from a List.
• authors.Clear();  
Find an Item in a C# List
Simple Method to find a element in List.
int index = animalsList.IndexOf("Camel");
if (index > -1)
Console.WriteLine("Item is in the List "+index);
else
Console.WriteLine("Item not found");

• For example, the following code snippet finds a string starting at the 3rd position in a String.
• Console.WriteLine(authors.IndexOf(“Fox", 2)); 

• The following code snippet looks for a string in the backward direction and returns the index of the
item if found.

• Console.WriteLine(authors.LastIndexOf(“Camel"));
Sort a C# List Items
The Sort method of List<T> sorts all items of the List using the QuickSort algorithm.

Original List items


List<string> authors = new List<string>(5); Console.WriteLine("Original List items");
===============
authors.Add("Bruce Hed");   Console.WriteLine("==============="); Bruce Hed
authors.Add("Chris Love"); // Print original order Chris Love
authors.Add("Allen O'neill"); foreach (string a in authors) Allen O'neill
authors.Add("Alice mark");   Console.WriteLine(a); Alice mark
// Sort list items SD wood
authors.Add("SD wood");  
authors.Sort(); David McCarter
authors.Add("David McCarter");
Sorted List items
Console.WriteLine(); ===============
Console.WriteLine("Sorted List items"); Alice mark
Console.WriteLine("==============="); Allen O'neill
// Print sorted items Bruce Hed
foreach (string a in authors) Chris Love
David McCarter
Console.WriteLine(a);
SD wood
Sort a C# List Items Original List items
===============
The Sort method of List<T> sorts all items of the List using the QuickSort algorithm. 12
34
54
List<int> Values = new List<int> { 12, 34, 54, 12, 354, 45, 623, 20 };  
12
Console.WriteLine("Original List items"); 354
Console.WriteLine("==============="); 45
// Print original order 623
foreach (int a in Values) 20
Console.WriteLine(a);
// Sort list items Sorted List items
Values.Sort(); ===============
12
12
Console.WriteLine();
20
Console.WriteLine("Sorted List items"); 34
Console.WriteLine("==============="); 45
// Print sorted items 54
foreach (int a in Values) 354
Console.WriteLine(a); 623
Reverse an ArrayList Original List items
===============
12
• List<int> Values = new List<int> { 12, 34, 54, 65, 74, 85, 95, 98 }; 
34
• Console.WriteLine("Original List items"); 54
• Console.WriteLine("==============="); 65
74
• // Print original order 85
• foreach (int a in Values) 95
• Console.WriteLine(a); 98

• // Reversed list items Reversed List items


• Values.Reverse(); ===============
98
• Console.WriteLine();
95
• Console.WriteLine(“Reversed List items"); 85
• Console.WriteLine("==============="); 74
65
• // Print sorted items 54
• foreach (int a in Values) 34
• Console.WriteLine(a); 12
Search a C# List
• The BinarySearch method of List<T> searches a sorted list and returns
the zero-based index of the found item. The List<T> must be sorted
before this method can be used. 

• The following code snippet returns an index of a string in a List.


• int binsear = authors.BinarySearch("Alice mark");  
Storing different Data types in Lists C#
• public static void Main(String[] args)
• {
• List<object> list = new List<object>();
• list.Add(23);
• list.Add("Pak");
• list.Add(45.6);
• list.Add("Vehari");
• foreach(object O in list)
• { System.Console.WriteLine(O); }
• Console.ReadKey();

• }
How to confuse C# Lists?

You might also like