array

10
Array

Transcript of array

Page 1: array

Array

Page 2: array

Introduction

• Sequential collection of elements of the same type

• used to store a collection of data

• consist of contiguous memory locations

• lowest address corresponds to the first element and the highestaddress to the last element.

First Element Last Element

Number[0] Number[1] Number[2] Number[3] Number[4] ………

Page 3: array

Types of Array

• One-Dimensional Array

• Two-Dimensional Array

• Jagged Array

0

1

2

21 85 94

21 89 15

6 47 64 55 10

Page 4: array

One-Dimensional Array

• Declaration

int[] intArray;

• Initializing an Array

int[] iArr = new int [10];

• Assigning Values to an Array

int[] iArr = { 234, 45, 34};

int[] iArr = int double[10]; balance[0] = 450;

int [] marks = new int [5] { 99, 98, 92, 97, 95};

Page 5: array

Two-Dimensional Array

• Initializing

• Accessing

int [,] a = 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 */ };

int val = a[2,3];

Page 6: array

Jagged Array

• is an array of arrays

• elements of a jagged array can be of different dimensions and sizes

• Jagged Array for 1-D Array

• Jagged Array for 2-D Array

Int [][] jaggedArray = new int[3][];jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };jaggedArray[1] = new int[] { 0, 2, 4, 6 };jaggedArray[2] = new int[] { 11, 22 };

int[][,] jaggedArray4 = new int [3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {11,22}, {99,88}, {0,9} } };

Page 7: array

Jagged Array(Real Life Example)

Page 8: array

C# Jagged vs. 2D ArrayNote 1:

• Jagged Array are faster than 2D Array

• Rectangular Jagged Array takes more memory than 2D Array.

//Program that tests jagged array memory: C#using System; class Program { static void Main() { long b1 = GC.GetTotalMemory(true);

// This program tests the memory usage of a 1000 x 100 element jagged array.

int[][] jagged = new int[1000][];for (int i = 0; i < 1000; i++) { jagged[i] = new int[100]; }long b2 = GC.GetTotalMemory(true);jagged[0][0] = 0; Console.WriteLine("{0} bytes (jagged 1000 x 100)", b2 - b1); } }

Output 416028 bytes (jagged 1000 x 100)

Program that tests 2D array memory: C#using System; class Program{ static void Main(){ long b1 = GC.GetTotalMemory(true);

// This tests the memory usage of a 1000 x 100 element two-dimensional array. //

int[,] array = new int[1000, 100];long b2 = GC.GetTotalMemory(true);array[0, 0] = 0;Console.WriteLine("{0} bytes (2D 1000 x 100)", b2 - b1); } }

Output 400032 bytes (2D 1000 x 100)

Page 9: array

Array Class

• provides functionality for creating, reversing, searching and sorting

• Array class is an abstract base class that means we cannot create an instance of the Array class.

• Creating an Array

Array ar = new Array(); // Errror:-cannot create an instance

Array stringArray = Array.CreateInstance(typeof(String), 3);stringArray.SetValue("Mahesh Chand", 0);stringArray.SetValue("Raj Kumar", 1);stringArray.SetValue("Neel Beniwal", 2);

Page 10: array