CREATING A CLASS OF RECORDS

advertisement
CREATING A CLASS OF RECORDS
Related lists of arrays or ArrayLists are one way to store a number of
different pieces of information related to one particular item. For
example, if I want to store the names of basketball players I have on a
team and the number of points they score per game, I could store the
names of players in an array or Arraylist of Strings and their points per
game in an array or ArrayList of doubles. However, there is an easier
and more efficient way of doing this using a set of records, which in Java
can be done using an array or ArrayList of objects. Each object
represents one record (or element) in your database, and each field in
the object stores the data items (e.g. name, points per game, etc.).
In the following example, we are going to create a class called Student,
which will store students’ names, student number and grade level.
The class will be created with the following constructors and methods:
public class Student {
private String name;
private String number;
private int grade;
public Student(String number, String name, int grade) {
this.number = number;
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public String getStudentNumber() {
return number;
}
public int getGrade() {
return grade;
}
Class of Records
Page 1 of 3
public String toString() {
return String.format(“%-20s%-25s%5s”, number, name,
grade);
}
In order to declare and initialize the Student record object in a program we would write the following
line of code:
Student student = new Student(“071-123-344”, “Jack Black”,
12);
To output this record to the system console, we would then do the following:
System.out.println(student.getStudentNumber() + “” +
student.getName() + “” + student.getGrade());
Of course, the real benefit of using records is realized when you store the records in an array or
ArrayList, as you will see in the following example.
THE StudentDemo APPLICATION
The following program will read the student numbers, names, and grades from the following unsorted
comma-delimited text file called students.txt:
The data will be stored in a LinkedList of Student objects, then outputted to the system console.
Here’s the code:
import java.util.*;
import java.io.*;
public class StudentDemo {
public static void main(String[] args) {
// Declare and initialize LinkedList object
LinkedList<Student> students = new LinkedList<Student>();
Class of Records
Page 2 of 3
try
{
// Declare and initialize BufferedReader object
BufferedReader in = new BufferedReader(new FileReader(new
File("students.txt")));
// Declare variables
String line;
String[] data;
line = in.readLine();
while (line != null)
{
// Read data from file
data = line.split(", ");
students.add(new Student(data[0], data[1],
Integer.parseInt(data[2])));
line = in.readLine();
}
in.close();
}
catch (IOException e)
{}
// Output students to console
for (int i = 0; i < students.size (); i++)
{
System.out.println(students.get(i).toString());
}
}
}
Class of Records
Page 3 of 3
Download