⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter9n5.java

📁 JAVA源代码程序aashjkjhkjhkjhjkhkj
💻 JAVA
字号:
/** * * searching a sorted array (2) * * Written by: Roger Garside * * First Written: 11/Oct/96 * Last Rewritten: 13/Oct/96 * */import java.io.* ;import java.lancs.* ;public class Chapter9n5    {    /**     *     * main     *     */    public static void main(String[] args) throws Exception        {	int MAX_NO_OF_PERSONS = 100 ;        Person[] personGroup = new Person[MAX_NO_OF_PERSONS] ;        int noOfPersons = 0 ;		// read the data in	BasicIo.prompt("type file name: ") ;        String fileName = BasicIo.readString() ;	noOfPersons = readArray(fileName, personGroup) ;	System.out.println("read " + noOfPersons + " elements from file "				 + fileName) ;	// display the (unsorted) data	displayArray(personGroup, noOfPersons) ;	// sort the array	sortArray(personGroup, noOfPersons) ;	// display the sorted data	System.out.println() ;	displayArray(personGroup, noOfPersons) ;	// search for a specified surname	searchArray(personGroup, noOfPersons) ;        } // end of main method    /**     *     * readArray     *     */    private static int readArray(String name, Person[] array)			throws IOException	{        BasicFileIo file = new BasicFileIo(BasicFileIo.INPUT, name) ;	int arrayLength = 0 ;        while (true)	    {	    String fore = file.readString() ;	    if (fore == null)	        break ;	    if (arrayLength == array.length)		{		System.out.println("ERROR: too many people") ;		System.exit(1) ;		}            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 ;            array[arrayLength] = new Person(fore, sur, age, gender) ;	    arrayLength++ ;	    }        file.closeFile() ;	return arrayLength ;	} // end of method readArray    /**     *     * displayArray     *     */    private static void displayArray(Person[] array, int arrayLength)        {        for (int i = 0 ; i < arrayLength ; i++)    	    System.out.println(i + ": " + array[i].getForename() + " " +		array[i].getSurname() + " " + array[i].getAge()) ;        } // end of method displayArray    /*     *     * sortArray     *     */    private static void sortArray(Person[] array, int arrayLength)        {         for (int i = 1 ; i < arrayLength ; i++)	    {	    int j = i ;	    String s = array[i].getSurname() ;	    Person p = array[i] ;	    while ((j > 0) &&                   (array[j - 1].getSurname().compareTo(s) > 0))	        {	        array[j] = array[j - 1] ;	        j-- ;	        }            array[j] = p ;	    }        } // end of method sortArray    /**     *     * searchArray     *     */    private static void searchArray(Person[] array, int arrayLength)			throws IOException        {        BasicIo.prompt("Type the surname to be searched for: ") ;        String surname = BasicIo.readString() ;        int i = -1 ;        int j = arrayLength ;        int k = 0 ;        boolean matchSurname = false ;        while (((i + 1) < j) && (!matchSurname))	    {	    k = (i + j) / 2 ;	    int result = array[k].getSurname().compareTo(surname) ;	    if (result == 0)	        matchSurname = true ;	    else if (result < 0)	        i = k ;	    else // i.e. result > 0	        j = k ;            }        if (matchSurname)	    System.out.println("found at position " + k) ;        else	    System.out.println("no such person") ;        } // end of method searchArray    } // end of class Chapter9n5

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -