SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Arrays
An array stores multiple elements of the same type
that type can be simple (value) types or objects
• for arrays of simple types, each element contains one value of
the declared type
• for arrays of reference types (e.g., objects), every element of
the array is a reference to an object of the data type of the
array

Refer to particular element in the array by position
number
the name of the array followed by the position number
(subscript) of the element in square brackets ([])
• [ ] is considered as an operator

1
Arrays: Declaration and Instantiation
An array can be allocated using the keyword new to specify how many elements the
array should hold
bool[] flags; // declare flags
flags = new bool[20]; // create an array and make flags a ref.
// now flags is reference to another array
flags = new bool[10];
// declare variable grades; create an array; make grades a
// reference of the array
int[] grades = new int[12];
float[] prices = new float[500];
string[] codes = new string[26];
Time1[] times;
times = new Time1[10];
2
Array: An Array of Simple Values
grades[ 0 ]
grades[ 1 ]

0

grades[ 3 ]

72
1543
-89

grades[ 6 ]

0

grades[ 7 ]

62

grades[ 8]

-3

grades[ 9 ]

1

grades[ 10 ]

6453

grades[ 11 ]
A 12-element array of values.

grades[ 2 ]

grades[ 5 ]

position number (index or
subscript) of the element
within array grades

6

grades[ 4 ]

grades

-45

-78
3
Array: An Array of Objects
times[ 0 ]
times[ 1 ]

ref to obj 2

times[ 3 ]

ref to obj 3
ref to obj 4
ref to obj 5

times[ 6 ]

A 10-element array of objects

times[ 2 ]

times[ 5 ]

position number (index or
subscript) of the element
within array times

ref to obj 1

times[ 4 ]

times

ref to obj 0

ref to obj 6

times[ 7 ]

ref to obj 7

times[ 8]

ref to obj 8

times[ 9 ]

ref to obj 9

4
Arrays as Objects
In C#, an array behaves very much like an
object
declaration and instantiation are like objects
• declare an array variable
• create an array using new
• make a variable a reference of an array

parameter passing is similar to objects
• we will discuss the detail later.

an array has the Length property
5
Array: Length
Each array has a public property called
Length that stores the size of the array
once an array is created, it has a fixed size

It is referenced using the array name (just like
any other object):
grades.Length
Note that Length holds the number of
elements, not the largest index
6
Array Instantiation and Initialization in
One Step: Initializer List
An initializer list can be used to instantiate and initialize
an array in one step
The values are delimited by braces and separated by
commas
Allocate space for the array – number of elements in initializer list determines
the size of array
Elements in array are initialized with the values in the initializer list

The new operator is not used
Examples:

int[] units = {147, 323, 89, 933, 540};
char[] letterGrades = {'A', 'B', 'C', 'D', 'F'};
string[] wordList = {“bs703“,

“computer", “television"};
7
Recall: Two Types of Variables
A variable represents a cell in memory
Value type

x
y

int, char, byte, float, double, string
A value type variable stores a value of the
type of the variable in the memory
int x = 45;
double y = 45.12;

45
45.12

Reference type

A variable that “stores” object or array
actually stores a reference to an object
or array, e.g.,
A reference is a location in computer’s
memory where the object or array itself
is stored
Time3 t1;
t1 = new Time3(11, 45, 59);

t1

11
45
59

8
Implications of the Two Types of Variables:
Assignment
An assignment of one value variable to
x
another value variable copies the value, e.g.,
int x = 45;
y
double y = 45.12;
int z;
z
z = x;
An assignment of one reference variable to
another reference variable copies the reference, e.g.,
Time3 t1;
t1 = new Time3(11, 45, 59);
t1
Time3 t2;
t2 = t1;

45
45.12
45

11
45
59

t2
9
Two-Dimensional Arrays
A one-dimensional array stores a list of
values
A two-dimensional array, also called doublesubscripted array, can be thought of as a
table of values, with rows and columns
a two-dimensional array element is referenced
using two index numbers

10
Two Types of Double-Subscripted Arrays
rectangular arrays

often represent tables in which each row is the same size and
each column is the same size, e.g.,

int[,] a1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] a11 = new int[3,4];

jagged arrays

• arrays of arrays
• arrays that compose jagged arrays can be of different lengths,
e.g.,

int[][] array2 = new int[
array2[ 0 ] = new int[] {
array2[ 1 ] = new int[] {
array2[ 2 ] = new int[] {
array2[2][1] = 3;

3 ][];
1, 2 };
3 };
4, 5, 6 };
11
Double-Subscripted Arrays
Column 0

Column 1

Column 2

Column 3

Row 0

a[0][0]

a[0][1]

a[0][2]

a[0][3]

Row 1

a[1][0]

a[1][1]

