CS211Fall 20002■ If there are only a few possible priorities then can use an array of lists● Each array position represents a priority (0..m-1 where m is the array size)● Each list holds all items that have that priority (treated as a queue)■ One text [Skiena] calls this a bounded height priority queue■ Time for add: O(1)■ Time for removeFirst: ● O(m) in the worst-case● Generally, fasterm-1013■ Example: Given a probabilistic model of bank-customer arrival times and transaction times, how many tellers are needed● Assume we have a way to generate random inter-arrival times● Assume we have a way to generate transaction times● Can simulate the bank to get some idea of how long customers must waitTime-Driven Simulation■ Check at each tick to see if any event occursEvent-Driven Simulation■ Advance clock to next event, skipping intervening ticks■ This uses a PQ!4■ Provides useful static methods for dealing with arrays● sort()▲ Mostly uses QuickSort▲ Uses MergeSort for Object[ ] (it’s stable)● binarySearch()● equals()● fill()■ These methods are overloaded to work with● arrays of each primitive type● arrays of Objects■ Methods sort and binarySearch can use the natural order or there is a version of each that can use a Comparator■ There is also a method for viewing an array as a List:static List asList (Object[ ] a);● Note that the resulting List is backed by the array (i.e., changes in the array are reflected in the List and vice versa)5■ Dangerous version:public final String suits[ ] = { “Clubs”, “Diamonds”, “Hearts”, “Spades” };■ The final modifier means that suits always refers to the same array, but the array’s elements can be changed● suits[0] = “Leisure”;■ Safe version:private final String theSuits[ ] = { “Clubs”, “Diamonds”, “Hearts”, “Spades” };public final List suits = Collections.unmodifiableList(Arrays.asList(theSuits));■ The Collections class provides unmodifiable wrappers; any methods that would modify the collection throw an UnsupportedOperationException● unmodifiableCollection, unmodifiableSet, unmodifiableSortedSet,unmodifiableList● unmodifiableMap, unmodifiableSortedMap6public static Object min (Collection c);public static Object min (Collection c, Comparator comp); public static Object max (Collection c);public static Object max (Collection c, Comparator comp);public static Comparator reverseOrder ( ); // Reverse of natural orderpublic static void reverse (List list); // Reverse the listpublic static void shuffle (List list); // Randomly shuffle the listpublic static void fill (List list, Object x); // List is filled with x’spublic static void sort (List list); // Sort using natural orderpublic static void sort (List list, Comparator comp);public static void binarySearch (List list, Object key);public static void binarySearch (List list, Object key, Comparator comp);…7MapputgetcontainsKeycontainsValueremovesizeisEmptyputAllclearkeySetvaluesentrySetSortedMapcomparatorfirstKeylastKeyHashMapTreeMapArraysasListbinarySearchequalsfillsortCollectionsminmaxreverseOrderreverseshufflefillsortbinarySearchunmodifiableCollectionunmodifiableSetunmodifiableSortedSetunmodifiableListunmodifiableMapunmodifiableSortedMap…8■ Layout● How items are arranged● There are lots of predefined GUI items JButton, JLabel, JCheckbox, JList, JScrollbar,…● You have to write the code that determines layout● In Java, you use LayoutManagers to help with layout■ Event Handling● An event is (generally) a user input or action● The JVM (Java Virtual Machine) takes care of generating eventsButton pushed, text typed, mouse clicked,…● You have to write the code that determines how your program responds to an event9JButton: a pushbutton that can be clicked by mouseJCheckbox: can be on (true) or off (false)JComboBox: a popup menu of user choicesJLabel: a text labelJList: scrolling list of user-choose-able itemsJScrollbar: a scroll barJTextField: allows editing of a single line of textJTextArea: multiline region for displaying and editing textJPanel: used for containing and grouping componentsJDialog: window used for user inputJFrame: top-level window with frame and border…10■ We are using the Swing components instead of the AWT (Abstract Windows Toolkit) components■ The Swing versions are more powerful and support pluggable look and feel (your application can look like Windows, Mac, or Motif regardless of underlying platform)■ The AWT components are still supported, but Swing use is recommended (by Sun)■ Swing was an internal codename that stuck■ The javax prefix was supposed to correspond to optional extensions, but javax.swing is an official part of Java 1.2 (= Java 2)11Containers■ A container is a component that can contain other components■ Since a container is also a component, containers can contain other containers, forming a containment hierarchy (not the same as the inheritance hierarchy)■ The add(component) method is used to add components to a container● Exactly where the component is placed depends on the container’s LayoutManager● The setLayout(…) method is used to set the container’sLayoutManager12■ FlowLayout● Use a left-to-right “flow”● If one row gets full, start on next row● The FlowLayout constructor can take an alignment (default is centered)▲ FlowLayout.LEFT▲ FlowLayout.CENTER▲ FlowLayout.RIGHT■ GridLayout● Uses a rectangular grid● You specify number of rows and number of columns ▲ new GridLayout(3,2);● Tries to fill each grid-box■ BorderLayout● Uses 5 regions: North, South, East, West, and Center● You specify location in add(component,where);● Where can be any one of▲ BorderLayout.NORTH▲ BorderLayout.SOUTH▲ BorderLayout.EAST▲ BorderLayout.WEST▲ BorderLayout.CENTER■ Others● CardLayout● GridBagLayout● BoxLayout● …13import javax.swing.*;import java.awt.FlowLayout;class GUITest {public static void main (String[ ] args) {JFrame frame = new JFrame();JPanel panel = new JPanel();panel.setLayout(new FlowLayout( ));panel.add(new JButton("One"));panel.add(new JButton("Two"));panel.add(new JButton("Three"));panel.add(new JButton("Four"));panel.add(new JButton("Five"));frame.getContentPane().add(panel);frame.setSize(200,400);frame.show();}}14import javax.swing.*;import java.awt.FlowLayout;class GUITest {public static void main (String[ ] args) {JFrame frame = new JFrame();JPanel panel
View Full Document