类 Counter
- java.lang.Object
-
- edu.princeton.cs.algs4.Counter
-
- 所有已实现的接口:
java.lang.Comparable<Counter>
public class Counter extends java.lang.Object implements java.lang.Comparable<Counter>
TheCounterclass 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.
-
方法概要
修饰符和类型 方法 说明 intcompareTo(Counter that)Compares this counter to the specified counter.voidincrement()Increments the counter by 1.static voidmain(java.lang.String[] args)Reads two command-line integers n and trials; creates n counters; increments trials counters at random; and prints results.inttally()Returns the current value of this counter.java.lang.StringtoString()Returns a string representation of this 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- 返回:
0if 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
-
-