a[1][2]

a[1][3]

Row 2

a[2][0] a [2][1] a[2][2]

a[2][3]

Column index (or subscript)
Row index (or subscript)
Array name

Double-subscripted array with three rows and four columns.

12

Weitere ähnliche Inhalte

Was ist angesagt?

Arrays Java
Arrays JavaArrays Java
Arrays JavaJose Sumba
 
Dynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDeepam Aggarwal
 
Presentation on array
Presentation on array Presentation on array
Presentation on array topu93
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arraysVictor Palmar
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT LATHA LAKSHMI
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryGauravPatil318
 
Standard template library
Standard template libraryStandard template library
Standard template librarySukriti Singh
 
Arrays in c
Arrays in cArrays in c
Arrays in cAnIsh Kumar
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)Sangharsh agarwal
 
2 arrays
2   arrays2   arrays
2 arraystrixiacruz
 
Data structures
Data structuresData structures
Data structuresSneha Chopra
 
Elementary data structure
Elementary data structureElementary data structure
Elementary data structureBiswajit Mandal
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)Liza Abello
 

Was ist angesagt? (20)

Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Arrays Java
Arrays JavaArrays Java
Arrays Java
 
Dynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDynamic memory allocation and linked lists
Dynamic memory allocation and linked lists
 
7.basic array
7.basic array7.basic array
7.basic array
 
STL in C++
STL in C++STL in C++
STL in C++
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
 
Array
ArrayArray
Array
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
2 arrays
2   arrays2   arrays
2 arrays
 
Java part 2
Java part  2Java part  2
Java part 2
 
Lecture 6 - Arrays
Lecture 6 - ArraysLecture 6 - Arrays
Lecture 6 - Arrays
 
Data structures
Data structuresData structures
Data structures
 
Elementary data structure
Elementary data structureElementary data structure
Elementary data structure
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 

Andere mochten auch

Visual studio .net c# study guide
Visual studio .net c# study guideVisual studio .net c# study guide
Visual studio .net c# study guideroot12345
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Abou Bakr Ashraf
 
Visual programming lab
Visual programming labVisual programming lab
Visual programming labSoumya Behera
 
Visual Programming
Visual ProgrammingVisual Programming
Visual ProgrammingBagzzz
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programmingRoger Argarin
 
Visual Basic Controls ppt
Visual Basic Controls pptVisual Basic Controls ppt
Visual Basic Controls pptRanjuma Shubhangi
 

Andere mochten auch (9)

Visual studio .net c# study guide
Visual studio .net c# study guideVisual studio .net c# study guide
Visual studio .net c# study guide
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1
 
Visual programming
Visual programmingVisual programming
Visual programming
 
Visual programming lab
Visual programming labVisual programming lab
Visual programming lab
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programming
 
Visual Basic Controls ppt
Visual Basic Controls pptVisual Basic Controls ppt
Visual Basic Controls ppt
 

Ă„hnlich wie Visula C# Programming Lecture 5

Ă„hnlich wie Visula C# Programming Lecture 5 (20)

Array.ppt
Array.pptArray.ppt
Array.ppt
 
array.ppt
array.pptarray.ppt
array.ppt
 
ch11.ppt
ch11.pptch11.ppt
ch11.ppt
 
Arrays in C Programming
Arrays in C ProgrammingArrays in C Programming
Arrays in C Programming
 
Array lecture
Array lectureArray lecture
Array lecture
 
Lecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdfLecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdf
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
java.pdf
java.pdfjava.pdf
java.pdf
 
Arrays in JAVA.ppt
Arrays in JAVA.pptArrays in JAVA.ppt
Arrays in JAVA.ppt
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Arrays
ArraysArrays
Arrays
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
array Details
array Detailsarray Details
array Details
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 

Mehr von Abou Bakr Ashraf

Security & protection in operating system
Security & protection in operating systemSecurity & protection in operating system
Security & protection in operating systemAbou Bakr Ashraf
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Abou Bakr Ashraf
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Abou Bakr Ashraf
 
Visula C# Programming Lecture 2
Visula C# Programming Lecture 2Visula C# Programming Lecture 2
Visula C# Programming Lecture 2Abou Bakr Ashraf
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 

Mehr von Abou Bakr Ashraf (6)

Security & protection in operating system
Security & protection in operating systemSecurity & protection in operating system
Security & protection in operating system
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Visula C# Programming Lecture 7
Visula C# Programming Lecture 7
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
Visula C# Programming Lecture 2
Visula C# Programming Lecture 2Visula C# Programming Lecture 2
Visula C# Programming Lecture 2
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 

KĂĽrzlich hochgeladen

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

