Upload
cameron-gordon
View
220
Download
2
Tags:
Embed Size (px)
Citation preview
1
Working with Data Structures
Kashef Mughal
2
Chapter 5
Please review on your own
A few terms
.NET Framework - programming model
CLR (Common Language Runtime) - compiles the code written in all the .NET languages
Bottom line - With .NET Microsoft wants to compete with other Object Oriented languages like JAVA, C++
3
Arrays
An array is a group of variables that have the same name and data type and are related in some way
Array is really a list that holds similar data e.g. employee lastnames
Although arrays in Visual Basic .NET can have as many as 60 dimensions, the most commonly used arrays are one-dimensional and two-dimensional
Programmers use arrays to store related data in the internal memory of the computer
4
One-Dimensional Arrays
A one-dimensional array is simply a row (or column) of variables
A two-dimensional array resembles a table in that it has rows and columns
Each element in an array is identified by a subscript or index, which Visual Basic .NET assigns to the variable when the array is created
You refer to an array element by the array’s name followed by the element’s index
5
One-Dimensional Array
Alaska Montana South Carolina Tennessee Texas
Alaska
Montana
South Carolina
Tennessee
Texas
6
Declaring an Array
Basic Syntax accessibility arrayname() As datatype =
{initialValues} accessibility is Dim, Public, or Private These statements create and initialize the array
variables in memory In VB.NET arrays are zero based meaning the first
item in the array has a 0 index
7
Declaring an array - Examples
Dim strCitys(3) As String
Private intNumbers(5) As Integer
Private udtItems(4) As ItemStruc
Private strStates() As String = {“Hawaii”, “Alaska”, “Maine”}
Dim intScores() As Integer = {75, 9, 23, 6}
8
Storing Data in a an Array
You can use a variety of ways to enter data into an array
strMonthArray(0) = “Jan”
strMonthArray(1) = “Feb”
strMonthArray(2) = “Mar”
strMonthArray(3) = “Apr”
strMonthArray(4) = “May”
strMonthArray(5) = “June”
9
Display the Contents of a an Array
You can use For .. Next loop to display an Array
or to search for a value
Dim strMonths() As String = {“JAN”, “FEB”, “MAR”, “APR”,
“MAY”, “JUN”}
Dim intX As Integer
For intX = 0 To strMonths.Length - 1
Me.MonthListBox.Items.Add(strMonths(intX))
Next intX
10
Array Properties and Methods
Clear Sets a range of elements in the Array to zero or to a null reference (Nothing in Visual Basic).
Copy Copies a section of one Array to another Array
Length Returns the size of the array
UBound Returns the upper bound (index) for the given array. Also GetUpperBound is the same
LBound Returns the lower bound (index) for the given array. Also GetLowerBound is the same
Sort Sorts the elements in Array
11
In-class Assignments
Try the following Exercises Pages 212 - 214 Pages 215 - 216 Page 217 - 222 Page 260 Page 263 Take a break when done
12
Misc. Array Topics
The ReDim statement is used to change the size of one or more dimensions of an array that has already been formally declared
The ReDim statement can appear only at procedure level. This means you can redefine arrays inside a procedure but not at class or module level.
Use the Preserve keyword with the ReDim to save the contents of existing array
13
Two-Dimensional Arrays
A two-dimensional array resembles a table in that the variables are in rows and columns
AC24 Shirt Red
BD12 Coat Blue
CP14 Blouse White
strProducts(0, 0) strProducts(0, 2)
strProducts(2, 1)
strProducts(1, 2)
14
How would you initialize a Two-Dimensional Array
Lets say we are working with an array that is 4*2. We could use the following code to initialize the values We are using next For .. Loops here
dim intRow, intColumn as integer
dim intScores
For intRow = 0 To 3
For intColumn = 0 To 1
intScores(intRow, intColumn) = 0
Next intColumn
Next intRow
15
Other Data Structures
Constants Enumerations Structures ArrayList Collections Hashtable
16
Constants
Constants store values that, as the name implies, remain constant throughout the execution of an application.
Using constants allows you to provide meaningful names instead of numbers, making your code more readable.
Examples of constants are values like Pi, location of a file that does not change
Public Const pi As Single = 3.14
17
Enumerations
Enumerations provide a convenient way to work with sets of related constants
In addition they let you associate constant values with names.
For example, you can declare an enumeration for a set of integer constants associated with the days of the week, and then use the names of the days rather than their integer values in your code
The only data types allowed are integer, long, short and byte
18
Structure
You can combine data items of different types to create a structure.
A structure associates one or more members with each other and with the structure itself.
When you declare a structure, it becomes a composite data type, and you can declare variables of that type.
Structures are useful when you want a single variable to hold several related pieces of information. E.g. you might want to keep an employee's name, telephone extension, and salary together
19
In-class Assignments
Try the following Exercises Pages 223-228 Pages 230-232 Page 234 Page 236 Pages 238-245 Take a break when done
20
ArrayList
ArrayList is a form of an array that is easier to use
Think of an ArrayList as a resizable one-dimensional array of objects.
In addition to automatic resizing, ArrayList has a few other advantages over Array.
ArrayList has methods that will add, insert, and remove a range of elements
ArrayList does not need the index like an array does
21
Collections
Collection object gives you a way to refer to a related group of items as a single object.
Collections are used a lot by VB itself (components in a project, for example, are a collection) and are pretty flexible.
The members in a collection don't even have to be the same data type
22
Hashtable
Hashtable is a special kind of collection
The Hashtable collection type stores key-value pairs and allows you to locate items in the collection based on the key
Similar in principle to a primary key in a database table
The advantage is that we do not need an index to locate the item
23
Midterm
On 2/11 Chapter 1-7 In-class material Closed Book 100 questions
true/false multiple choice
200 points 2 hours Grades right now
24
In-class Assignments
Try the following Exercises Page 247 Page 248 Page 251 Page 254 Next week Chapter 7 and Review