SlideShare a Scribd company logo
Arrays
Overview
 Overview of Arrays
 Creating Arrays
 Using Arrays
Overview of Arrays
 What Is an Array?
 Array Notation in C#
 Array Rank
 Accessing Array Elements
 Checking Array Bounds
 Comparing Arrays to Collections
What Is an Array?
 An array is a sequence of elements
 All elements in an array have the same type
 Structs can have elements of different types
 Individual elements are accessed using integer indexes
Integer index 0
(zero)
Integer index 4
(four)
Array Notation in C#
 You declare an array variable by specifying:
 The element type of the array
 The rank of the array
 The name of the variable
This specifies the rank of the array
This specifies the name of the array variable
This specifies the element type of the array
type[ ] name;
Array Rank
 Rank is also known as the array dimension
 The number of indexes associated with each element
Rank 1: One-dimensional
Single index associates with
each long element
Rank 2: Two-dimensional
Two indexes associate with
each int element
long[ ] row; int[,] grid;
Accessing Array Elements
 Supply an integer index for each rank
 Indexes are zero-based
3
2
1
long[ ] row;
...
row[3];
int[,] grid;
...
grid[1,2];
Checking Array Bounds
 All array access attempts are bounds checked
 A bad index throws an IndexOutOfRangeException
 Use the Length property and the GetLength method
row grid
row.GetLength(0)==6
row.Length==6
grid.GetLength(0)==2
grid.GetLength(1)==4
grid.Length==2*4
Comparing Arrays to Collections
 An array cannot resize itself when full
 A collection class, such as ArrayList, can resize
 An array is intended to store elements of one type
 A collection is designed to store elements of different
types
 Elements of an array cannot have read-only access
 A collection can have read-only access
 In general, arrays are faster but less flexible
 Collections are slightly slower but more flexible
 Creating Array Instances
 Initializing Array Elements
 Initializing Multidimensional Array Elements
 Creating a Computed Size Array
 Copying Array Variables
Creating Arrays
Creating Array Instances
 Declaring an array variable does not create an array!
 You must use new to explicitly create the array instance
 Array elements have an implicit default value of zero
row
0 0 0 0
grid
0 0 0
0 0 0
Variable Instance
long[ ] row = new long[4];
int[,] grid = new int[2,3];
Initializing Array Elements
 The elements of an array can be explicitly initialized
 You can use a convenient shorthand
row
0 1 2 3
Equivalent
long[ ] row = new long[4] {0, 1, 2, 3};
long[ ] row = {0, 1, 2, 3};
Initializing Multidimensional Array
Elements
 You can also initialize multidimensional array elements
 All elements must be specified
grid
5 4 3
2 1 0
Implicitly a new int[2,3] array


int[,] grid = {
{5, 4, 3},
{2, 1, 0}
};
int[,] grid = {
{5, 4, 3},
{2, 1 }
};
Creating a Computed Size Array
 The array size does not need to be a compile-time
constant
 Any valid integer expression will work
 Accessing elements is equally fast in all cases
Array size specified by compile-time integer constant:
Array size specified by run-time integer value:
long[ ] row = new long[4];
string s = Console.ReadLine();
int size = int.Parse(s);
long[ ] row = new long[size];
Copying Array Variables
 Copying an array variable copies the array variable only
 It does not copy the array instance
 Two array variables can refer to the same array instance
copy
row
0 0 0 0
Variable Instance
long[ ] row = new long[4];
long[ ] copy = row;
...
row[0]++;
long value = copy[0];
Console.WriteLine(value);
 Array Properties
 Array Methods
 Returning Arrays from Methods
 Passing Arrays as Parameters
 Command-Line Arguments
 Demonstration: Arguments for Main
 Using Arrays with foreach
 Quiz: Spot the Bugs
Using Arrays
Array Properties
row
0 0 0 0
grid
0 0 0
0 0 0
row.Rank
row.Length
grid.Rank
grid.Length
long[ ] row = new long[4];
int[,] grid = new int[2,3];
2
4
1
6
Array Methods
 Commonly used methods
 Sort – sorts the elements in an array of rank 1
 Clear – sets a range of elements to zero or null
 Clone – creates a copy of the array
 GetLength – returns the length of a given dimension
 IndexOf – returns the index of the first occurrence of a
value
Returning Arrays from Methods
 You can declare methods to return arrays
class Example {
static void Main( ) {
int[ ] array = CreateArray(42);
...
}
static int[ ] CreateArray(int size) {
int[ ] created = new int[size];
return created;
}
}
Passing Arrays as Parameters
 An array parameter is a copy of the array variable
 Not a copy of the array instance
