Java Reference
In-Depth Information
6.2 Searching information
When organizing data into objects by defining their records, we access the
various object attributes by defining a key and additional fields. That is,
complex objects are accessed via their corresponding keys that represent
handles. We suppose that our data-sets fit the local volatile memory so that
all objects can be loaded in the main memory without external input/output.
Further, we first suppose static data-structures that are not updated. We will
later consider the more challenging problem of adding/removing or changing
object attributes dynamically.
To fix ideas, consider searching for a word in a dictionary. The basic container
for storing information is the object that consists of a pair of “word” with its
corresponding “definition.” In Java, we thus declare the following class that
contains the two fields: word and definition :
class DictionaryEntry
{
String word;
String definition;
}
To create a dictionary, we first create an array of DictionaryEntry elements,
and then assign to each element an entry of the dictionary. The following
program illustrates this process:
Program 6.1 Structuring data of a dictionary into basic object elements
class DictionaryEntry
{ String word;
String definition ;
DictionaryEntry(String w, String def)
{ this .word= new String(w) ; // Clone the strings
this . definition= new String(def) ;
}
}
class TestMyDictionary
{
{
DictionaryEntry [] Dico = new DictionaryEntry [10];
Dico [0]= new DictionaryEntry( "Java" , "A modern object-oriented
programming language" );
Dico [1]= new DictionaryEntry( "C++" , "An effective object-
oriented programming language" );
Dico [2]= new DictionaryEntry( "FORTRAN" , "FORTRAN stands for
FORmula TRANSlation. Often used for simulation." );
//...
}
}
public static void main( String [ ]
args )
 
Search WWH ::




Custom Search