Page 601 - Introduction to Programming with Java: A Problem Solving Approach
P. 601
14.7 UncheckedExceptions 567 to cleaner solutions. In the next example, the preferred strategy isn’t so clear cut. We’ll use both strategies
and compare the results.
StudentList Example
Figure 14.8 presents a StudentList class that manages a list of student names. The class stores stu- dent names in an ArrayList named students. The class contains a constructor for initializing the students list, a display method for printing the students list, and a removeStudent method that removes a specified student from the students list. We’ll focus on the removeStudent method.
/******************************************************
*
StudentList.java
Dean & Dean
This class manages an ArrayList of students.
******************************************************/
*
*
*
import java.util.ArrayList;
public class StudentList
{
ArrayList<String> students = new ArrayList<String>();
//***************************************************
Apago PDF Enhancer
public StudentList(String[] names)
{
}
// end constructor
for (int i=0; i<names.length; i++)
{
}
students.add(names[i]);
//***************************************************
public void display()
{
}
// end display
for (int i=0; i<students.size(); i++)
{
}
System.out.print(students.get(i) + " ");
System.out.println();
//***************************************************
public void removeStudent(int index)
{
}
// end StudentList
students.remove(index);
This is a dangerous method call.
} // end removeStudent
Figure 14.8 First draft of StudentList class which maintains a list of students