Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 1 of 19 CSCI1402 Introductory Java Programming Using class ArrayList (cont) Collections Example 10,000 students registered at the university. Computer application to manage the student data will use a Student object to store details of each individual student . Therefore,10,000 students objects needed Page 1 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 2 of 19 Display the details of each student: System.out.println(student1.toString()); System.out.println(student2.toString()); System.out.println(student3.toString()); System.out.println(student4.toString()); System.out.println(student5.toString()); System.out.println(student6.toString()); . . System.out.println(student9999.toString()); Page 2 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 3 of 19 We want to be able to store the student objects in some kind of data structure that will allow us to process the collection of students as a whole We can do this with an array: Student[] students = new Student(10000); Page 3 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 4 of 19 Student objects need to be added one by one e.g. Student s1 = new Student(“Safia”, ‘F’); students[0] = s1; OR students[0] = new Student (“Safia”, ‘F’); Page 4 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 5 of 19 Once added, the array can be processed as a whole. e.g display the names of all of the students for(int i = 0; i<students.length;i++) { System.out.println(students[i].getName()); } Page 5 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 6 of 19 A problem with using an array: Unused elements of the array are initialised to null We have to ensure that we don’t request a null to execute a method Safia Malik Sue Ben null null null null ... 18 21 19 50 0 1 2 3 4 5 6 7 ... a nullPointerException is thrown getName() Page 6 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 7 of 19 Solution 1 Check each object before sending the getName() message for(int i = 0; i<students.length;i++) { //begin for loop if(students[i] ! = null) {//begin do if not a null System.out.println(students[i].getName()); } // end do if not a null } // end for loop Page 7 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 8 of 19 Solution 2 If you know that objects will always be stored in a contiguous block starting at position 0 then stop the loop when the first null is reached for(int i = 0; i<students.length && students[i] ! = null;i++) { //begin for loop System.out.println(students[i].getName()); } // end for loop Page 8 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 9 of 19 A better solution Use a java ArrayList - it does the work for you Page 9 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 10 of 19 Another (more serious) problem with using an array: An array is a static fixed –size structure It cannot grow We have to specify it’s size when we create it If it becomes full, then the only thing we can do is to create another array that is bigger, copy all of the objects from the old array into the new array etc. If the students array already stored 10,000 students and another student wanted to enrol then this would need to be done This involves a lot of extra coding Page 10 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Solution Don’t enrol any more students Page 11 of 19 Page 11 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 12 of 19 Another Solution Create a much bigger array to start with This can be very wasteful of the computer’s memory Page 12 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 A better solution Use a java ArrayList it is a dynamic data structure it grows on demand Page 13 of 19 Page 13 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 14 of 19 The java ArrayList is provided in the java API (Application Programmers Interface) The API contains ready – made classes that are available for programmers to use –ArrayList is one of them ArrayList is one of many classes provided in the API to manage collections of objects Page 14 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 15 of 19 This is the specification for the ArrayList: java.util Class ArrayList Constructor Summary ArrayList() Constructs an empty list. ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity. Page 15 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 16 of 19 Method Summary add(int index, Object element) Inserts the specified element at the specified position in this list. add(Object o) Appends the specified element to the end of this list. addAll(Collection c) Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. clear() Removes all of the elements from this list. void boolean boolean void Page 16 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 17 of 19 contains(Object elem) Returns true if this list contains the specified element. Object get(int index) Returns the element at the specified position in this list. indexOf(Object elem) Searches for the first occurence of the given argument, testing for equality using the equals method. isEmpty() Tests if this list has no elements. boolean int boolean Page 17 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 18 of 19 lastIndexOf(Object elem) Returns the index of the last occurrence of the specified object in this list. Object remove(int index) Removes the element at the specified position in this list. int Page 18 of 19 CSCI1402 Introductory Java Programming Week 20 Lecture 1 Page 19 of 19 set(int index, Object element) Replaces the element at the specified position in this list with the specified element. int size() Returns the number of elements in this list. String toString() Returns a String representation of all of the objects in the ArrayList Object Page 19 of 19