22
2002 Prentice Hall. All rights reserved. 1 Nizovi Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays 7.4.1 Allocating an Array 7.4.2 Initializing the Values in an Array 7.4.3 Summing the Elements of an Array 7.4.4 Using Arrays to Analyze Survey Results 7.4.5 Using Histograms to Display Array Data Graphically 7.5 Passing Arrays to Procedures 7.6 Passing Arrays: ByVal vs. ByRef 7.7 Sorting Arrays 7.8 Searching Arrays: Linear Search and Binary Search 7.8.1 Searching an Array with Linear Search 7.8.2 Searching a Sorted Array with Binary Search 7.9 Multidimensional Rectangular and Jagged Arrays 7.10 Variable-Length Parameter Lists 7.11 For Each/Next Repetition Structure

Visual basic - lekcija nizovi

Embed Size (px)

DESCRIPTION

lekcija nizovi iz Visual basic-a

Citation preview

Page 1: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

1

Nizovi

Outline7.1 Introduction7.2   Arrays7.3   Declaring and Allocating Arrays7.4   Examples Using Arrays

7.4.1  Allocating an Array7.4.2  Initializing the Values in an Array7.4.3  Summing the Elements of an Array7.4.4  Using Arrays to Analyze Survey Results7.4.5  Using Histograms to Display Array Data

Graphically7.5   Passing Arrays to Procedures7.6   Passing Arrays: ByVal vs. ByRef7.7   Sorting Arrays7.8   Searching Arrays: Linear Search and Binary Search

7.8.1  Searching an Array with Linear Search7.8.2  Searching a Sorted Array with Binary Search

7.9   Multidimensional Rectangular and Jagged Arrays7.10   Variable-Length Parameter Lists7.11   For Each/Next Repetition Structure

Page 2: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

2

1. Uvod

• Nizovi(Arrays)– Struktura podataka koja se sastoji od kolekcije elemenata

istog tipa

– “Statički” entiteti• Zadržavaju dimenziju koja im je dodijeljenja pri kreiranju

Page 3: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

3

2 Nizovi

• Nizovi– Grupa kontinualno zauzetih memorijkih lokacija istog tipa

koji se pristupa preko zajedničkog imena

• Index– Vrijednost koja identifikuje lokaciju unutar niza– Prvi element niza je nulti element

• Length svojstvo– Svaki niz uVisual Basic'u “zna” svoju dimeniziju putem Length svojstva

• GetUpperBound method– Returns the index of the last element in the array– The value returned by this GetUpperBound is one less

than the value of the array’s Length property

Page 4: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

4

2 Nizovi

Fig. 7.1 Array consisting of 12 elements.

-45numberArray(0)6numberArray(1)0numberArray(2)72numberArray(3)1543numberArray(4)-89numberArray(5)0numberArray(6)62numberArray(7)-3numberArray(8)1numberArray(9)

6453numberArray(10)78numberArray(11)

Name of array (note that all elements of this array have the

same name, numberArray)

Position number (index or subscript) of the element within array numberArray

Page 5: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

5

3 Alociranje i dealociranje memorije za nizove

• Memorija– Količina memorije neophodna za smještanje elemenata niza zavisi

od dimenzije niza kao i od tipa podatka samih elemenata niza

• Ključna riječ New– Upotrebljava se za specificiranje dimenzije niza kao i za alokaciju

memorije

• Granice niza – Određuju koji indeksi se mogu koristiti za pristup elementima niza

• Lista za inicijaliciju– Specificira početnu vrijednost elemenata niza

• Ključna riječ Nothing– Označava da je pokazivač nedefinisan

Page 6: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

6

3 Alociranje i dealociranje memorije za nizove

• Dim number as Integer()– Obične zagrade poslije ključne riječi Integer označavaju da se radi

o nizu

– Memorija za niz nije alocirana još uvijek već je samo alocirana referenca na elemente niza

• number = New Integer(11){}– Alociranje memorije potrebne za smještanje elemenata niz koristi

