SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Arrays ❑ Simple arrays ❑ Multidimensional arrays ❑ Jagged arrays ❑ The Array class ❑ Interfaces for arrays ❑ Enumerations Rajpat Systems
Arrays If you need to work with multiple objects of the same type, you can use collections and arrays. Simple Arrays If you need to use multiple objects of the same type, you can use an array. An array is a data structure that contains a number of elements of the same type. Rajpat Systems
Array Declaration & Array Initialization The array cannot be resized after the size was specified without copying all elements. int[] myArray; myArray = new int[4]; Using  an array initializer int[] myArray = new int[4] {4, 7, 11, 2}; int[] myArray = new int[] {4, 7, 11, 2}; int[] myArray = {4, 7, 11, 2}; Rajpat Systems
Rajpat Systems
Accessing Array Elements After an array is declared and initialized, you can access the array elements using an indexer. Arrays only support indexers that have integer parameters. With custom classes, you can also create indexers that support other types. int[] myArray = new int[] {4, 7, 11, 2}; int v1 = myArray[0]; // read first element int v2 = myArray[1]; // read second element myArray[3] = 44; // change fourth element If you use a wrong indexer value where no element exists, an exception of type IndexOutOfRangeExceptionis thrown. Rajpat Systems
If you don ’ t know the number of elements in the array, you can use the Length property 	for (int i = 0; i < myArray.Length; i++) 	{ Console.WriteLine(myArray[i]); 	} Rajpat Systems
Using Reference Types In addition to being able to declare arrays of predefined types, you can also declare arrays of custom types. public class Person { 	public Person() 	{ 	} 	public Person(string firstName, string lastName) 	{ this.FirstName = firstName; this.LastName = lastName; 	} 	public string FirstName { get; set; } 	public string LastName { get; set; } 	public override string ToString() 	{ 		return String.Format(“{0} {1}”, FirstName, LastName); 	} } Rajpat Systems
Declaring an array of two Person elements is similar to declaring an array of int 	Person[] myPersons = new Person[2]; If the elements in the array are reference types, memory must be allocated for every array element. In the array where no memory was allocated, a NullReferenceException is thrown. myPersons[0] = new Person(“Ayrton”, “Senna”); myPersons[1] = new Person(“Michael”, “Schumacher”); Person[] myPersons = { new Person(“Ayrton”, “Senna”), 				        new	 Person(“Michael”,“Schumacher”) }; Rajpat Systems
Rajpat Systems
Multidimensional Arrays You cannot change the rank after declaring an array. int[,] twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2; twodim[0, 2] = 3; twodim[1, 0] = 4; twodim[1, 1] = 5; twodim[1, 2] = 6; twodim[2, 0] = 7; twodim[2, 1] = 8; twodim[2, 2] = 9; Rajpat Systems
Using an array initializer When using an array initializer, you must initialize every element of the array. It is not possible to leave the initialization for some values. Rajpat Systems
Rajpat Systems
Jagged Arrays A jagged array is more flexible in sizing the array. With a jagged array every row can have a different size. int[][] jagged = new int[3][]; jagged[0] = new int[2] { 1, 2 }; jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 }; jagged[2] = new int[3] { 9, 10, 11 }; Rajpat Systems
for (int row = 0; row < jagged.Length; row++) {       for (int element = 0; element < jagged[row].Length; element++) 	{ Console.WriteLine(“row: {0}, element: {1}, value: {2}”, 			    row, element, jagged[row][element] 			); 	} } Rajpat Systems
Array Class Properties Rajpat Systems
Creating Arrays The Array class is abstract, so you cannot create an array by using a constructor. Rajpat Systems
Rajpat Systems
Copying Arrays Because arrays are reference types, assigning an array variable to another one just gives you two variables referencing the same array. For copying arrays, the array implements the interface ICloneable .  The Clone() method that is defined with this interface creates a shallow copy of the array. If the elements of the array are value types, all values are copied. Rajpat Systems
Rajpat Systems
If the array contains reference types, only the references are copied Rajpat Systems
Sorting The Array class implements a bubble - sort for sorting the elements in the array.br />The Sort() method requires the interface IComparable to be implemented by the elements in the array. Simple types such as System.String and System.Int32 implement IComparable , so you can sort elements containing these types. Rajpat Systems
Rajpat Systems
If you are using custom classes with the array, you must implement the interface IComparable . This interface defines just one method, CompareTo(). Rajpat Systems
Rajpat Systems
Array and Collection Interfaces The Array class implements the interfaces IEnumerable , ICollection , and IList for accessing and enumerating the elements of the array. Because with a custom array a class is created that derives from the abstract class Array , you can use the methods and properties of the implemented interfaces with an array variable. Rajpat Systems
IEnumerable IEnumerable is an interface that is used by the foreach statement to iterate through the array Rajpat Systems
ICollection The interface ICollection derives from the interface IEnumerable and has additional properties and methods. This interface is mainly used to get the number of elements in a collection and for synchronization. Rajpat Systems
Rajpat Systems
IList The IList interface derives from the interface ICollection and defines additional properties and methods. The major reason why the Array class implements the IList interface is that the IList interface defines the Item property for accessing the elements using an indexer. Many of the other IList members are implemented by the Array class by throwing a NotSupportedExceptio n , because these do not apply to arrays. Rajpat Systems
Rajpat Systems
IEnumerator Interface The foreach statement uses the methods and properties of the IEnumerator interface to iterate all elements in a collection. Rajpat Systems
foreach Statement The C# foreach statement is not resolved to a foreach statement in the IL code. Instead, the C# compiler converts the foreach statement to methods and properties of the IEnumerable interface. Rajpat Systems
Rajpat Systems

