Download Dim

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Dimension wikipedia , lookup

Transcript
Introduction to Programming
Lecture 5
Msury Mahunnah,
Department of Informatics,
Tallinn University of Technology
Storing set of data
 Arrays
 ArrayLists
Variable vs Array
 Variable consists of a cell to keep (store) the value, provided
with the name
 Array consists of cells to keep (store) the values, provided
with the name and the indexes.
Why?
 Let’s set up an assignment to find maximum salary among the 5
employee.
Dim salary1%, salary2%, salary3%, salary4%, salary5%
or
Dim salaries%(4)
 Example on the whiteboard!
Main properties
1. Name
For names, same rules as variables
2. Dimensions
It will determine how the items are organised?
3. Boundaries of indexes
Boundaries for every dimension
4. Dynamism
Static memory (Dim) and Dynamic memory (ReDim)
Dim – allocate memory before the work of a program
Redim – allocate memory during the program work
5. Type
Integer, String, Date, Boolean, ...
Every property is determined directly or indirecty with an array declaration!!!
Main properties - example
Dim A(5) As Integer
Name – A
Dimesions – 1 (how many boundaries do we have?)
Boundaries of indexes – (for first dimension) 0 to 5
Dynamism – static (Dim)
Type – Integer
Main properties - example
Dim B( , ) As String
Redim B(3, 7)
Name – B
Dimesions – 2 (how many boundaries do we have?)
Boundaries of indexes
for first dimension – 0 to 3
for second dimension – 0 to 7
Dynamism – dynamical (ReDim)
Type – String
Declaring a dynamic array
 At first declare it as usual with Dim statement (or Public or
Private), but do not specify boundaries for its dimensions:
Dim DynArray() As Integer
 Later in the program, when you know how many elements you
want to store in the array, use the ReDim statement:
ReDim DynArray(UserCount)
Declaring a dynamic array
Dim A( , , ) As Integer
ReDim A(3, 7, 4)
How to know some essentials features?
Dimensions in array
A.Rank - > 3
Total elements in array
A.Length - > 160
Elements in first dimension
A.GetLength(0) -> 4
Elements in second dimension
A.GetLength(1) -> 8
Elements in third dimension
A.GetLength(2) -> 5
Last index in the first dimension
A.GetUpperBound(0) - > 3
Last index in the second dimension
A.GetUpperBound(1) - > 7
Last index in the second dimension
A.GetUpperBound(2) - > 4
Initializing Arrays
 General constructor:

Dim arrayname() As type = {entry0, entry1, ...
…, entryN}
 Two ways to do this (real example):

Dim Names() As String = {“Peter”, “Kathy”}

Dim Names(1) As String


Names(0)=“Peter”
Names(1)=“Kathy”
Initializing Arrays – real example
Dim Matrix ( , ) As Integer =
{{10, 20, 30}, {40, 50, 60}, {70, 80, 90}}
Or
Dim Matrix ( , ) As Integer =
{{10, 20, 30}, _
{40, 50, 60}, _
{70, 80, 90}}
0
1
2
0
10 20 30
1
40 50 60
2
70 80 90
Repeated ReDim declaration
 Each time you execute the ReDim statement, all the
values currently stored in the array are lost!!!
Q: But If I need to do it for enlarging an array?
A: Use keyword Preserve
Preserve example
Dim Vector() As Byte
Redim Vector(7)
 To enlarge Vector:
ReDim Preserve Vector (12)
Or
ReDim Preserve (Vector.GetUpperBound(0)+5)
Initializing array with random number
Const max%=3, a%=1, b%=9
‘random numbers from 1 to 9
Dim V%(max), i%
‘Before (for cycle) -> V={0, 0, 0, 0}
For i=0 to max
V(i)=Int(Rnd()*(b-a+1)-a)
Next i
‘After (for cycle) - > V = {6, 2, 5, 8}
An additional possibility to store data ...
The ArrayList Collection ...
... is a dynamical structure
Advantages:
1. Adding new items simply
2. Removing items (by index and also by value)
3. Storing there different type of values
4. More convinient - grow automatically as you add elements
The ArrayList Collection
 Declaration:
Dim aList As New ArrayList
 Adding items:
Dim words() As String = {"Just", "a", "few", "words"}
aList.Add(words): aList.Add("Hello")
aList.Add(777): aList.Add(True)
The ArrayList Collection
 First item index (sequence number) is 0
 Removing items:
By value:
aList.Remove(“Hello”)
By index:
aList.RemoveAt(2)
All items at the same time:
aList.Clear()