class Example2 {
static void Main( ) {
int[ ] arg = {10, 9, 8, 7};
Method(arg);
System.Console.WriteLine(arg[0]);
}
static void Method(int[ ] parameter) {
parameter[0]++;
}
}
This method will modify
the original array
instance created in Main
Command-Line Arguments
 The runtime passes command line arguments to Main
 Main can take an array of strings as a parameter
 The name of the program is not a member of the array
class Example3 {
static void Main(string[ ] args) {
for (int i = 0; i < args.Length; i++) {
System.Console.WriteLine(args[i]);
}
}
}
Using Arrays with foreach
 The foreach statement abstracts away many details of
array handling
class Example4 {
static void Main(string[ ] args) {
foreach (string arg in args) {
System.Console.WriteLine(arg);
}
}
}
Quiz: Spot the Bugs
2
3
4
5
1
int [ ] array;
array = {0, 2, 4, 6};
int [ ] array;
System.Console.WriteLine(array[0]);
int [ ] array = new int[3];
System.Console.WriteLine(array[3]);
int [ ] array = new int[ ];
int [ ] array = new int[3]{0, 1, 2, 3};

More Related Content

PPTX
Java arrays
Jin Castor
 
ODP
C++ arrays part1
Subhasis Nayak
 
PDF
Array in Java
Ali shah
 
PPT
2 arrays
trixiacruz
 
PDF
An Introduction to Programming in Java: Arrays
Martin Chapman
 
PPTX
Arrays in C++
Kashif Nawab
 
PPT
Array
PRN USM
 
PDF
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 
Java arrays
Jin Castor
 
C++ arrays part1
Subhasis Nayak
 
Array in Java
Ali shah
 
2 arrays
trixiacruz
 
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Arrays in C++
Kashif Nawab
 
Array
PRN USM
 
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 

What's hot (20)

PDF
Java arrays (1)
Liza Abello
 
PPTX
C# Arrays
Hock Leng PUAH
 
PPT
C++ Arrays
أحمد محمد
 
PPT
02 c++ Array Pointer
Tareq Hasan
 
PPT
Array in Java
Shehrevar Davierwala
 
PPTX
Array in C# 3.5
Gopal Ji Singh
 
PDF
Java Arrays
OXUS 20
 
PPT
07 Arrays
maznabili
 
PDF
Week06
hccit
 
PDF
Arrays Java
Jose Sumba
 
PPTX
Array in Java
Ali shah
 
PPTX
Array
Anil Neupane
 
PPTX
C++ programming (Array)
طارق بالحارث
 
PPTX
ARRAY
ayush raj
 
PPT
9781439035665 ppt ch09
Terry Yoast
 
PPT
Array
Hajar
 
PPTX
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
LATHA LAKSHMI
 
PPTX
Arrays in java
Arzath Areeff
 
PPTX
OOPs with java
AAKANKSHA JAIN
 
PPTX
7array in c#
Sireesh K
 
Java arrays (1)
Liza Abello
 
C# Arrays
Hock Leng PUAH
 
C++ Arrays
أحمد محمد
 
02 c++ Array Pointer
Tareq Hasan
 
Array in Java
Shehrevar Davierwala
 
Array in C# 3.5
Gopal Ji Singh
 
Java Arrays
OXUS 20
 
07 Arrays
maznabili
 
Week06
hccit
 
Arrays Java
Jose Sumba
 
Array in Java
Ali shah
 
C++ programming (Array)
طارق بالحارث
 
ARRAY
ayush raj
 
9781439035665 ppt ch09
Terry Yoast
 
Array
Hajar
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
LATHA LAKSHMI
 
Arrays in java
Arzath Areeff
 
OOPs with java
AAKANKSHA JAIN
 
7array in c#
Sireesh K
 
Ad

Similar to Module 7 : Arrays (20)

PPTX
arrays in c# including Classes handling arrays
JayanthiM19
 
PDF
LectureNotes-05-DSA
Haitham El-Ghareeb
 
DOC
Arrays In General
martha leon
 
PDF
DSA-Lecture-05
Haitham El-Ghareeb
 
PPT
Visula C# Programming Lecture 5
Abou Bakr Ashraf
 
PPTX
Arrays.pptx
Mohit750936
 
PDF
Intake 38 3
Mahmoud Ouf
 
PDF
C sharp chap6
Mukesh Tekwani
 
PDF
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
PDF
C# p9
Renas Rekany
 
PDF
Lecture06 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PPTX
C# Lesson 3
Bohdan Pashkovskyi
 
PDF
Array and Collections in c#
Umar Farooq
 
PPTX
arrays-120712074248-phpapp01
Abdul Samee
 
PPT
Lecture 9
Soran University
 
PPTX
Arrays in .Net Programming Language.pptx
vanithachezian
 
PPTX
Intro to C# - part 2.pptx emerging technology
worldchannel
 
arrays in c# including Classes handling arrays
JayanthiM19
 
LectureNotes-05-DSA
Haitham El-Ghareeb
 
Arrays In General
martha leon
 
DSA-Lecture-05
Haitham El-Ghareeb
 
Visula C# Programming Lecture 5
Abou Bakr Ashraf
 
Arrays.pptx
Mohit750936
 
Intake 38 3
Mahmoud Ouf
 
C sharp chap6
Mukesh Tekwani
 
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Lecture06 computer applicationsie1_dratifshahzad
Atif Shahzad
 
C# Lesson 3
Bohdan Pashkovskyi
 
Array and Collections in c#
Umar Farooq
 
arrays-120712074248-phpapp01
Abdul Samee
 
Lecture 9
Soran University
 
Arrays in .Net Programming Language.pptx
vanithachezian
 
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Ad

More from Prem Kumar Badri (20)

PPTX
Module 15 attributes
Prem Kumar Badri
 
PPTX
Module 14 properties and indexers
Prem Kumar Badri
 
PPTX
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
PPTX
Module 13 operators, delegates, and events
Prem Kumar Badri
 
PPTX
Module 11 : Inheritance
Prem Kumar Badri
 
PPTX
Module 10 : creating and destroying objects
Prem Kumar Badri
 
PPTX
Module 9 : using reference type variables
Prem Kumar Badri
 
PPTX
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
PPTX
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
PPTX
Module 5 : Statements & Exceptions
Prem Kumar Badri
 
PPTX
Module 4 : methods & parameters
Prem Kumar Badri
 
PPTX
Module 3 : using value type variables
Prem Kumar Badri
 
PPTX
Module 2: Overview of c#
Prem Kumar Badri
 
PPTX
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
PPTX
C# Non generics collection
Prem Kumar Badri
 
PPTX
C# Multi threading
Prem Kumar Badri
 
PPT
C# Method overloading
Prem Kumar Badri
 
PPTX
C# Inheritance
Prem Kumar Badri
 
PPTX
C# Generic collections
Prem Kumar Badri
 
PPTX
C# Global Assembly Cache
Prem Kumar Badri
 
Module 15 attributes
Prem Kumar Badri
 
Module 14 properties and indexers
Prem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
Module 13 operators, delegates, and events
Prem Kumar Badri
 
Module 11 : Inheritance
Prem Kumar Badri
 
Module 10 : creating and destroying objects
Prem Kumar Badri
 
Module 9 : using reference type variables
Prem Kumar Badri
 
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
Module 5 : Statements & Exceptions
Prem Kumar Badri
 
Module 4 : methods & parameters
Prem Kumar Badri
 
Module 3 : using value type variables
Prem Kumar Badri
 
Module 2: Overview of c#
Prem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
C# Non generics collection
Prem Kumar Badri
 
C# Multi threading
Prem Kumar Badri
 
C# Method overloading
Prem Kumar Badri
 
C# Inheritance
Prem Kumar Badri
 
C# Generic collections
Prem Kumar Badri
 
C# Global Assembly Cache
Prem Kumar Badri
 

Recently uploaded (20)

PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
High Ground Student Revision Booklet Preview
jpinnuck
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
High Ground Student Revision Booklet Preview
jpinnuck
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 