Weitere ähnliche Inhalte

Was ist angesagt? (20)

intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java arrays
Java arraysJava arrays
Java arrays
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Array
ArrayArray
Array
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java arrays
Java arraysJava arrays
Java arrays
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Chap09
Chap09Chap09
Chap09
 

Andere mochten auch

Arrays In General
Arrays In GeneralArrays In General
Arrays In Generalmartha leon
 
Csc153 chapter 06
Csc153 chapter 06Csc153 chapter 06
Csc153 chapter 06PCC
 
Learning VB.NET Programming Concepts
Learning VB.NET Programming ConceptsLearning VB.NET Programming Concepts
Learning VB.NET Programming Conceptsguest25d6e3
 
Portfolio -LouLou &amp; Tummie-
Portfolio -LouLou &amp; Tummie-Portfolio -LouLou &amp; Tummie-
Portfolio -LouLou &amp; Tummie-loulouandtummie
 
Exposure Lecture 2014 - Literature
Exposure Lecture 2014 - LiteratureExposure Lecture 2014 - Literature
Exposure Lecture 2014 - Literaturemediaplaylab
 
A L BÚ M D E L P A R Q U E E C O LÓ G I C O
A L BÚ M  D E L  P A R Q U E  E C O LÓ G I C OA L BÚ M  D E L  P A R Q U E  E C O LÓ G I C O
A L BÚ M D E L P A R Q U E E C O LÓ G I C OBAT007
 
Corinthians letroca 8 5A
Corinthians letroca 8 5ACorinthians letroca 8 5A
Corinthians letroca 8 5Asansampa
 
Performance of investment funds berhmani frigerio pan
Performance of investment funds berhmani frigerio panPerformance of investment funds berhmani frigerio pan
Performance of investment funds berhmani frigerio panBERHMANI Samuel
 
6B Quadrinhos da Mônica
6B Quadrinhos da Mônica6B Quadrinhos da Mônica
6B Quadrinhos da Mônicasansampa
 
Descascar letroca 03
Descascar letroca 03Descascar letroca 03
Descascar letroca 03sansampa
 
Character Development, for students, by students!
Character Development, for students, by students!Character Development, for students, by students!
Character Development, for students, by students!mediaplaylab
 
Learning with Quest Atlantis in Singapore
Learning with Quest Atlantis in SingaporeLearning with Quest Atlantis in Singapore
Learning with Quest Atlantis in Singaporemediaplaylab
 
Second Life user guide
Second Life user guideSecond Life user guide
Second Life user guidemediaplaylab
 
презентация1
презентация1презентация1
презентация1guest08adc2
 
Property Centric Media Kit
Property Centric Media KitProperty Centric Media Kit
Property Centric Media KitProperty Centric
 
