CMPT 225 - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

CMPT 225

Description:

... of an array, in which case the new operator is not used ... If the array is full new items can't be added to it. There are, time consuming, ways around this ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 51
Provided by: johne78
Category:
Tags: cmpt | fact

less

Transcript and Presenter's Notes

Title: CMPT 225


1
CMPT 225
  • Arrays and Linked Lists

2
Concrete Data Type
  • The term concrete data type is usually used in
    contrast with an Abstract Data Type
  • An ADT is a collection of data and a set of
    operations on the data
  • An ADT is specified without describing its
    implementation (What not how)
  • A concrete data type is an implementation of an
    ADT using some data structure
  • A data structure is a construct that can be
    defined in a programming language to store a
    collection of data

3
Arrays
  • Arrays are sequences of identically typed values
  • Values are stored at specific numbered positions
    in the array
  • The first value is stored at index 0, the second
    at index 1, the ith at index i-1, and so on
  • The last item is stored at position n-1, assuming
    that n values are stored in the array
  • Values are stored sequentially in main memory

4
Arrays in Java
  • To declare an array follow the type with (empty)
    s
  • int grade //or
  • int grade //both declare an int array
  • In Java arrays are objects so must be created
    with the new keyword
  • To create an array of ten integers
  • int grade new int10
  • Note that the array size has to be specified,
    although it can be specified with a variable at
    run-time
  • When the array is created memory is reserved for
    its contents
  • Initialization lists can be used to specify the
    initial values of an array, in which case the new
    operator is not used
  • int grade 87, 93, 35 //array of 3 ints
  • To find the length of an array use its .length
    variable
  • int numGrades grade.length //note not
    .length()!!

5
Array Indexing
  • int arr 3,7,6,8,1,7,2 creates a new
    integer array with seven elements
  • The elements are assigned values as given in the
    initialization list
  • Individual elements can be accessed by referring
    to the array name and the appropriate index
  • int x arr3 would assign the value of the
    fourth array element (8) to x
  • arr5 11 would change the sixth element of
    the array from 7 to 11
  • arr7 3 would result in an error because the
    index is out of bounds

11
error!
6
Arrays and Main Memory
int grade
main memory is depicted below
grade
7
Arrays and Main Memory
int grade grade new int4
main memory is depicted below
grade
8
Arrays and Main Memory
int grade grade new int4 grade2 23
main memory is depicted below
grade
23
9
Memory Addresses
  • It turns out that access to array elements is
    very fast
  • Remember that an array variable references the
    (start of) an array
  • A reference is just a main memory address
  • The address is stored as number, with each
    address referring to one byte of memory
  • Address 0 would be the first byte
  • Address 1 would be the second byte
  • Address 20786 would be the twenty thousand, seven
    hundred and eighty seventh byte
  • and so on

10
Offset Calculations
  • Given something like grade2 23 how do we
    find a particular element in the array?
  • We know the address of the first element in the
    array
  • Because we know the type of the values stored in
    the array, we know the size of each element in
    the array
  • 4 bytes in the case of an int
  • We know which element we want to access
  • We can therefore calculate the address of the
    desired element as being
  • address of first element index size of stored
    type

11
Offset Example
The int stored at grade2 is located at
byte 1282 2 4 1290
grade
Stores a reference to the start of the array, in
this case byte 1282
12
Passing Arrays to Methods
  • Array variables are reference variables
  • When an array variable is passed as an argument
    to a method the method is being given the address
    of an array object
  • Not a new copy of the array object
  • Any changes made to the array in the method are
    therefore made to the original (and only) array
    object
  • If this is not desired, a copy of the array
    should be made within the method

13
Insertion and Removal into Arrays
  • What happens if you want to insert an item at a
    specified position in an existing array?
  • i.e. the position of an item in an array carries
    some meaning e.g.
  • An array that is sorted by name, or grade or some
    other value
  • Or an array where the position corresponds to a
    position of some entity in the world (like books
    on a bookshelf)
  • When an item is added to the middle of the array
    we either have to
  • Write over the current contents at the given
    index (which might not be appropriate) or
  • The item originally at the given index must be
    moved up one position, and all the items after
    that index must shuffled up
  • Similarly when an item is removed from an array
    we either have to
  • Leave gaps in the array, i.e. indices that
    contain no elements, which in practice, means
    that the array element has to be given a special
    value to indicate that it is empty or
  • All the items after the (removed items) index
    must be shuffled down

14
Arrays are Static Data Structures
  • The size of an array must be specified when it is
    created with new and cannot be changed
  • If the array is full new items cant be added to
    it
  • There are, time consuming, ways around this
  • To avoid this problem make arrays much larger
    than they are needed
  • However this wastes space

15
Array Summary
  • Good things about arrays
  • Fast, random access, of elements using a simple
    offset calculation
  • Very memory efficient, very little memory is
    required other than that needed to store the
    contents
  • Easy to use
  • Bad things about arrays
  • Slow deletion and insertion of elements
  • Size must be known when the array is created and
    is fixed (static)

16
Dynamic Arrays
  • A dynamic data structure is one that changes
    size, as needed, as items are inserted or removed
  • The Java ArrayList class is implemented using a
    dynamic array
  • There is usually no limit on the size of such
    structures, other than the size of main memory
  • Dynamic arrays are arrays that grow or shrink as
    required
  • In fact a new array is created when the old array
    becomes full by creating a new array object,
    copying over the values from the old array and
    then assigning the new array to the existing
    array reference variable

