y.j.f.cartwrightstaffs.ac.uk - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

y.j.f.cartwrightstaffs.ac.uk

Description:

This would be a two-dimensional jagged array with 2 rows, the first of which can ... Jagged arrays are very flexible but have a performance penalty if all you want ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 26
Provided by: gawainSoc
Category:

less

Transcript and Presenter's Notes

Title: y.j.f.cartwrightstaffs.ac.uk


1
Data Structures
Staffordshire University Faculty of
Computing, Engineering and Technology
  • Arrays
  • Multi-dimensional arrays
  • Arrays of objects
  • The List abstract data type

2
Arrays
  • All programming languages have a simple way to
    store related elements of the same type - the
    array.
  • Different languages do arrays in different ways.
  • This is particularly true for web languages.
  • In most 3GLs, an array has a fixed
  • name
  • type
  • length
  • All of these must be declared when it is created.
  • C arrays follow the same syntax as C, C and
    Java.

3
Example array
  • myArray has room for 8 elements.
  • All elements are of type int.
  • The elements are accessed by their index.
  • In C, array indices start at 0.

4
Declaring arrays
  • int myArray
  • declares myArray to be an array of integers.
  • myArray new int8
  • sets up 8 integer-sized spaces in memory,
    labelled myArray0 to myArray7.
  • int myArray new int8
  • combines the two statements in one line.

5
Arrays
  • We refer to the array elements by their index to
    store values in them
  • myArray0 3
  • myArray1 6
  • myArray2 3 etc.
  • We can create and initialise in one step
  • int myArray 3, 6, 3, 1, 6, 3, 4, 1
  • Although compact, there are very few practical
    uses of creating and initialising arrays in this
    way.

6
Looping through arrays
  • for loops are useful when dealing with arrays
  • for (int i 0 i lt myArray.Length i)
  • Console.WriteLine("Please enter a number ")
  • myArrayi Convert.ToInt32(Console.ReadLine())
  • for (int i 0 i lt myArray.Length i)
  • Console.WriteLine("Element " i " "
    myArrayi)

7
Multi-dimensional arrays
  • So far, our arrays have one dimension.
  • We have seen this pictorially in the previous
    slides as a row of slots or elements.
  • For two-dimensional arrays, you have rows and
    columns of elements (a grid).
  • For three-dimensional arrays, you have multiple
    layers of rows and columns (a 3D grid).
  • For four-dimensional arrays? Well, you can have
    them but they are hard to picture!

8
Multi-dimensional arrays
  • In C, there are actually two different ways of
    specifying a multi-dimensional array.
  • For 2D, the standard approach is to do something
    like
  • int myArray new int
  • int myArray new int2 // array has 2
    rows
  • int myArray new int3 // not valid!
  • int myArray new int23 // 2 rows, 3
    columns
  • This has a similar syntax to the one-dimensional
    array.
  • This is known as a jagged array (or array of
    arrays) where each row can have a different
    number of elements.

9
Jagged arrays
  • After you declare a jagged array, you have to
    then define how many elements a particular row
    has before you can use it
  • int myArray int
  • myArray0 new int7
  • myArray1 new int5
  • This would be a two-dimensional jagged array with
    2 rows, the first of which can store 7 integers
    and the second row stores 5 integers.
  • Jagged arrays are very flexible but have a
    performance penalty if all you want is a simple
    2D grid of elements.

10
Rectangular arrays
  • When all you want is a standard 'grid' of
    elements (in any arbitrary number of dimensions),
    you can use a rectangular array instead.
  • int, myArray new int2,3
  • This creates a two-dimensional array which has 2
    rows containing 3 elements each.
  • myArray1,2 refers to the second row and third
    column along.
  • These types of arrays are simpler to use and
    faster to run than jagged arrays but have no
    flexibility to their sizes.

11
Arrays of objects
  • So far, we have looked at an array of primitive
    types
  • integers.
  • could also use doubles, floats, characters
  • We often want to have an array of objects
  • Students, Books, Loans etc.
  • We need to follow 3 steps.
  • Declare the array
  • private Student studentArray
  • this declares studentArray to be an array of
    Student objects

