final exam review
play

Final Exam Review Monchai Sopitkamon, Ph.D. Final Exam Topics - PDF document

2/22/2007 Final Exam Review Monchai Sopitkamon, Ph.D. Final Exam Topics Arrays Methods 1 2/22/2007 What Is an Array? An array is a sequence of elements All elements in an array have the same type Structs can have elements


  1. 2/22/2007 Final Exam Review Monchai Sopitkamon, Ph.D. Final Exam Topics � Arrays � Methods 1

  2. 2/22/2007 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 Array notation in C# � You declare an array variable by specifying: if i � The element type of the array � The rank of the array � The name of the variable 2

  3. 2/22/2007 Array Rank � Rank is also known as the array di dimension i � The number of indexes associated with each element Accessing Array Elements � Supply an integer index for each rank � Indexes are zero-based 3

  4. 2/22/2007 Arrays - Declaration � D � Declaration only l ti l < type > [] < name > ; � Declaration with creation < type > [] < name > = new < type > [ <num-elts> ]; � Declaration with initialization: 2 ways 1. < type > [] < name > = new < type > [ < num-elts > ] { < value-lists > }; 2. < type > [] < name > = { < value-lists > }; Arrays – Declaration: Examples STACK HEAP � Declaration only int[] a i t [] int [] a; ? ? � Declaration with creation int[] a int [] a = new int[3]; @ 0 0 0 � Declaration with initialization int[] a 1. int [] a = new int[3] {1,2,3}; @ 1 2 3 2. int [] d = {7,8,9,10}; 4

  5. 2/22/2007 Array - Rules � First index of an array begins with 0 � Must initialize all elements; can’t do partial � Must initialize all elements; can t do partial initialization. � Arrays are reference types, not value types. � The array size does not need to be a compile- time constant; any valid integer expression will work � Rank property specifies the dimension of the � Rank property specifies the dimension of the array instance. � Length property specifies the total number of elements in an array instance. Comparing Arrays to Collections � An array cannot resize itself when full � A collection class, such as ArrayList, can resize co ect o c ass, suc as ay st, ca es e � 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 5

  6. 2/22/2007 Array Rank & Length Example Copying Arrays � Arrays are reference types. � An array variable contains a reference to an � A i bl t i f t array instance. � This means that when you copy an array variable, you end up with two references that refer to the same array instance, for example: int[] pins = { 9, 3, 7, 2 }; int[] alias = pins; // alias and pins refer to the same array instance 6

  7. 2/22/2007 Copying Arrays � If you want to make a copy of the array instance that an array variable refers to you have to do that an array variable refers to, you have to do two things. � First, you need to create a new array instance of the same type and the same length as the array you are copying, as in this example: int[] pins = { 9, 3, 7, 2 }; int[] pins = { 9 3 7 2 }; int[] copy = new int[pins.Length]; � It's better to determine the length of an array by using its Length property Copying Arrays � The values inside copy are now all initialized to their default value of 0 their default value of 0. � The second thing you need to do is set the values inside the new array to the same values as in the original array. You could do this by using a for statement, as shown in this example: int[] pins = { 9, 3, 7, 2 }; int[] copy = new int[pins.Length]; for (int i = 0; i != copy.Length; i++) { copy[i] = pins[i]; } 7

  8. 2/22/2007 Copying Arrays � An array copy is a shallow copy and not a deep copy copy. � To illustrate this principle, consider the effect of creating and copying an array whose element type is, itself, a reference type. The following example creates an array of four triangles: Triangle[] triangles = new Triangle[4]; Copying Arrays � If Triangle is a class, the rules of array instance initialization state that each element is initialized initialization state that each element is initialized to null . In other words, a diagram of the previous statement looks like this. Triangle[] triangles = new Triangle[4]; 8

  9. 2/22/2007 Copying Arrays � Suppose you then fill the triangles array with four new Triangle objects, like this: Triangle objects, like this: Triangle[] triangles = new Triangle[4]; for (int i = 0; i != triangles.Length; i++) { triangles[i] = new Triangle(); } � A diagram of this example looks like this. Copying Arrays � Finally, suppose you now make a copy of the triangles array, like this: array, like this: Triangle[] copy = new Triangle[triangles.Length]; for (int i = 0; i != copy.Length; i++) { copy[i] = triangles[i]; } � A diagram of this example looks like this. 9

  10. 2/22/2007 Copying Arrays – Important! � From the previous slide, an array of objects is really an array of references to objects really an array of references to objects. � The references inside each array element live in contiguous memory, but the objects that these references refer to almost certainly do not. � When you copy an array of references, you get two arrays whose corresponding references two arrays whose corresponding references refer to the same objects. Array Methods � Sort – sorts the elements in an array of rank 1 k 1 10

  11. 2/22/2007 Array Methods � Clear – sets a range of elements to zero (f (for value types) or null (for reference l t ) ll (f f types) Array Methods � Clone – creates a copy of the array 11

  12. 2/22/2007 Array Methods � GetLength – returns the length of a given di dimension i Array Methods � IndexOf – returns the index of the first occurrence of a value/element, or - 1 if the occurrence of a value/element or if the value/element is not present. Only works on 1-dimensional arrays. 12

  13. 2/22/2007 Methods � A method is a named sequence of statements. � Each method has a name and a body � Each method has a name and a body. � The method body contains the actual statements to be run when the method is called. � The method name should be a meaningful identifier that indicates the overall purpose of the method (CalculateIncomeTax, for example). ( p ) � Most methods can be given some data, which they process, and can return information, which is usually the result of the processing. Specifying the Method Declaration Syntax � The syntax of a Microsoft Visual C# method is as follows: th d i f ll returnType methodName ( parameterList ) { // method body statements go here } 13

  14. 2/22/2007 Specifying the Method Declaration Syntax � The returnType is the name of a type and specifies what kind of information the method returns. This can be the name of any type, such as int or string . If you're writing a f t h i t t i If ' iti method that does not return a value, you must use the keyword void in place of the return type. � The methodName is the name used to call the method. Method names must follow the same identifier rules as variable names. For example, addValues is a valid method name, whereas add$Values is not valid. � The parameterList is optional and describes the types � The parameterList is optional and describes the types and names of the information that the method accepts. You write the parameters between the left and right parentheses. Two or more parameters must separated with commas. returnType methodName ( parameterList ) { // method body statements go here } Important! � C# does not support global methods. You must write all your methods inside a class or your code will not your methods inside a class or your code will not compile. Class Method1 Method2 Method3 14

  15. 2/22/2007 Method: Examples � Here's the definition of a method called addValues that returns an int and has two int parameters called lhs and p rhs . � Here's the definition of a method called showResult that does not return a value and has a single int parameter called answer : Indicates that the method Does not return anything. Writing return Statements � If you want your method to return information (in other words its return type information (in other words, its return type is not void ), you must write a return statement inside the method. � You do this using the keyword return followed by an expression that calculates the returned value and a semicolon the returned value and a semicolon. � The type of expression must be the same as the type specified by the function. 15

  16. 2/22/2007 return Statement Example � The return statement should be at the end of your method because it causes the method to finish. Any statements after the return statement finish. Any statements after the return statement will not be executed (though the compiler will warn you about this problem if you put statements after the return statement). return Statement Example 2 � If you don't want your method to return information (its return type is void ) you can use information (its return type is void ), you can use a variation of the return statement to cause an immediate exit from the method. You write the keyword return , immediately followed by a semicolon. For example: 16

Recommend


More recommend