arrays Array Declaration Format for creating an array

  • Slides: 12
Download presentation
arrays

arrays

Array Declaration • Format for creating an array type [ ] identifier = new

Array Declaration • Format for creating an array type [ ] identifier = new type [integral value]; • Type can be any predefined types like int or string, or a class that you create in C# • Integral value is the number of elements – Length or size of the array – Can be a constant literal, a variable, or an expression that produces an integral value C# Programming: From Problem Analysis to Program Design 2

Array Declaration (continued) Figure 7 -1 Creation of an array • C# uses zero-based

Array Declaration (continued) Figure 7 -1 Creation of an array • C# uses zero-based arrays—meaning the first element is indexed by 0 C# Programming: From Problem Analysis to Program Design 3

Array Declaration (continued) • Array identifier, name, references first element – Contains address where

Array Declaration (continued) • Array identifier, name, references first element – Contains address where score[0] is located • First index for all arrays is 0 • Last element of all arrays is always referenced by an index with a value of the length of the array minus one • Can declare an array without instantiating it • The general form of the declaration is: type [ ] identifier; C# Programming: From Problem Analysis to Program Design 4

Array Declaration (continued) Figure 7 -2 Declaration of an array C# Programming: From Problem

Array Declaration (continued) Figure 7 -2 Declaration of an array C# Programming: From Problem Analysis to Program Design 5

Array Declaration (continued) • If you declare array with no values to reference, second

Array Declaration (continued) • If you declare array with no values to reference, second step required – must dimension the array • General form of the second step is: identifier = new type [integral value]; • Examples const int size = 15; string [ ] last. Name = new string [25]; double [ ] cost = new double [1000]; double [ ] temperature = new double [size]; int [ ] score; Two steps score = new int [size + 15]; C# Programming: From Problem Analysis to Program Design 6

using System; class Main. Class { public static void Main (string[] args) { double

using System; class Main. Class { public static void Main (string[] args) { double [ ] price = new double [ ] {3, 2. 2, 4. 7, 6. 1, 4}; //int [ ] an. Array = new int[5]; //int [ ] more. Nums = new int[3] {3, 4, 5}; for(int i=0; i<price. Length; i++) Console. Write. Line(price[i]); Console. Write. Line ("Done"); }//end main }//end class

using System; class Main. Class { public static void Main (string[] args) { //double

using System; class Main. Class { public static void Main (string[] args) { //double [ ] price = new double [ ] {3, 2. 2, 4. 7, 6. 1, 4}; int [ ] an. Array = new int[5]; //int [ ] more. Nums = new int[3] {3, 4, 5}; for(int i=0; i<an. Array. Length; i++) Console. Write. Line(an. Array[i]); Console. Write. Line ("Done"); }//end main }//end class

using System; class Main. Class { public static void Main (string[] args) { //double

using System; class Main. Class { public static void Main (string[] args) { //double [ ] price = new double [ ] {3, 2. 2, 4. 7, 6. 1, 4}; //int [ ] an. Array = new int[5]; int [ ] more. Nums = new int[3] {3, 4, 5}; for(int i=0; i<more. Nums. Length; i++) Console. Write. Line(more. Nums[i]); Console. Write. Line ("Done"); }//end main }//end class

Array name you are coping from Start at index 0 in source Array name

Array name you are coping from Start at index 0 in source Array name you are coping to Start coping at index 0 in target Copy 2 values from source Array. Copy(source, 0 , target, 0, 2 );

Array. Copy(source, 0, target, 0, 2); Copy 2 values

Array. Copy(source, 0, target, 0, 2); Copy 2 values

Array name you are coping from Start at index 2 in source Array name

Array name you are coping from Start at index 2 in source Array name you are coping to Start coping at index 1 in target Copy 2 values from source Array. Copy(source, 2 , target, 1, 2 );