Download presentation
Presentation is loading. Please wait.
Published byRosaline Young Modified over 8 years ago
1
ARRAYS II CITS1001
2
Scope of this lecture Arrays of objects Multi-dimensional arrays The Game of Life (next lecture) 2
3
Arrays of objects The arrays we have seen so far had elements of a primitive type int[], double[], boolean[], etc. But an array can also hold objects e.g. Term[] Arrays can also be two-dimensional e.g. int[][] Arrays can also be three-dimensional, or more But we won’t get to that in CITS1001 3
4
Arrays of objects When using an array of primitive type, there are two steps involved Declare the variable to refer to the array Create the space for the array elements using new When using an array of object type, there are three steps involved Declare the variable to refer to the array Create the space for the array elements using new Populate the array with objects by repeatedly using new, often in a loop 4
5
A Student class 5 public class Student { private String studentNumber; private int mark; public Student(String studentNumber, int mark) { this.studentNumber = studentNumber; this.mark = mark; } public String getStudentNumber() { return studentNumber; } public int getMark() { return mark; } A skeleton version of a possible Student class in a student records system
6
Creating a unit list 6 Student[] unitList; unitList = new Student[numStudents]; unitList[0] = new Student(“042371X”,64); unitList[1] = new Student(“0499731”,72); unitList[2] = new Student(“0400127”,55); … unitList[numStudents-1] = new Student(“0401332”,85); Declare Create Populate
7
The three steps 7 unit unit[0]unit[1]unit[2]unit[3]unit[4] null unit null
8
Using arrays of objects Using an array of objects is the same as using any array You just need to remember that the elements are objects! 8 private Student top(Student[] unitlist) { Student top = unitlist[0]; for (Student si : unitlist) if (si.getMark() > top.getMark()) top = si; return top; }
9
Compare with max 9 private int max(int[] a) { int max = a[0]; for (int i : a) if (i > max) max = i; return max; }
10
Method signatures 10 int max(int[] a) Student top(Student[] unitList)
11
Initialisation 11 Student top = unitList[0]; int max = a[0]; Declare a variable to hold the “best so far”, and initialise it to the first element in the array
12
Processing 12 for (Student si : unitList) if (si.getMark() > top.getMark()) top = si; for (int i : a) if (i > max) max = i; Check each element in turn, compare it with the best so far, and update the best if necessary
13
Return 13 return top; return max; Finally return the extreme element – the highest int or the Student with the best mark
14
What if we want to find the highest mark? 14 private int highestMark(Student[] unitlist) { int max = unitlist[0].getMark(); for (Student si : unitlist) if (si.getMark() > max) max = si.getMark(); return max; }
15
Much better to use the existing method! 15 Do not write another looping method! Use the already-written functionality private int highestMark(Student[] unitlist) { return top(unitlist).getMark(); }
16
2D arrays We sometimes need arrays with more than one dimension int[][] a = new int[4][3]; 16 a[0][0] 0 a[0][1] 0 a[0][2] 00 a[3][0] 0 a[3][1] 0 a[3][2] 00 a[2][0] 0 a[2][1] 0 a[2][2] 00 a[1][0] 0 a[1][1] 0 a[1][2] 00 This creates an array with four “rows” and three “columns” The “row” index ranges from 0–3 and the “column” index from 0–2 This creates an array with four “rows” and three “columns” The “row” index ranges from 0–3 and the “column” index from 0–2
17
2D arrays contd. 17 This is really an array where each element is itself an array a is a valid variable, representing the whole thing a[0] is a valid variable, representing the first “row” a[0][2] is a valid variable, representing the last element in the first “row”
18
2D arrays contd. 18 a[0][0] 0 a[0][1] 0 a[0][2] 00 a[1][0] 0 a[1][1] 0 a[1][2] 00 a[3][0] 0 a[3][1] 0 a[3][2] 00 a[2][0] 0 a[2][1] 0 a[2][2] 00 a[0] a[1] a[2] a[3] a
19
2D arrays contd. 19 Given that an array is an object, this is really just a special case of an array of objects The single statement int[][] a = new int[4][3]; declares the array variable a, and both creates and populates the array!
20
2D arrays examples 20 A 2D array is normally processed with two nested for-loops private int sum(int[][] a) { int sum = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) sum = sum + a[i][j]; return sum; }
21
2D arrays examples 21 Suppose we want to return the index of the first row containing any element that is true private int firsttrue(boolean[][] a) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (a[i][j]) return i; return -1; }
22
2D arrays example 22 We will illustrate the use of 2D arrays further with a case study of The Game of Life http://en.wikipedia.org/wiki/Conway’s_Game_of_Life In the next lecture… And there’s plenty of practice in the lab too
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.