1 / 20

CS0004: Introduction to Programming

CS0004: Introduction to Programming. Arrays. Arrays. So far we have been using simple variables . Simple variables hold only one value. Dim x As String = “Banana” x only has one value: “Banana” Arrays are indexed lists of simple variables of the same type

gannon
Download Presentation

CS0004: Introduction to Programming

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. CS0004: Introduction to Programming Arrays

  2. Arrays • So far we have been using simple variables. • Simple variables hold only one value. Dim x As String = “Banana” • x only has one value: “Banana” • Arrays are indexed lists of simple variables of the same type • Indexed here means we can access each simple variable by its index. Indices are usually numbers for arrays. • In other words, arrays hold multiple values (complex variables) • Arrays allow programmers to greatly simplify their code by logically organizing similar data into one entity.

  3. Arrays • Imagine the following problem: • You want to take in the grades of 30 students and display which ones had above average scores. • The algorithm could look as follows: • Take in the grades of 30 students with their names • Find the average • Display which students scored above the average • What is the problem here with using simple variables?

  4. Arrays • Instead of using separate simple variables for the names and scores of the students, we can use arrays. Dim students(29) As String Dim grades(29) As Double • This is declaring two arrays each holding 30 elements (or subscripted variables) one holding values of type String, one of type Double • Elements(or subscripted variables) are the individual simple variables in the array. • We can access an individual element by using its index students(0) • This will access the first element of the students array. • 0 in this case is called an index or subscript. • Indexing of arrays starts at 0.

  5. Arrays • General Form of an array declaration: Dim arrayName(n) As DataType • arrayName follows normal naming conventions • n is called the upper bound. • The upper bound of an array is the last index that can be used in the array. The size, the number of elements in the array, is the upper bound plus one. • DataTypeis the data type of the elements of the array. • When an array is declared, the individual elements are located successively in memory. This is good for memory management.

  6. Array Example 1 • New Topics: • Arrays • On Form Load Event Procedure

  7. Array Initialization and Implicit Array Sizing • If you know all the values that are going to be stored in an array at the time you want to create it, you can use implicit array sizing. • Instead of: Dim winners(3) AsString winners(0) = "Packers" winners(1) = "Packers" winners(2) = "Jets" winners(3) = "Chiefs" • You can do: Dim winners() AsString = {"Packers", "Packers", "Jets", "Chiefs"} • The compiler knows that the array has 4 elements implicitly because you specified 4 initial values. • General Form: Dim arrayName() As DataType = {value0, value1, value2, …, valueN}

  8. Array Methods • Assume we have the following array: DimmyNumbers() AsInteger = {4, 1, 6, 3, 9, 5} • Some useful array methods: • myNumbers.Count • Gives the number of elements in the array (6) • myNumbers.Max • Gives the “largest” element of the array (depends on the type of the array) (9) • myNumbers.Min • Gives the “smallest” element of the array (depends on the type of the array) (1) • myNumbers.First • Gives the first element of the array (4) • myNumbers.Last • Gives the first element of the array (5)

  9. Arrays as Parameters • Just like any other variables, arrays can be used as parameters: SubmySubprocedure(ByValmyNumbers() AsInteger) • Here numbers is a parameter that is an array of integers • We can call this subprocedure as such: DimmyNumbers() AsInteger = {4, 1, 6, 3, 9, 5} mySubprocedure(myNumbers)

  10. Arrays and Looping • Arrays have a structure that makes them natural for use in For…Next loops. • With For…Next loops we can iterate over all the elements of an array: DimmyNumbers() AsInteger = {4, 1, 6, 3, 9, 5} For i AsInteger = 0 TomyNumbers.Count- 1 MessageBox.Show(myNumbers(i)) Next • What is the output of this code?

  11. Array Example 2 • New Topics: • Array Initialization and Implicit Array Sizing • Array Methods • Arrays as Parameters • Arrays and Looping

  12. Resizing an Array • So far, when you declare an array, its size is set one way or another Dim winners(3) AsString Dim winners() AsString = {"Packers", "Packers", "Jets", "Chiefs"} • However, you can resize an array after it has been declared ReDimwinners(4) • This would make the array hold 5 values, but DELETE ALL PREVIOUS VALUES STORED in winners. • To retain all values after resizing, use the Preserve keyword: ReDimPreservewinners(4) • As a matter of fact, you do not need to specify the size of an array when you declare it, but you must give it a size with ReDim before using it in any way: Dim winners() AsString ReDim winners(4)

  13. Array Example 3 • New Topic: • Resizing an Array

  14. For Each Loops • For Arrays there is a more intuitive way to loop through the elements called a For Each Loop • We can do the following: DimmyNumbers() AsInteger = {4, 1, 6, 3, 9, 5} For i AsInteger = 0 TomyNumbers.Count - 1 MessageBox.Show(myNumbers(i)) Next • By using a For Each loop: DimmyNumbers() AsInteger = {4, 1, 6, 3, 9, 5} ForEachaNumberAsIntegerIn myNumbers MessageBox.Show(aNumber) Next • What this will do is iterate through all the elements in myNumbers and assign them to aNumber for each iteration. • General Form: ForEach variableNameAsDataTypeIn arrayName statement(s) Next

  15. Array Example 4 • New Topics: • For Each Loop

  16. Character Data Type • So far in this course we often handled single characters as Strings of length 1 DimtheLetterAAsString = "a" • However, there is a data type specifically designed for holding one character called Char DimtheLetterAAsChar = "a"c • Notice the c at the end of the double quotes. • We can convert any String into a character array for easy indexing using the ToCharArray method DimmyStringAsString = "banana" DimmyCharArray() AsChar = myString.ToCharArray

  17. Character Array Example 1 • New Topics: • Character Array

  18. Searching For an Element in an Array • To find the index of a particular element in an array you can use the IndexOf method DimmyNumbers() AsInteger = {4, 1, 6, 3, 9, 5} DimtheIndexAsInteger theIndex = Array.IndexOf(myNumbers, 6) • theIndexwill be assigned the value of 2 because the index of 6 in myNumbers is 2 • General Form: numVar= Array.IndexOf(arrayName, value, startIndex) • numVar-the index of the found value • arrayName- the array to search in • value - the value to search for • startIndex- the index to start looking for • numVarwill be -1 if the value is not found in arrayName.

  19. Copying an Array • Be careful when copying an array. Assume two arrays arrayOneand arrayTwo are declared… arrayTwo = arrayOne • This will copy the reference of arrayOne to arrayTwo. This means that whatever happens to arrayOne will happen to arrayTwo and vice versa. • To copy just the values you can do something like this: For i AsInteger = 0 ToarrayOne.Length - 1 arrayTwo(i) = arrayOne(i) Next

  20. Notes • If you attempt to index into an array that is larger than the size of the array it will cause a run-time error: DimmyNumbers() AsInteger = {4, 1, 6, 3, 9, 5} DimaNumberAsInteger = myNumbers(8) • This will cause an error because there is no element in myNumberswith index 8 • There are many cool things about arrays in VB we will not be able to go over such as: • The split and join methods • LINQ • Two-Dimensional Arrays • There are also many topics that the book goes over that are somewhat related to arrays that we will not be able to go over: • Reading from a text file • Creation of structures

More Related