Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and Collections Tonga Institute of Higher Education.

Similar presentations


Presentation on theme: "Arrays and Collections Tonga Institute of Higher Education."— Presentation transcript:

1 Arrays and Collections Tonga Institute of Higher Education

2 Introduction An array is a data structure. An array holds data. Definitions  Cell/Element – A box in which you can enter a piece of data.  Index – A number identifying the location of the cell Example: 0, 1, 2, 3, 4, 5  Field - A piece of data Example: 23, 11, 2, 35, 4, 43 Once you define the size of an array, you cannot change it. 23 11 2 35 4 43 0 1 2 3 4 5

3 Array Indexes Index – A number identifying the location of the cell Index starts at 0  If your array has 6 cells, the indexes are from 0 to 5 0 1 2 3 4 5

4 Creating Arrays You need to declare and create them Or Parenthesis means it’s an array Must define the size when you create them. This has 5 cells. Must state what the array will contain Did not include size Size and data are set here Brackets used for setting values Initial values are “”

5 Demonstration Creating Arrays

6 Accessing Data in Arrays Syntax: ( ) Setting Values  arrNames(0) = “Tevita”  arrAges(3) = 5  arrNames(10) = 3  arrNames(2) = 5 Getting Values  MessageBox.Show(arrNames(0))  Dim Age as Integer = arrAges(3) Error: Index is too big. Error: Array can only contain letters.

7 Demonstration Accessing Data in Arrays

8 Arrays are Objects Arrays have properties and methods  Length  Clear  Sort  And others But they must be declared and initialized in the previously covered way

9 Demonstration Array Object

10 Other Collections Arrays are the most commonly used data structure There are other classes that implement different data structures  ArrayList  SortedList  Queue  Stack  HashTable

11 ArrayList An array whose size changes automatically as elements are added and removed Methods make it easier to use the array Index management is simplified

12 Demonstration ArrayList

13 SortedList A collection that sorts the elements based on a value The sort value can be different from the data stored. This is useful if the object stored has many pieces of data to sort by.

14 Demonstration SortedList

15 Queue Queue - A data structure that stores a number of items. The first item entered into a queue is the first item to be removed FIFO – First in First out – Sometimes used to describe a queue Enqueue - The act of adding an item to a queue Dequeue - The act of removing an item from the front of a queue Examples of Queues  Waiting to checkout at a grocery store  The queue for our laser printer  The keyboard remembers the letters you type so even if the computer is busy, they still stay in order when the computer is able to print them.

16 Queue Code

17 Demonstration Queue

18 Stack Stack – A data structure that stores a number of items. The last item entered into a stack is the first item to be removed Push – The act of adding an item to a stack Pop – The act of removing an item from the top of a stack Peek – The act of viewing the value of the item on top of the stack. Note: You cannot see the values for items under the top item. LIFO – Last in First Out – Sometimes used to describe a stack Examples of Stacks  A stack of plates at a cafeteria  A stack of letters

19 Stack Code

20 Demonstration Stack

21 HashTable A collection of values organized by key Hashtables are very good at finding data

22 Demonstration HashTable

23 For…Each Statement Some collections allow you to get each item in the collection  Array  ArrayList  SortedList  Others

24 Demonstration For…Each Statement


Download ppt "Arrays and Collections Tonga Institute of Higher Education."

Similar presentations


Ads by Google