se New ključna riječ jezika(slično kao kod objekata)

– Ova komanda alocira 11 elementa tipa Integer

• number = New Integer(){1, 2, 4, 6} – Alocira niz za četiri elementa i pri tome ih inicijalizuje

Page 7: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

7

4 Primjeri upotrebe nizova

• Nekoliko primjera koji demonstriraju– Deklaraciju

– Alokaciju

– Inicijalizaciju elemenata niza

Page 8: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline8

InitArray.vb

Page 9: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

9

5 Sortiranje Nizova

• Sortiranje– Sortiranje podataka je jedna od najčešćih računarskih

operacija

– Nekada, najjednostavniji algoritmi imaju loše performanse

• Bubble Sort (a.k.a. sinking sort)– Elementi niza sa manjom vrijednošću "isplivavaju" na

početak niza.

– Slično, elementi niza sa većom vrijednošću "tonu" na kraj niza.

– Lak za programiranje ali rezultira lošim performansama.

Page 10: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline10

BubbleSortTest.vb

1 ' Fig. 7.11: BubbleSortTest.vb2 ' Program creates random numbers and sorts them.3 4 Imports System.Windows.Forms5 6 Public Class FrmBubbleSort7 Inherits System.Windows.Forms.Form8 9 ' buttons10 Friend WithEvents cmdCreate As Button11 Friend WithEvents cmdSort As Button12 13 ' labels14 Friend WithEvents lblOriginal As Label15 Friend WithEvents lblSorted As Label16 17 ' textboxes18 Friend WithEvents txtOriginal As TextBox19 Friend WithEvents txtSorted As TextBox20 21 ' Visual Studio .NET generated code22 23 Dim array As Integer() = New Integer(9) {}24 25 ' creates random generated numbers26 Private Sub cmdCreate_Click(ByVal sender As System.Object, _27 ByVal e As System.EventArgs) Handles cmdCreate.Click28 29 Dim output As String30 Dim randomNumber As Random = New Random()31 Dim i As Integer32 33 txtSorted.Text = ""34

Page 11: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline11

BubbleSortTest.vb

35 ' create 10 random numbers and append to output36 For i = 0 To array.GetUpperBound(0)37 array(i) = randomNumber.Next(100)38 output &= array(i) & vbCrLf39 Next40 41 txtOriginal.Text = output ' display numbers 42 cmdSort.Enabled = True ' enables cmdSort button 43 End Sub ' cmdCreate_Click44 45 ' sorts randomly generated numbers46 Private Sub cmdSort_Click(ByVal sender As System.Object, _47 ByVal e As System.EventArgs) Handles cmdSort.Click48 49 Dim output As String50 Dim i As Integer51 52 ' sort array53 modBubbleSort.BubbleSort(array)54 55 ' creates string with sorted numbers56 For i = 0 To array.GetUpperBound(0)57 output &= array(i) & vbCrLf58 Next59 60 txtSorted.Text = output ' display numbers61 cmdSort.Enabled = False62 End Sub ' cmdSort_Click63 64 End Class ' FrmBubbleSort

Page 12: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline12

BubbleSortTest.vb

Page 13: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

13

6. Pretraživanje nizova: Linearno i binarno pretraživanje

• Pretraživanje – Proces lociranja određenog elementa u nizu

• Linearno pretraživanje– Jednostavna tehnika

– Obično je primjenjuje za male nizove i nizove koji nijesu sortirani

– U prosjeku, broj poređenja jednak je polovini broja elemenate u nizu

• Binarno pretraživanje– Ako je niz sortiran može se primijeniti binarno pretraživanje koje

je efikasnija, ali i složenija tehnika u odnosu na linearno pretraživanje

– Poslije svakog poređenja eliminiše se polovina broja elemenata koji su preostali u nizu

– Ima logaritamsku složenost

Page 14: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline14

LinearSearchTest.vb

