📄 chapter9n3.java
字号:
/** * * demonstration of an array of objects * * Written by: Roger Garside * * First Written: 11/Oct/96 * Last Rewritten: 11/Oct/96 * */import java.lancs.* ;public class Chapter9n3 { /** * * main * */ public static void main(String[] args) throws Exception { Person[] personGroup = new Person[100] ; // read the data from the file "person.data" BasicFileIo file = new BasicFileIo(BasicFileIo.INPUT, "person.data") ; int noOfPersons = 0 ; while (true) { String fore = file.readString() ; if (fore == null) break ; String sur = file.readString() ; int age = file.readInteger() ; String x = file.readString() ; int gender ; if (x.equalsIgnoreCase("MALE")) gender = Person.MALE ; else if (x.equalsIgnoreCase("FEMALE")) gender = Person.FEMALE ; else gender = Person.UNKNOWN ; personGroup[noOfPersons] = new Person(fore, sur, age, gender) ; noOfPersons++ ; } file.closeFile() ; // search for a specified surname BasicIo.prompt("Type the surname to be searched for: ") ; String surname = BasicIo.readString() ; int i = 0 ; while ((i < noOfPersons) && (!personGroup[i].getSurname().equals(surname))) i++ ; if (i == noOfPersons) System.out.println("no such person") ; else System.out.println("found a match at position " + i) ; // alternative search algorithm 1 /* int i = noOfPersons - 1 ; while ((i != -1) && (!personGroup[i].getSurname().equals(surname))) i-- ; if (i == -1) System.out.println("no such person") ; else System.out.println("found a match at position " + i) ; */ // alternative search algorithm 2 /* int matchIndex = -1; int noOfMatches = 0 ; for (int i = 0 ; i < noOfPersons ; i++) if (personGroup[i].getSurname().equals(surname)) { matchIndex = i ; noOfMatches++ ; } System.out.println("no of matches is "+ noOfMatches) ; if (noOfMatches != 0) System.out.println("last match was at " + matchIndex) ; */ } // end of method main } // end of class Chapter9n3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -