1 / 10

FURTHER PROGRAMMING Lecture 5

FURTHER PROGRAMMING Lecture 5. A list class Using 2D arrays Null pointers again The debugger. Why Arrays. Arrays Some programming languages only provide arrays as a form of list Fast for accessing elements directly

burnsj
Download Presentation

FURTHER PROGRAMMING Lecture 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FURTHER PROGRAMMINGLecture 5 • A list class • Using 2D arrays • Null pointers again • The debugger MSc IT Further Programming Lecture 5

  2. Why Arrays • Arrays • Some programming languages only provide arrays as a form of list • Fast for accessing elements directly • Slow for adding and deleting elements if we want to keep the list in a sorted order • This comes next week • There are lots of alternative ways of storing lists • Algorithms and Data Structures elective module MSc IT Further Programming Lecture 5

  3. A LIST OF ITEMS CLASS • What will the instance variables be? • What will the constructor do? • What methods do we need? See L5IntArray and HoursOfSun for example of a class which contains a list of items in an array MSc IT Further Programming Lecture 5

  4. Size of array • Arrays in Java must be created with a fixed size • Either we refuse to add additional items when the array is full • Or we copy the entire array and recreate a bigger area when required • Our approach will be • To assume that we can realistically create an array that will be big enough • However, always to check that there is room in the array and produce an error message if necessary MSc IT Further Programming Lecture 5

  5. hoursOfSun Points to details 1 3 0 9 8 6 0 ASSIGNING MEMORY TO AN ARRAY • When an array is declared with no values and no size, a small amount of space is allocated to it to hold a ‘pointer’ • The pointer is the address in memory of the full details for the object, and is initially null • Memory is only allocated once the array is initialised hoursOfSun Points nowhere MSc IT Further Programming Lecture 5

  6. NULL POINTER EXCEPTION • You can get a null pointer exception by not initialising the array in the HoursOfSun class • Either by omitting this line in the constructorhoursOfSun = new int [7]; • I then ran the program : error when I try to refer to a speific item in the array in the addEntry method. sunHours[numInList] = number; java.lang.NullPointerException at HoursOfSun.addEntry(HoursOfSun.java:31) at L3cIntArray.main(L3cIntArray.java:46) MSc IT Further Programming Lecture 5

  7. We will not use • We will not be using these java classes in Further Programming • Array : methods which allow you to create arrays and get and set the elements • Arrays : methods for sorting and searching • ArrayList : provides methods to add, delete, return values etc • System.arraycopy : probably not required by the coursework • We are teaching arrays from first principles using Java, rather than Java programming MSc IT Further Programming Lecture 5

  8. Multiple arrays • You can have arrays of many dimensions. The next example shows 2D arrays • In object oriented programming, quite often we do not need 2D arrays as these may be better modelled as arrays of objects which are themselves arrays • Here, could have an array of Weeks, where each week is an array of sunshine hours for each day of the week • The above comment is also true for parallel arrays covered later • However, we are covering ‘basic programming concepts’ which is why we are doing parallel arrays and multi-D arrays MSc IT Further Programming Lecture 5

  9. 2 D arrays : notation • Declare a 2D array with the rows first, then the columns int [][] sunshine = new int [4][7]; MSc IT Further Programming Lecture 5

  10. 2D arrays contd • Each element needs to have the row and the column specified sunshine[1][5] = 9; pw.println(sunshine[weekIndex][dayIndex]); • Use nested loops to traverse complete array • Use meaningful indexes e.g. rowIndex, columnIndex, weekIndex, dayIndex rather than iand j. MSc IT Further Programming Lecture 5

More Related