17
Dynamic Array
top 4
0
1
2
3
4
5
18
Dynamic Array
insert 5
top 4
0
1
2
3
4
5
19
Dynamic Array
top 5
0
1
2
3
4
5
20
Dynamic Array
top 5
insert 2
0
1
2
3
4
5
21
Dynamic Array
top 6
insert 3
0
1
2
3
4
5
22
Dynamic Array
top 6
insert 3
!The array is full and there is no room for a new
item!
0
1
2
3
4
5
23
Dynamic Array
top 6
insert 3
So we will create a new, bigger array
0
1
2
3
4
5
24
Dynamic Array
top 6
insert 3
So we will create a new, bigger array
0
1
2
3
4
5
0
1
2
3
4
5
6
7
8
9
10
11
25
Dynamic Array
top 6
insert 3
copy the elements of the old array into it
0
1
2
3
4
5
0
1
2
3
4
5
6
7
8
9
10
11
26
Dynamic Array
insert 3
copy the elements of the old array into it
0
1
2
3
4
5
top 6
0
1
2
3
4
5
6
7
8
9
10
11
27
Dynamic Array
insert 3
and finally insert 3 into the new array.
0
1
2
3
4
5
top 7
0
1
2
3
4
5
6
7
8
9
10
11
28
Dynamic Array
The old array will eventually be deleted by
Javas garbage collector
top 7
0
1
2
3
4
5
6
7
8
9
10
11
29
Dynamic Array Summary
  • Before every insertion, check to see if the array
    needs to grow
  • Growing by doubling works well in practice,
    because it grows very large very quickly
  • 10, 20, 40, 80, 160, 320, 640, 1280,
  • Very few array re-sizings must be done
  • To insert n items you need to do ? log(n)
    re-sizings
  • While the copying operation is expensive it does
    not have to be done often

30
Dynamic Array Problems
  • When the doubling does happen it may be
    time-consuming
  • And, right after a doubling half the array is
    empty
  • Re-sizing after each insertion would be
    prohibitively slow
  • Deleting and inserting in the middle is still O(n)

31
A Dream Data Structure
  • It would be nice to have a data structure that is
  • Dynamic
  • Does fast insertions/deletions in the middle
  • We can achieve this using linked lists

32
Linked Lists
  • A linked list is a dynamic data structure that
    consists of nodes and links to nodes
  • A node is an object that contains
  • data
  • a node reference

33
Linked Lists
  • A linked list is a dynamic data structure
    consisting of nodes and links

start
This symbol indicates a null reference
34
Linked Lists
  • Each node contains two things
  • The data
  • A pointer to the next node

35
Linked Lists
  • class Node
  • public int data
  • public Node next

36
Linked Lists
  • class Node
  • public int data
  • public Node next

A node points to another node, so the pointer
must be of type Node
37
Building a Linked List
  • Node a new Node(7, null)

38
Building a Linked List
  • Node a new Node(7, null)
  • a.next new Node(3, null)

39
Building a Linked List
  • Node a new Node(7, null)
  • a.next new Node(3, null)
  • Node p a

a
7
3
40
Building a Linked List
  • Node a new Node(7, null)
  • a.next new Node(3, null)
  • Node p a
  • p p.next // go to the next node

We can walk through a linked list by going from
node to node.
a
7
3
p
41
Building a Linked List
  • Node a new Node(7, null)
  • a.next new Node(3, null)
  • Node p a
  • p p.next // go to the next node
  • p p.next

Eventually, p hits the end of the list and
becomes null.
a
7
3
p
42
Linked List Classes
  • A linked list class requires a reference to the
    head of the list (a class variable of type Node)
  • Some lists have references to the head and the
    tail of the list for convenience
  • Some lists (doubly linked lists) have nodes with
    two references, so that the list can be traversed
    in either direction
  • The linked list class also required methods to
    add and remove nodes from the list
  • Depending on the purpose of the list nodes may be
    added at the front or rear of the list
  • Finally other public methods (e.g. search) should
    be written as required for the particular list
    implementation

43
Appendix 1
  • Arrays in C

44
C Array Variables
  • Like Java, a C array variable contains the
    address of the first element of the array
  • The offset calculations to find individual array
    elements work in exactly the same way
  • Therefore, C arrays have the same advantages
    and disadvantages as Java arrays
  • But C arrays are not objects
  • They are not created with new
  • They do not have any class variables or methods
  • Including the .length variable

45
Appendix 2
  • Java Object Review

46
Objects in Java
  • String s new String("cat")

s
47
Objects in Java
  • String s new String("cat")

The variable s is a reference to the object
s
cat
48
Objects in Java
  • String s new String("cat")
  • S null

Makes s not refer to the object any more
s
49
Objects in Java
  • String s new String("cat")
  • S null

Makes s not refer to the object any more
s
The object gets deleted by Javas automated
garbage collection
50
Objects in Java
  • String s new String("cat")
  • String t s

s
cat
This makes another reference to the object ---
but no new object is created!!
t
Write a Comment
User Comments (0)
About PowerShow.com