Download Arrays and Collections

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

Comparison of programming languages (associative array) wikipedia , lookup

Array data structure wikipedia , lookup

Transcript
Arrays and
Collections
Tonga Institute of Higher Education
Introduction


11
2
35
4
43
0
1
2
3
4
5
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


23
Example: 23, 11, 2, 35, 4, 43
Once you define the size of an array, you cannot change
it.
Array Indexes
0
1
2
3
4
5
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
Creating Arrays

You need to declare and create them
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
Initial values are “”
Or
Brackets used
for setting values
Did not include
size
Size and data
are set here
Demonstration
Creating Arrays
Accessing Data in Arrays

Syntax: <ArrayName>(<Index>)

Setting Values





arrNames(0) = “Tevita”
arrAges(3) = 5
arrNames(10) = 3
arrNames(2) = 5
Error: Index is too big.
Error: Array can only contain letters.
Getting Values


MessageBox.Show(arrNames(0))
Dim Age as Integer = arrAges(3)
Demonstration
Accessing Data in Arrays
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
Demonstration
Array Object
Other Collections
Arrays are the most commonly used data
structure
 There are other classes that implement
different data structures

 ArrayList
 SortedList
 Queue
 Stack
 HashTable
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
Demonstration
ArrayList
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.
Demonstration
SortedList
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.
Queue Code
Demonstration
Queue
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
Stack Code
Demonstration
Stack
HashTable


A collection of values organized by key
Hashtables are very good at finding data
Demonstration
HashTable
For…Each Statement

Some collections allow you to get each item in
the collection
 Array
 ArrayList
 SortedList
 Others
Demonstration
For…Each Statement