Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Lecture 5

Similar presentations


Presentation on theme: "Introduction to Programming Lecture 5"— Presentation transcript:

1 Introduction to Programming Lecture 5
Msury Mahunnah, Department of Informatics, Tallinn University of Technology

2 Storing set of data Arrays ArrayLists

3 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.

4 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!

5 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!!!

6 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

7 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

8 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)

9 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 A.GetUpperBound(2) - > 4

10 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”

11 Initializing Arrays – real example
Dim Matrix ( , ) As Integer = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}} Or {{10, 20, 30}, _ {40, 50, 60}, _ {70, 80, 90}} 1 2 10 20 30 40 50 60 70 80 90

12 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

13 Preserve example To enlarge Vector: Dim Vector() As Byte
Redim Vector(7) To enlarge Vector: ReDim Preserve Vector (12) Or ReDim Preserve (Vector.GetUpperBound(0)+5)

14 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}

15 An additional possibility to store data ...
The ArrayList Collection ... ... is a dynamical structure Advantages: Adding new items simply Removing items (by index and also by value) Storing there different type of values More convinient - grow automatically as you add elements

16 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)

17 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()


Download ppt "Introduction to Programming Lecture 5"

Similar presentations


Ads by Google