Download presentation
Presentation is loading. Please wait.
1
Managing Collections of Data
Imran Rashid CTO at ManiWeber Technologies
2
Outline Simple Array Multidimensional Arrays Generic
3
Simple Array
4
datatype[] arrayName;
Simple Array An array stores a fixed-size sequential collection of elements of the same type All arrays consist of contiguous memory locations A specific element in an array is accessed by an index The lowest address corresponds to the first element and the highest address to the last element Declaring Arrays datatype[] arrayName; double[] threeHeights;
5
double[] heights = new double[10];
Simple Array Initializing an Array Declaring an array does not initialize the it in the memory When the array variable is initialized, you can assign values to the array Array is a reference type, so you need to use the new keyword to create an instance of the array double[] heights = new double[10]; string[] colors = new string[7]; int[] ages = new int[3];
6
Simple Array Assigning Values to an Array
double[] balance = new double[10]; balance[0] = ; double[] balance = { , , }; int[] marks = new int[5] { 99, 98, 92, 97, 95 }; int[] marks = new int[] { 99, 98, 92, 97, 95 }; int[] marks = new int[] { 99, 98, 92, 97, 95 }; int[] score = marks;
7
Simple Array Console.WriteLine() Array of Arguemnts Iterating Arrays
foreach for Array Behaviors Sum() : int Average() : double Passing Arrays to Functions
8
Multidimensional Arrays
9
Multidimensional Arrays
Multidimensional arrays are also called rectangular array Multidimensional Arrays Initializing Two-Dimensional Arrays string[ , ] names; int[ , , ] marks; int[ , ] a = new int[3, 4] { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ };
10
Multidimensional Arrays
Example (Declaring) int[ , ] m = new int[3, 4]; Column 01 [0] Column 02 [1] Column 03 [2] Column 04 [3] Row 01 m[0][0] m[0][1] m[0][2] m[0][3] Row 02 m[1][0] m[1][1] m[1][2] m[1][3] Row 03 m[2][0] m[2][1] m[2][2] m[2][3]
11
Multidimensional Arrays
Initializing Arrays (guess the addresses) int[ , ] m = new int[3, 4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; Column 01 [0] Column 02 [1] Column 03 [2] Column 04 [3] Row 01 1 2 3 4 Row 02 5 6 7 8 Row 03 9 10 11 12
12
Multidimensional Arrays
Sections Array 3 X 3 top left, top center, top right middle left, middle center, middle right bottom left, bottom center, bottom right Tic Tac Toe Array 3 X 3 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 0,0 0,1 0,2 0,0 1,0 2,0 0,1 1,1 2,1 0,2 1,2 2,2 1,0 1,1 1,2 2,0 2,1 2,2
13
Generic
14
List<int> intList = new List<int>();
Generic List An array has a fixed number of items as we seen before Once you declared it, can’t change the number of items You can set an item to null but if an array has 3 items it always has 3 items In contrast, the List class let you create resizable array It let you add and remove items as you need to and can accommodate large amount of data This will introduce a new style of programming known as generic data typing List<int> intList = new List<int>(); var intList = new List<int>();
15
Generic Adding Items Manually Iterating List List Behaviors
foreach for List Behaviors Add(T) – at the end AddRange() – collection at the end Remove(T) – remove first occurrence RemoveAt(Index) – remove from a specific index Sort() – sorts all the elements Passing List to Functions
16
Q1: Difference b/w collection and generics
ASSIGNMENT Submission Date 29/10/2018 & in written form Q1: Difference b/w collection and generics Q2: ArrayList with 2 examples Q3: Dictionary with 2 examples Q4: Hashtable with 2 examples
17
Any ?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.