类 Counter

  • 所有已实现的接口:
    java.lang.Comparable<Counter>

    public class Counter
    extends java.lang.Object
    implements java.lang.Comparable<Counter>
    The Counter class is a mutable data type to encapsulate a counter.

    For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 构造器概要

      构造器 
      构造器 说明
      Counter​(java.lang.String id)
      Initializes a new counter starting at 0, with the given id.
    • 方法概要

      修饰符和类型 方法 说明
      int compareTo​(Counter that)
      Compares this counter to the specified counter.
      void increment()
      Increments the counter by 1.
      static void main​(java.lang.String[] args)
      Reads two command-line integers n and trials; creates n counters; increments trials counters at random; and prints results.
      int tally()
      Returns the current value of this counter.
      java.lang.String toString()
      Returns a string representation of this counter.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 构造器详细资料

      • Counter

        public Counter​(java.lang.String id)
        Initializes a new counter starting at 0, with the given id.
        参数:
        id - the name of the counter
    • 方法详细资料

      • increment

        public void increment()
        Increments the counter by 1.
      • tally

        public int tally()
        Returns the current value of this counter.
        返回:
        the current value of this counter
      • toString

        public java.lang.String toString()
        Returns a string representation of this counter.
        覆盖:
        toString 在类中 java.lang.Object
        返回:
        a string representation of this counter
      • compareTo

        public int compareTo​(Counter that)
        Compares this counter to the specified counter.
        指定者:
        compareTo 在接口中 java.lang.Comparable<Counter>
        参数:
        that - the other counter
        返回:
        0 if the value of this counter equals the value of that counter; a negative integer if the value of this counter is less than the value of that counter; and a positive integer if the value of this counter is greater than the value of that counter
      • main

        public static void main​(java.lang.String[] args)
        Reads two command-line integers n and trials; creates n counters; increments trials counters at random; and prints results.
        参数:
        args - the command-line arguments