Java Reference
In-Depth Information
With all the supporting routines written, let us consider the only method,
generateCrossReference , shown in Figure 12.30. Lines 6 and 7 create an empty
map. We read the input and build the map at lines 11-20. At each iteration, we
1 /**
2 * Output the cross reference
3 */
4 public void generateCrossReference( )
5 {
6 Map<String,List<Integer>> theIdentifiers =
7 new TreeMap<String,List<Integer>>( );
8 String current;
9
10 // Insert identifiers into the search tree
11 while( ( current = tok.getNextID( ) ) != null )
12 {
13 List<Integer> lines = theIdentifiers.get( current );
14 if( lines == null )
15 {
16 lines = new ArrayList<Integer>( );
17 theIdentifiers.put( current, lines );
18 }
19 lines.add( tok.getLineNumber( ) );
20 }
21
22 // Iterate through search tree and output
23 // identifiers and their line number
24 Set entries = theIdentifiers.entrySet( );
25 for( Map.Entry<String,List<Integer>> thisNode : entries )
26 {
27 Iterator<Integer> lineItr = thisNode.getValue( ).iterator( );
28
29 // Print identifier and first line where it occurs
30 System.out.print( thisNode.getKey( ) + ": " );
31 System.out.print( lineItr.next( ) );
32
33 // Print all other lines on which it occurs
34 while( lineItr.hasNext( ) )
35 System.out.print( ", " + lineItr.next( ) );
36 System.out.println( );
37 }
38 }
figure 12.30
The main cross-reference algorithm
Search WWH ::




Custom Search