KĂĽrzlich hochgeladen (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

Visula C# Programming Lecture 5

  • 1. Arrays An array stores multiple elements of the same type that type can be simple (value) types or objects • for arrays of simple types, each element contains one value of the declared type • for arrays of reference types (e.g., objects), every element of the array is a reference to an object of the data type of the array Refer to particular element in the array by position number the name of the array followed by the position number (subscript) of the element in square brackets ([]) • [ ] is considered as an operator 1
  • 2. Arrays: Declaration and Instantiation An array can be allocated using the keyword new to specify how many elements the array should hold bool[] flags; // declare flags flags = new bool[20]; // create an array and make flags a ref. // now flags is reference to another array flags = new bool[10]; // declare variable grades; create an array; make grades a // reference of the array int[] grades = new int[12]; float[] prices = new float[500]; string[] codes = new string[26]; Time1[] times; times = new Time1[10]; 2
  • 3. Array: An Array of Simple Values grades[ 0 ] grades[ 1 ] 0 grades[ 3 ] 72 1543 -89 grades[ 6 ] 0 grades[ 7 ] 62 grades[ 8] -3 grades[ 9 ] 1 grades[ 10 ] 6453 grades[ 11 ] A 12-element array of values. grades[ 2 ] grades[ 5 ] position number (index or subscript) of the element within array grades 6 grades[ 4 ] grades -45 -78 3
  • 4. Array: An Array of Objects times[ 0 ] times[ 1 ] ref to obj 2 times[ 3 ] ref to obj 3 ref to obj 4 ref to obj 5 times[ 6 ] A 10-element array of objects times[ 2 ] times[ 5 ] position number (index or subscript) of the element within array times ref to obj 1 times[ 4 ] times ref to obj 0 ref to obj 6 times[ 7 ] ref to obj 7 times[ 8] ref to obj 8 times[ 9 ] ref to obj 9 4
  • 5. Arrays as Objects In C#, an array behaves very much like an object declaration and instantiation are like objects • declare an array variable • create an array using new • make a variable a reference of an array parameter passing is similar to objects • we will discuss the detail later. an array has the Length property 5
  • 6. Array: Length Each array has a public property called Length that stores the size of the array once an array is created, it has a fixed size It is referenced using the array name (just like any other object): grades.Length Note that Length holds the number of elements, not the largest index 6
  • 7. Array Instantiation and Initialization in One Step: Initializer List An initializer list can be used to instantiate and initialize an array in one step The values are delimited by braces and separated by commas Allocate space for the array – number of elements in initializer list determines the size of array Elements in array are initialized with the values in the initializer list The new operator is not used Examples: int[] units = {147, 323, 89, 933, 540}; char[] letterGrades = {'A', 'B', 'C', 'D', 'F'}; string[] wordList = {“bs703“, “computer", “television"}; 7
  • 8. Recall: Two Types of Variables A variable represents a cell in memory Value type x y int, char, byte, float, double, string A value type variable stores a value of the type of the variable in the memory int x = 45; double y = 45.12; 45 45.12 Reference type A variable that “stores” object or array actually stores a reference to an object or array, e.g., A reference is a location in computer’s memory where the object or array itself is stored Time3 t1; t1 = new Time3(11, 45, 59); t1 11 45 59 8
  • 9. Implications of the Two Types of Variables: Assignment An assignment of one value variable to x another value variable copies the value, e.g., int x = 45; y double y = 45.12; int z; z z = x; An assignment of one reference variable to another reference variable copies the reference, e.g., Time3 t1; t1 = new Time3(11, 45, 59); t1 Time3 t2; t2 = t1; 45 45.12 45 11 45 59 t2 9
  • 10. Two-Dimensional Arrays A one-dimensional array stores a list of values A two-dimensional array, also called doublesubscripted array, can be thought of as a table of values, with rows and columns a two-dimensional array element is referenced using two index numbers 10
  • 11. Two Types of Double-Subscripted Arrays rectangular arrays often represent tables in which each row is the same size and each column is the same size, e.g., int[,] a1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; int[,] a11 = new int[3,4]; jagged arrays • arrays of arrays • arrays that compose jagged arrays can be of different lengths, e.g., int[][] array2 = new int[ array2[ 0 ] = new int[] { array2[ 1 ] = new int[] { array2[ 2 ] = new int[] { array2[2][1] = 3; 3 ][]; 1, 2 }; 3 }; 4, 5, 6 }; 11
  • 12. Double-Subscripted Arrays Column 0 Column 1 Column 2 Column 3 Row 0 a[0][0] a[0][1] a[0][2] a[0][3] Row 1 a[1][0] a[1][1] a[1][2] a[1][3] Row 2 a[2][0] a [2][1] a[2][2] a[2][3] Column index (or subscript) Row index (or subscript) Array name Double-subscripted array with three rows and four columns. 12