|
|
||
| You are here: | ||
|
7.1.5
Thus index[1] contains the value 5 - the first title in the file alphabetically. If it was an author index, what would index[1] contain?
I hope you answered 2 |
Ideally, all data files would be sequential (the records maintained in order of some key field) to allow binary search. However, this can require shifting large amounts of data when a new record is inserted and, you can't order data on more than one field . If you are keeping a list of library books , for example, you might want to search by author, title or keyword. If the file were to be sequential you would probably organise it by book number anyway. Consider the following file:
The records have been added in no book number order, but it is possible to maintain an index on any of the other fields. For example, for the title :
In order to output the records in title order the following kind of algorithm might be used: i = 1While not eof(Datafile) do MoveTo(Datafile(,Index(i)) read(datafile, CDRecord Output(CD number, Title, Author, Year) i = i + 1 enddo The code assumes that the array, Index, has been set-up to contain the order of records by Title, as shown. This code moves the file pointer to the selected record, reads the record and outputs the data in title order. If index lists are held on other fields, Year or Author, for example, it is equally easy to output records in different orders. The list of indexes can be held in memory (using an array) or held in files (index files). This avoids the need to load the whole data file into memory. related: [ Topic 7 home | previous: partially-indexed files | next: direct access files ] See the pupil\java\files directory for examples of using fully indexed files. See the Java Page on files for some more programming examples on this topic (not yet, just a reminder to me to do it).
|
|
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |