1 / 140

INF160 IS Development Environments AUBG, COS dept, Fall semester 2011

asta
Download Presentation

INF160 IS Development Environments AUBG, COS dept, Fall semester 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INF160IS Development EnvironmentsAUBG, COS deptReference books:Baltzan Paige, Business Driven Information Systems, McGraw-Hill/Irwin, 3e, 2012.Doar Matthew B., Practical Development Environments, O’Reilly, 2005.Any C++, C#, Java, VBasic book available in AUBG libraryCourse lecturer: Assoc. Prof. Stoyan Bonev, PhD

  2. INF160 IS Development Environments AUBG, COS dept, Fall semester 2011 Lecture 11 Title: Data Collections: Arrays (presented topic by topic) (Extract from Syllabus)

  3. Lecture Contents: • One-Dimensional arrays • Examples in VB, C++, C#, Java • Two-Dimensional arrays • Examples in VB, C++, C#, Java • Multi-Dimensional arrays • Examples in VB, C++, C#, Java • Jagged/Ragged arrays • Examples in VB, C++, C#, Java • Associative arrays • Examples in VB, C++, C#, Java

  4. Demo Programs: Group name: Test160bArrays • Test160bArraysVB • Test160bArraysCPP • Test160bArraysC# • Test160bArraysJava

  5. Data Collections vs. Simple Data Types Simple data type or scalar data type: Data type used to store a single value. Data collection or data structure: Composite of related data items stored in memory under the same name.

  6. Arrays Array: collection of group of data variables of same type, sharingthe same name for convenience - Easy to search and manipulate - Elements in an array are always numbered 0 through N-1, where N is the total number ofelements, called the size or length of array - Position of an element in an array is called the element index or subscript - Array of values are arranged in consecutive memorylocations

  7. Arrays: How to declare/define • .

  8. How is it in VB? Dim a(6) As Integer ‘ array named a with size of 7 Dim b(9) As Single ‘ array named b with size of 10 Dim c(19) As Character ‘ array named c with size of 20 ‘ c names a collection of 20 char data items ‘ where 20 symbols (char values) may store

  9. How is it in C++? • How to define (declare) an array: int a[6]; float b[10]; char c[20]; bool d[44];

  10. How is it in C#? Source:http://msdn.microsoft.com/en-us/library/0a7fscd0(v=vs.80).aspx You can declare an array of five integers as in the following example: int[] array = new int[5]; This array contains the elements from array[0] to array[4]. The new operator is used to create the array and initialize the array elements to their default values. In this example, all the array elements are initialized to zero. An array that stores string elements can be declared in the same way. For example: string[] stringArray = new string[6];

  11. How is it in C#? Declaration of array In C#, declaration of array is a reference declaration, no allocation of memory to hold array elements. No actual array created yet. int[] data; Create array using new operator data = new int[10]; Put together declaration and creation: int[] data = new int[100]; To access each element, use subscript or index, e.g. data[i], where i should be in the range 0 through Length-1

  12. How is it in Java? • A

  13. Similar to C# Declaring and creating arrays • Example for array objects: int[] myArray; // declaration • This declaresmyArray to be an array reference variable • It does not create an array—it only provides a place to put an array. • Notice that the size is not part of the type myArray = new int[10]; // definition • The new int[10]creates the array of size 10 • Write as int[] myArray = new int[10];// combined

  14. An array’s size is not part of its type • When you declare an array, you declare its type; you mustnot specify its size • Example: String[] names; • or String names[];//not recommended • When you create the array, you allocate space; you must specify its size • Example:names = new String[50]; • This is true even when the two are combined. Example: String[] names = new String[50];

  15. Arrays of objects • Suppose you declare and create an array of objects: • Person[] people = new Person[20]; • You have created an array named people, but you have not yet given values to each element in that array • There is nothing wrong with this array, but • it has 20 references to Persons in it • all of these references are initially null • you have not yet defined 20 Persons • For example, people[12].name will give you a nullPointerException if you do not define the Persons.

  16. Here’s one way to initialize an array of objects: • Person[] people = new Person[20]; • for (int i = 0; i < people.length; i++) { people[i] = new Person(“John");} • This approach has a slight drawback: all the array elements have similar values! • Remember: index of array elements starts at 0 and ends at (length – 1)

  17. The following is legal: • int[] myArray = new int[10]; • ...and later in the program, • myArray = new int[500]; // legal! • This is legal because the array’s size is not part of its type, but part of its value

  18. Length of an array • Arrays are objects. • Every array has an instance constant, length, that tells how large the array is • Example: • for (int i = 0; i < scores.length; i++) System.out.println(scores[i]); • Use of length is always preferred over using a literal constant such as 10 • Arrays have a lengthvariable, • Strings have a length()method

  19. Arrays: How to initialize • .

  20. How is it in VB? ‘ size matches the list of initializers Dim a(6) As Integer = { 2, 3, 5, 7, 11, 13, 17 } ‘ size omitted Dim a() As Integer = { 2, 3, 5, 7, 11, 13, 17 } ‘ size is greater than the list of initializers Dim a(6) As Integer = { 2, 3, 5, 7 }

  21. How is it in C++? How to initialize an array: // size matches the list of initializers int prime1[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; // size omitted int prime2[ ] = { 2, 3, 5, 7, 11, 13 }; // size is greater than the list of initializers int prime3[20] = { 2, 3, 5, 7 };

  22. How is it in C#? C# has an easy way to initialize element values while declaring and creating the array int[] a = new int[2] {10, 20}; Or int[] a = new int[] {10, 20}; Length, a public method of System.Array, can be used to get the total number of elements, i.e. size of an array. Lots of other methods in System.Array

  23. Example using System; class ArraySum { public static void Main() { int[] data = new int[] {11,12,13,14,15,16,17}; int sum = 0; double ave; for (int i = 0; i < data.Length; ++i) { sum = sum + data[i]; Console.Write(data[i] + ", "); } ave = sum / (double)(data.Length); Console.WriteLine(“Sum="+sum+" average="+ave); } }

  24. How is it in Java? • A

  25. Array Initializers • Declaring, creating, initializing in one step: double[] myList={1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.

  26. Declaring, creating, initializing Using the Shorthand Notation double[] myList = {1.9, 2.9, 3.4, 3.5}; The above shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

  27. There is a special syntax for giving initial values to the elements of arrays • This syntax can be used in place ofnew type[size] • It can only be used in an array declaration • The syntax is: { value, value, ..., value } • Examples: int[] primes = { 2,3,5,7,11,13,19}; String[] languages = { "Java","C","C++" };

  28. Initializing arrays • To initialize an array of Person: • Person[] people = { new Person(“Elena"), new Person(“Ivan"), new Person(“Katya"), new Person(“Dimitar") }; • Notice that you do not specify the size of the array

  29. Arrays: How to refer array element

  30. How is it in VB? How to reference an array element: Using subscript or index Always integer valued Dim x(10) As Integer, I As Integer = 5 X(4) x(I) x(I+1) x(2*I-3) Valid range of index values: 0 .. 10 / 0 .. Size/

  31. How is it in C++? How to reference an array element: Using subscript or index Always integer valued int x[10], I=5; X[4] x[I] x[I+1] x[2*I-3] x[I++] Valid range of index values: 0 .. 9 / 0 .. Size-1/

  32. How is it in C#? Array of primitive number types automatically initialized as 0, while bool type is initialized as false. Other types are reference types and initialized to the special value null. Once an array has been declared and created, can assign values to each element int[] a = new int[10]; a[0] a[1] Valid range of index values: 0 .. 9 / 0 .. Size-1/

  33. How is it in Java? • A

  34. Indexed Variables The array elements are accessed through the index. The array indices are 0-based, i.e., it starts from 0 to arrayRefVar.length-1. In the example above, myList holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayRefVar[index];

  35. Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1];

  36. Arrays:How to manipulate array element

  37. How is it in VB? Array element manipulation Given the following: Dim A(10) , i, j, k As Integer i = 7 : j = 2 : k = 4 A(0) = 1 A(i) = 5 A(j) = A(i) + 3 A(j+1) = A(i) + A(0) A(A(j)) = 12

  38. How is it in C++? Array element manipulation Given the following: int A[10] , i, j, k; i = 7 ; j = 2 ; k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12;

  39. How is it in C#? Array of primitive number types automatically initialized as 0, while bool type is initialized as false. Other types are reference types and initialized to the special value null. Once an array has been declared and created, can assign values to each element a[0] = 10; a[1] = 20;

  40. How is it in Java? • Similar to C#

  41. Arrays: as actual arguments

  42. How is it in VB? Passing Arrays to Procedures An array declared in a procedure is local to that procedure. An entire array can be passed to a Sub procedure or a Function procedure as an argument. Remember arrays are pass by reference Passing the array address Remember these points when passing arrays to procedures The formal array argument in a procedure is not itself an array but rather is a name that represents an actual array argument. Therefore in the procedure definition, you need only inform the compiler with () that the actual argument will be an array

  43. Example – compute average of group of numbers Example 7.2 - 4, File Lecture42aSchneider Dim score() As Integer = { 85, 92, 75, 68, 84, _ 86, 94, 74, 79, 88} ‘ 10 values txtAverage.Text = CStr(Sum(score) / 10) Function Sum(ByVal s() As Integer) As Integer Dim total, index As Integer total = 0 For index = 0 To s.GetUpperBound(0) - 1 total = total + s(index) Next Return total End Function Function Sum computes the sum of the numbers in the array s. GetUpperBound(0) returns the upper bound of an array.

  44. How is it in C++? • Remember arrays are pass by reference • Passing the array address • Remember these points when passing arrays to functions • The formal array argument in a function is not itself an array but rather is a name that represents an actual array argument. Therefore in the function definition, you need only inform the compiler with [] that the actual argument will be an array

  45. How is it in C#? • A

  46. Passing Array to Method as Parameter C# passes array parameters by reference More efficient to pass reference, rather thanlarge amount of actual data Called method can manipulate the contentsof the array passed from a calling method,which causes side effect: possible “damage” to the original data May use built-in methods to copy arrays.

  47. using System; class MyCopy { public static void Main( ) { int[] data1 = {1, 2, 3, 4, 5, 6, 7}; int[] data2 = {8, 9, 10, 11, 12, 13, 14}; CopyIt(data1, data2); Console.Write("data1:");// Output data1 for (int i = 0; i < data1.Length; ++i) Console.Write(" " + data1[i]); Console.WriteLine(); Console.Write("data2:");// Output data2 for (int i = 0; i < data2.Length; ++i) Console.Write(" " + data2[i]); Console.WriteLine(); // Output data2 } static void CopyIt(int[] from, int[] to) { for (int i = 0; i < from.Length; ++i) to[i] = from[i]; } }

  48. How is it in Java? • A

  49. Passing Arrays to Methods public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } Invoke the method int[] list = {3, 1, 2, 6, 4, 2}; printArray(list); Invoke the method printArray(new int[]{3, 1, 2, 6, 4, 2}); Anonymous array

More Related