Bluesky Concept Presentation
Bluesky Concept PresentationBluesky Concept Presentation
Bluesky Concept PresentationHuanYang
 

Andere mochten auch (20)

Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Csc153 chapter 06
Csc153 chapter 06Csc153 chapter 06
Csc153 chapter 06
 
Arrays
ArraysArrays
Arrays
 
Learning VB.NET Programming Concepts
Learning VB.NET Programming ConceptsLearning VB.NET Programming Concepts
Learning VB.NET Programming Concepts
 
C
CC
C
 
Portfolio -LouLou &amp; Tummie-
Portfolio -LouLou &amp; Tummie-Portfolio -LouLou &amp; Tummie-
Portfolio -LouLou &amp; Tummie-
 
Exposure Lecture 2014 - Literature
Exposure Lecture 2014 - LiteratureExposure Lecture 2014 - Literature
Exposure Lecture 2014 - Literature
 
England
EnglandEngland
England
 
A L BÚ M D E L P A R Q U E E C O LÓ G I C O
A L BÚ M  D E L  P A R Q U E  E C O LÓ G I C OA L BÚ M  D E L  P A R Q U E  E C O LÓ G I C O
A L BÚ M D E L P A R Q U E E C O LÓ G I C O
 
Corinthians letroca 8 5A
Corinthians letroca 8 5ACorinthians letroca 8 5A
Corinthians letroca 8 5A
 
Performance of investment funds berhmani frigerio pan
Performance of investment funds berhmani frigerio panPerformance of investment funds berhmani frigerio pan
Performance of investment funds berhmani frigerio pan
 
6B Quadrinhos da Mônica
6B Quadrinhos da Mônica6B Quadrinhos da Mônica
6B Quadrinhos da Mônica
 
Descascar letroca 03
Descascar letroca 03Descascar letroca 03
Descascar letroca 03
 
Character Development, for students, by students!
Character Development, for students, by students!Character Development, for students, by students!
Character Development, for students, by students!
 
Learning with Quest Atlantis in Singapore
Learning with Quest Atlantis in SingaporeLearning with Quest Atlantis in Singapore
Learning with Quest Atlantis in Singapore
 
Second Life user guide
Second Life user guideSecond Life user guide
Second Life user guide
 
презентация1
презентация1презентация1
презентация1
 
Epic product
Epic productEpic product
Epic product
 
Property Centric Media Kit
Property Centric Media KitProperty Centric Media Kit
Property Centric Media Kit
 
Bluesky Concept Presentation
Bluesky Concept PresentationBluesky Concept Presentation
Bluesky Concept Presentation
 

Ähnlich wie Guide to Arrays in C

arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arraysJayanthiM19
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programmingTaseerRao
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyworldchannel
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptxSoniaKapoor56
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collectionsphanleson
 

Ähnlich wie Guide to Arrays in C (20)

arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Array properties
Array propertiesArray properties
Array properties
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Md08 collection api
Md08 collection apiMd08 collection api
Md08 collection api
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 