1 ' Fig. 7.13: LinearSearchTest.vb2 ' Linear search of an array.3 4 Imports System.Windows.Forms5 6 Public Class FrmLinearSearchTest7 Inherits System.Windows.Forms.Form8 9 ' buttons10 Friend WithEvents cmdSearch As Button11 Friend WithEvents cmdCreate As Button12 13 ' text boxes14 Friend WithEvents txtInput As TextBox15 Friend WithEvents txtData As TextBox16 17 ' labels18 Friend WithEvents lblEnter As Label19 Friend WithEvents lblResult As Label20 21 ' Visual Studio .NET generated code22 23 Dim array1 As Integer() = New Integer(19) {}24 25 ' creates random data 26 Private Sub cmdCreate_Click(ByVal sender As System.Object, _27 ByVal e As System.EventArgs) Handles cmdCreate.Click28 29 Dim output As String30 Dim randomNumber As Random = New Random()31 Dim i As Integer32 33 output = "Index" & vbTab & "Value" & vbCrLf34

Page 15: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline15

LinearSearchTest.vb

35 ' creates string containing 11 random numbers36 For i = 0 To array1.GetUpperBound(0)37 array1(i) = randomNumber.Next(1000)38 output &= i & vbTab & array1(i) & vbCrLf39 Next40 41 txtData.Text = output ' displays numbers 42 txtInput.Text = "" ' clear search key text box43 cmdSearch.Enabled = True ' enable search button44 End Sub ' cmdCreate_Click45 46 ' searches key of element 47 Private Sub cmdSearch_Click(ByVal sender As System.Object, _48 ByVal e As System.EventArgs) Handles cmdSearch.Click49 50 ' if search key text box is empty, display 51 ' message and exit procedure52 If txtInput.Text = "" Then53 MessageBox.Show("You must enter a search key.")54 Exit Sub55 End If56 57 Dim searchKey As Integer = Convert.ToInt32(txtInput.Text)58 Dim element As Integer = LinearSearch(searchKey, array1)59 60 If element <> -1 Then61 lblResult.Text = "Found Value in index " & element62 Else63 lblResult.Text = "Value Not Found"64 End If65 66 End Sub ' cmdSearch_Click67 68 End Class ' FrmLinearSearch

Page 16: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline16

LinearSearchTest.vb

Page 17: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

17

7 Višedimenzionalni, Dvodimenzionalni i "Jagged" nizovi

• Višedimenzionalni nizovi (multiple-subscripted)– Potrebno je dva ili više indeksa da bi se pristupilo

određenom elementu niza

• Dvodimenzionalni nizovi– Dva indeksa, jedan identifikuje vrstu, a drugi kolonu

• "Jagged" nizovi– Predstavljaju nizove nizova

– Vrste mogu biti nizovi promjenljive dužine

Page 18: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

18

7 Višedimenzionalni, Dvodimenzionalni i "Jagged" nizovi

Fig. 7.15 Two-dimensional array with three rows and four columns.

a(0, 2) a(0, 3)a(0, 1)a(0, 0)

a(1, 2) a(1, 3)a(1, 1)a(1, 0)

a(2, 2) a(2, 3)a(2, 1)a(2, 0)

Column 0 Column 1 Column 2 Column 3

Row 0

Row 1

Row 2

Column index

Row index (or subscript)

Array name

Page 19: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline19

JaggedTest.vb

Page 20: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline20

BubbleSortTest.vb

Page 21: Visual basic - lekcija nizovi

2002 Prentice Hall. All rights reserved.

21

7.11 For Each/Next iterativna strukutura

• For Each/Next– Omogučava pristup elementima strukture podataka kakav je

niz

– Umjesto brojača, For Each/Next koristi varijablu koja prestavlja jedan element niza

– Koristi se uglavnom kada index elementa niza nije bitan

– Ovakav mehanizam posebno je koristan kada se radi o nizu objekata

Page 22: Visual basic - lekcija nizovi

2002 Prentice Hall.All rights reserved.

Outline22

BubbleSortTest.vb