12
Arrays of objects
  • Create the array
  • studentArray new Student10
  • this sets up 10 spaces in memory that can hold
    references to Student objects.
  • Create Student objects and add them to the array
  • studentArray0 new Student(Yvan",
    "Computing")

13
Arrays of objects
  • Once we have declared, initialised and filled our
    array of objects, we can call the objects'
    methods as usual.
  • Note we use the array index to identify which
    object we are invoking the method on.
  • studentArray4.setProgram("Multimedia")
  • studentArray6.incrementLevel( )
  • Console.WriteLine(studentArray3.ToString())

14
Looping through arrays of objects
  • for loops are also useful when dealing with
    arrays of objects.
  • for (int i 0 i lt studentArray.Length i)
  • Console.WriteLine("Name? ")
  • String name Console.ReadLine()
  • Console.WriteLine(Program? ")
  • String program Console.ReadLine()
  • studentArrayi new Student(name, program)
  • for (int i 0 i lt studentArray.Length i)
  • Console.WriteLine(studentArrayi.ToString())

15
List - An abstract data type
  • In computing (and in real life) we often use
    lists.
  • A list is a group of items arranged in some
    order
  • groceries to buy
  • names in a phone book
  • students in tutorial group
  • Lists are often modelled using an array
  • there are other ways of modelling lists, such as
    trees and linked lists which we haven't covered
    yet
  • or you could use paper!

16
List - An abstract data type
  • There are certain operations we would like to be
    able to carry out on a list
  • These operations should not depend on how the
    List is actually modelled in code form hence
    abstract.
  • insert a new value at the end of the list
  • output the list in order
  • insert a new value into a given position, not
    necessarily at the end
  • delete a data value
  • search for a given data value
  • sort the list by some criterion (for example,
    into alphabetical order)

17
Modelling a list with an array
  • We need to create an array to hold all the list
    elements.
  • This array will need to be
  • big enough for the likely maximum size of the
    list
  • but not so big that it wastes space
  • We need to keep track of the next empty array
    element.
  • this also equals the current number of list
    elements
  • We need to store the maximum size of the array.
  • this is already done for us as the Length
    attribute of the array class provides this.
  • notice that this implies that an array (of any
    type) is really a C class.

18
Lists and arrays
  • This array has room for 8 integers
  • therefore, myArray.Length has the value 8
  • It now contains 4 integers.
  • The next available space is position 4

19
Create a list with a given size
  • We need a constructor that makes use of a
    user-defined parameter to set the size of the
    array.
  • public IntegerList(int size)
  • myArray new intsize
  • next 0

20
Adding to the end of a list
  • We need to
  • store the new data value (8 in this example) in
    position next
  • increase next by 1

21
Adding to the end of a list
  • We need to check first that the array is not
    already full
  • this is the case when next myArray.Length
  • if the array is full
  • display an appropriate message
  • do not add the new element

22
Adding to the end of a list
  • public bool add(int newData)
  • if (next lt myArray.length) // make sure array
    not full
  • myArraynext newData
  • next
  • return true // indicate success
  • else
  • return false // indicate new data not added

23
Printing out the list
  • Remember the array might not be full!
  • Loop from element 0 to next - 1
  • public void printList()
  • Console.WriteLine("List follows ")
  • for (int i0 i lt next i)
  • Console.WriteLine(i " " myArrayi)

24
Lists of objects
  • You can write methods to deal with lists of
    objects similar to our examples of lists of
    integers
  • public bool addStudent(Student s)
  • public void printStudentList()
  • Put these methods in a suitably named class (e.g.
    School).
  • Store the list in an array, which is a private
    attribute of the class.
  • private Student studentArray
  • Be sure to create the array in the class
    constructor
  • public School(int capacity)
  • can then create an array of size capacity in this
    constructor

25
Tutorial work
  • Implement a class which maintains a list of
    integers
  • use methods from this lecture.
  • write a main method to test.
  • Implement a School class which has a list of
    Students
  • adapt methods from this lecture.
  • write a SchoolApp class with a main method for
    testing purposes.
Write a Comment
User Comments (0)
About PowerShow.com