Use for output String className list getClassgetName int locPeriod className

Use for output string classname list getclassgetname

This preview shows page 8 - 12 out of 14 pages.

// Use for output String className = list .getClass().getName(); int locPeriod = className .lastIndexOf( "." ); className = className .substring( locPeriod +1); String msg = String. format ( "%s size: %d, time to remove %d vals: " , className , list .size(), vals .size()); // Begin timing long begTime = System. currentTimeMillis (); for ( int x : vals ) { list .remove( x ); } // Stop timing long endTime = System. currentTimeMillis (); // Calculate total time in seconds. double totTime = ( endTime - begTime )/1000.0; msg += String. format ( "%.3f sec" , totTime ); System. out .println( msg ); } } 8
Image of page 8
: ) . ). 9
Image of page 9
12.Run Examplea.Create a package named: treeset_examplesb.Create a class named TreeSetExamples (in the treeset _examples package)and replace everything in the class(exceptthe package statement at the top) with:importjava.util.Set;importjava.util.SortedSet;importjava.util.TreeSet;publicclassTreeSetExamples {publicstaticvoidmain(String[] args) throwsFileNotFoundException {tsExamples1();}publicstaticvoidtsExamples1() {System.out.println("TreeSet Example 1\n-----------------");Set<String> tsCities = newTreeSet<String>();tsCities.add("New York");tsCities.add("Atlanta");tsCities.add("Durango");tsCities.add("Moab");System.out.println(" Order cities added: New York, Atlanta, Durango, Moab");System.out.print("Access cities (ordered) with for loop: ");for(String city : tsCitiesSystem.out.print(city + ", "}System.out.println();}}c.Run and verify the output. Your objective to observe that a TreeSet keeps the elements ordered. . ) { ); 10
Image of page 10
13. Read (no action required) a. Above, we showed that TreeSet is-a Set. Actually, there are two interfaces in between (as shown in the class diagram on the right) that prescribe the behaviors we saw in TreeSet . We first consider a few of the methods specified in the SortedSet interface: Method Description first() The first (smallest) element is returned last() The last (largest) element is returned headSet(toElement:E) Returns a SortedSet of elements that are strictly less than toElement . {x| x<toElement} tailSet(fromElement:E) Returns a SortedSet of elements greater than or equal to fromElement . {x| x>=fromElement} subSet(fromElement:E,
Image of page 11
Image of page 12

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture