Chapter 8 ArraysSingle-Dimension ArraysSubscriptsDeclaration Statement for Arraysforeach Statementforeach ExamplesStructuresStructures cont – Simply declaring a datatypeStructures cont.Table LookupUsing List Boxes with ArraysMultidimensional ArraysMultidimensional Arrays cont.Initialize Two-Dimensional ArraysPrinting a Two-Dimensional TableSumming a Two-Dimensional TableLookup Operation for Two-Dimensional TablesPowerPoint PresentationChapter 8 ArraysProgramming in C# .NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.2Single-Dimension ArraysAn array is a list or series of values, similar to a list box without the boxCreate an array to keep a series of variables for later processingEach individual variable in an array is referenced by the same nameArrays also called tables or subscripted variablesEach individual variable is an element of the arraySubscript (or index) inside parentheses is the position of the element within the array3SubscriptsSubscripts may be constants, variables, or numeric expressionsSubscripts must be integersSpecify the number of elements in the array in the array’s declaration statementArray subscripts are zero basedYou declare a data type for the array and all array elements must be the same data typeA subscript must reference a valid element of the arrayException will be thrown if a subscript is out of range4Declaration Statement for ArraysGeneral Forma) Datatype[] ArrayName = new DataType[NumberofElements];b) Datatype[] ArrayName = new DataType[] {InitialValueList};c) Datatype[] ArrayName = {InitialValueList};Examplesa) string[] strName = new string[25]; <- initialized to emptya) decimal[] decBalance = new decimal[10]; <- initialized to zeroa) string[] strProduct = new string[99];b) int[] intValue = new int[] {1,5,12,18,20};c) string[] strName = {“Sean”, “Sam”, “Sally”, “Sara”};5foreach Statementforeach is a looping construct that does not require manipulation of the array subscriptsGeneral Formforeach (DataType ElementName in ArrayName){//Statement(s) in loop}C# automatically references each array element, assigns its value to ElementName, and makes one pass through the loopThe foreach loop will execute if the array has at least one element6foreach Examplesforeach (string strOneName in strName){Console.WriteLine(strOneName); //Write one element of the array}int intSum;foreach (int intOneTotal in intTotal){intSum += intOneTotal; //Add each element of the array to intSum}7StructuresCombine multiple fields of related data to create a new structure (record)Defining a structure is similar to defining a new data typeStruct declaration cannot go inside a methodBy default, a structure is publicIf an array is included inside a structure, you cannot specify the number of elements8Structures cont – Simply declaring a datatypeGeneral Form[public | private] struct NameOfStruct{public Datatype FirstField;public Datatype SecondField;. . .}Examplepublic struct Product{public string strDescription;public string strID;public int intQuantity;public decimal decPrice;}9Structures cont.Once a structure is created, declare variables of the structure typeEach field of data in variable declared as a structure is called an element of the structureC# does not allow declaration of the number of elements in an array in the struct declarationUsing a value as an index to an array is called a direct referenceView contents of array elements in break time using the Autos window by clicking on the plus sign at left of the array name10Table LookupDetermine which array element to update or access using a table lookupUse a while loop for a table lookupValidate input before performing table lookupCompare input to each element in array to find a matchIt is not necessary to arrange fields being searched in any particular sequence11Using List Boxes with ArraysIt is a good idea to use a list box for input of information to lookup in arrayUse the SelectedIndex property to determine the array subscriptSelectedIndex holds the position or index of the selected item from the list12Multidimensional ArraysTwo-dimensional arrays have rows and columnsArray declaration specifies number of rows and columns in the arrayRow is horizontal and column is verticalMust always use two subscripts to refer to individual elements of tableRow is first subscriptColumn is second subscript13Multidimensional Arrays cont.General FormDataType[,] ArrayName = new Datatype[NumberOfElements,NumberOfElements];DataType[,] ArrayName = new DataType[ , ] = {ListOfValues};Cannot specify the number of elements within parentheses and specify the initial valuesMust use a comma to specify two dimensions to the arrayExamples:string[,] strName = new string[3,4]; <- 3 rows , 4 columnsstring[,] strName = new string[ , ] = {{“James”, “Mary”, “Sammie”, “Sean”}, <- row 0{“Tom”, “Lee”, “Leon”, “Larry”}, <- row 1 {“Maria”, “Margaret”, “Jill”, “John”}}; <- row 214Initialize Two-Dimensional ArraysNumeric array elements are initialized to 0 and strings are initialized to empty stringsUse nested for loops to initialize array elementsint intRow, intColumn;for (intRow = 0; intRow < 3; intRow++){for (intColumn = 0; intColumn < 4; intColumn++){strName[intRow, intColumn] = “”;}}15Printing a Two-Dimensional TableUse a foreach loop to print contents of a two-dimensional tableProcesses all the columns in a row thenmoves to the next rowUse a for loop and specify a new x coordinate for each element to print on the same line16Summing a Two-Dimensional TableThere are several ways to sum a tableSum the columnsSum the rowsSum the figures in both directions and double-check the totalsTo sum an array in both directions, create two one-dimensional arrays to store the total fields17Lookup Operation for Two-Dimensional TablesUse direct reference and table lookup as in single-dimensional arraysLimitationsRow and column subscripts must be available to use direct reference Table lookup is the most common lookup techniqueLookup processes may require additional one-dimensional arrays or listsUse list boxes or text
View Full Document