Module 7 : Arrays

  • 2. Overview  Overview of Arrays  Creating Arrays  Using Arrays
  • 3. Overview of Arrays  What Is an Array?  Array Notation in C#  Array Rank  Accessing Array Elements  Checking Array Bounds  Comparing Arrays to Collections
  • 4. What Is an Array?  An array is a sequence of elements  All elements in an array have the same type  Structs can have elements of different types  Individual elements are accessed using integer indexes Integer index 0 (zero) Integer index 4 (four)
  • 5. Array Notation in C#  You declare an array variable by specifying:  The element type of the array  The rank of the array  The name of the variable This specifies the rank of the array This specifies the name of the array variable This specifies the element type of the array type[ ] name;
  • 6. Array Rank  Rank is also known as the array dimension  The number of indexes associated with each element Rank 1: One-dimensional Single index associates with each long element Rank 2: Two-dimensional Two indexes associate with each int element long[ ] row; int[,] grid;
  • 7. Accessing Array Elements  Supply an integer index for each rank  Indexes are zero-based 3 2 1 long[ ] row; ... row[3]; int[,] grid; ... grid[1,2];
  • 8. Checking Array Bounds  All array access attempts are bounds checked  A bad index throws an IndexOutOfRangeException  Use the Length property and the GetLength method row grid row.GetLength(0)==6 row.Length==6 grid.GetLength(0)==2 grid.GetLength(1)==4 grid.Length==2*4
  • 9. Comparing Arrays to Collections  An array cannot resize itself when full  A collection class, such as ArrayList, can resize  An array is intended to store elements of one type  A collection is designed to store elements of different types  Elements of an array cannot have read-only access  A collection can have read-only access  In general, arrays are faster but less flexible  Collections are slightly slower but more flexible
  • 10.  Creating Array Instances  Initializing Array Elements  Initializing Multidimensional Array Elements  Creating a Computed Size Array  Copying Array Variables Creating Arrays
  • 11. Creating Array Instances  Declaring an array variable does not create an array!  You must use new to explicitly create the array instance  Array elements have an implicit default value of zero row 0 0 0 0 grid 0 0 0 0 0 0 Variable Instance long[ ] row = new long[4]; int[,] grid = new int[2,3];
  • 12. Initializing Array Elements  The elements of an array can be explicitly initialized  You can use a convenient shorthand row 0 1 2 3 Equivalent long[ ] row = new long[4] {0, 1, 2, 3}; long[ ] row = {0, 1, 2, 3};
  • 13. Initializing Multidimensional Array Elements  You can also initialize multidimensional array elements  All elements must be specified grid 5 4 3 2 1 0 Implicitly a new int[2,3] array   int[,] grid = { {5, 4, 3}, {2, 1, 0} }; int[,] grid = { {5, 4, 3}, {2, 1 } };
  • 14. Creating a Computed Size Array  The array size does not need to be a compile-time constant  Any valid integer expression will work  Accessing elements is equally fast in all cases Array size specified by compile-time integer constant: Array size specified by run-time integer value: long[ ] row = new long[4]; string s = Console.ReadLine(); int size = int.Parse(s); long[ ] row = new long[size];
  • 15. Copying Array Variables  Copying an array variable copies the array variable only  It does not copy the array instance  Two array variables can refer to the same array instance copy row 0 0 0 0 Variable Instance long[ ] row = new long[4]; long[ ] copy = row; ... row[0]++; long value = copy[0]; Console.WriteLine(value);
  • 16.  Array Properties  Array Methods  Returning Arrays from Methods  Passing Arrays as Parameters  Command-Line Arguments  Demonstration: Arguments for Main  Using Arrays with foreach  Quiz: Spot the Bugs Using Arrays
  • 17. Array Properties row 0 0 0 0 grid 0 0 0 0 0 0 row.Rank row.Length grid.Rank grid.Length long[ ] row = new long[4]; int[,] grid = new int[2,3]; 2 4 1 6
  • 18. Array Methods  Commonly used methods  Sort – sorts the elements in an array of rank 1  Clear – sets a range of elements to zero or null  Clone – creates a copy of the array  GetLength – returns the length of a given dimension  IndexOf – returns the index of the first occurrence of a value
  • 19. Returning Arrays from Methods  You can declare methods to return arrays class Example { static void Main( ) { int[ ] array = CreateArray(42); ... } static int[ ] CreateArray(int size) { int[ ] created = new int[size]; return created; } }
  • 20. Passing Arrays as Parameters  An array parameter is a copy of the array variable  Not a copy of the array instance class Example2 { static void Main( ) { int[ ] arg = {10, 9, 8, 7}; Method(arg); System.Console.WriteLine(arg[0]); } static void Method(int[ ] parameter) { parameter[0]++; } } This method will modify the original array instance created in Main
  • 21. Command-Line Arguments  The runtime passes command line arguments to Main  Main can take an array of strings as a parameter  The name of the program is not a member of the array class Example3 { static void Main(string[ ] args) { for (int i = 0; i < args.Length; i++) { System.Console.WriteLine(args[i]); } } }
  • 22. Using Arrays with foreach  The foreach statement abstracts away many details of array handling class Example4 { static void Main(string[ ] args) { foreach (string arg in args) { System.Console.WriteLine(arg); } } }
  • 23. Quiz: Spot the Bugs 2 3 4 5 1 int [ ] array; array = {0, 2, 4, 6}; int [ ] array; System.Console.WriteLine(array[0]); int [ ] array = new int[3]; System.Console.WriteLine(array[3]); int [ ] array = new int[ ]; int [ ] array = new int[3]{0, 1, 2, 3};