Guide to Arrays in C

  • 1. Arrays ❑ Simple arrays ❑ Multidimensional arrays ❑ Jagged arrays ❑ The Array class ❑ Interfaces for arrays ❑ Enumerations Rajpat Systems
  • 2. Arrays If you need to work with multiple objects of the same type, you can use collections and arrays. Simple Arrays If you need to use multiple objects of the same type, you can use an array. An array is a data structure that contains a number of elements of the same type. Rajpat Systems
  • 3. Array Declaration & Array Initialization The array cannot be resized after the size was specified without copying all elements. int[] myArray; myArray = new int[4]; Using an array initializer int[] myArray = new int[4] {4, 7, 11, 2}; int[] myArray = new int[] {4, 7, 11, 2}; int[] myArray = {4, 7, 11, 2}; Rajpat Systems
  • 5. Accessing Array Elements After an array is declared and initialized, you can access the array elements using an indexer. Arrays only support indexers that have integer parameters. With custom classes, you can also create indexers that support other types. int[] myArray = new int[] {4, 7, 11, 2}; int v1 = myArray[0]; // read first element int v2 = myArray[1]; // read second element myArray[3] = 44; // change fourth element If you use a wrong indexer value where no element exists, an exception of type IndexOutOfRangeExceptionis thrown. Rajpat Systems
  • 6. If you don ’ t know the number of elements in the array, you can use the Length property for (int i = 0; i < myArray.Length; i++) { Console.WriteLine(myArray[i]); } Rajpat Systems
  • 7. Using Reference Types In addition to being able to declare arrays of predefined types, you can also declare arrays of custom types. public class Person { public Person() { } public Person(string firstName, string lastName) { this.FirstName = firstName; this.LastName = lastName; } public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format(“{0} {1}”, FirstName, LastName); } } Rajpat Systems
  • 8. Declaring an array of two Person elements is similar to declaring an array of int Person[] myPersons = new Person[2]; If the elements in the array are reference types, memory must be allocated for every array element. In the array where no memory was allocated, a NullReferenceException is thrown. myPersons[0] = new Person(“Ayrton”, “Senna”); myPersons[1] = new Person(“Michael”, “Schumacher”); Person[] myPersons = { new Person(“Ayrton”, “Senna”), new Person(“Michael”,“Schumacher”) }; Rajpat Systems
  • 10. Multidimensional Arrays You cannot change the rank after declaring an array. int[,] twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2; twodim[0, 2] = 3; twodim[1, 0] = 4; twodim[1, 1] = 5; twodim[1, 2] = 6; twodim[2, 0] = 7; twodim[2, 1] = 8; twodim[2, 2] = 9; Rajpat Systems
  • 11. Using an array initializer When using an array initializer, you must initialize every element of the array. It is not possible to leave the initialization for some values. Rajpat Systems
  • 13. Jagged Arrays A jagged array is more flexible in sizing the array. With a jagged array every row can have a different size. int[][] jagged = new int[3][]; jagged[0] = new int[2] { 1, 2 }; jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 }; jagged[2] = new int[3] { 9, 10, 11 }; Rajpat Systems
  • 14. for (int row = 0; row < jagged.Length; row++) { for (int element = 0; element < jagged[row].Length; element++) { Console.WriteLine(“row: {0}, element: {1}, value: {2}”, row, element, jagged[row][element] ); } } Rajpat Systems
  • 15. Array Class Properties Rajpat Systems
  • 16. Creating Arrays The Array class is abstract, so you cannot create an array by using a constructor. Rajpat Systems
  • 18. Copying Arrays Because arrays are reference types, assigning an array variable to another one just gives you two variables referencing the same array. For copying arrays, the array implements the interface ICloneable . The Clone() method that is defined with this interface creates a shallow copy of the array. If the elements of the array are value types, all values are copied. Rajpat Systems
  • 20. If the array contains reference types, only the references are copied Rajpat Systems
  • 21. Sorting The Array class implements a bubble - sort for sorting the elements in the array.br />The Sort() method requires the interface IComparable to be implemented by the elements in the array. Simple types such as System.String and System.Int32 implement IComparable , so you can sort elements containing these types. Rajpat Systems
  • 23. If you are using custom classes with the array, you must implement the interface IComparable . This interface defines just one method, CompareTo(). Rajpat Systems
  • 25. Array and Collection Interfaces The Array class implements the interfaces IEnumerable , ICollection , and IList for accessing and enumerating the elements of the array. Because with a custom array a class is created that derives from the abstract class Array , you can use the methods and properties of the implemented interfaces with an array variable. Rajpat Systems
  • 26. IEnumerable IEnumerable is an interface that is used by the foreach statement to iterate through the array Rajpat Systems
  • 27. ICollection The interface ICollection derives from the interface IEnumerable and has additional properties and methods. This interface is mainly used to get the number of elements in a collection and for synchronization. Rajpat Systems
  • 29. IList The IList interface derives from the interface ICollection and defines additional properties and methods. The major reason why the Array class implements the IList interface is that the IList interface defines the Item property for accessing the elements using an indexer. Many of the other IList members are implemented by the Array class by throwing a NotSupportedExceptio n , because these do not apply to arrays. Rajpat Systems
  • 31. IEnumerator Interface The foreach statement uses the methods and properties of the IEnumerator interface to iterate all elements in a collection. Rajpat Systems
  • 32. foreach Statement The C# foreach statement is not resolved to a foreach statement in the IL code. Instead, the C# compiler converts the foreach statement to methods and properties of the IEnumerable interface. Rajpat Systems