类 Interval1D
- java.lang.Object
-
- edu.princeton.cs.algs4.Interval1D
-
public class Interval1D extends java.lang.Object
TheInterval1D
class represents a one-dimensional interval. The interval is closed—it contains both endpoints. Intervals are immutable: their values cannot be changed after they are created. The classInterval1D
includes methods for checking whether an interval contains a point and determining whether two intervals intersect.For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
字段概要
字段 修饰符和类型 字段 说明 static java.util.Comparator<Interval1D>
LENGTH_ORDER
Compares two intervals by length.static java.util.Comparator<Interval1D>
MAX_ENDPOINT_ORDER
Compares two intervals by max endpoint.static java.util.Comparator<Interval1D>
MIN_ENDPOINT_ORDER
Compares two intervals by min endpoint.
-
构造器概要
构造器 构造器 说明 Interval1D(double min, double max)
Initializes a closed interval [min, max].
-
方法概要
修饰符和类型 方法 说明 boolean
contains(double x)
Returns true if this interval contains the specified value.boolean
equals(java.lang.Object other)
Compares this transaction to the specified object.int
hashCode()
Returns an integer hash code for this interval.boolean
intersects(Interval1D that)
Returns true if this interval intersects the specified interval.double
left()
已过时。Replaced bymin()
.double
length()
Returns the length of this interval.static void
main(java.lang.String[] args)
Unit tests theInterval1D
data type.double
max()
Returns the max endpoint of this interval.double
min()
Returns the min endpoint of this interval.double
right()
已过时。Replaced bymax()
.java.lang.String
toString()
Returns a string representation of this interval.
-
-
-
字段详细资料
-
MIN_ENDPOINT_ORDER
public static final java.util.Comparator<Interval1D> MIN_ENDPOINT_ORDER
Compares two intervals by min endpoint.
-
MAX_ENDPOINT_ORDER
public static final java.util.Comparator<Interval1D> MAX_ENDPOINT_ORDER
Compares two intervals by max endpoint.
-
LENGTH_ORDER
public static final java.util.Comparator<Interval1D> LENGTH_ORDER
Compares two intervals by length.
-
-
构造器详细资料
-
Interval1D
public Interval1D(double min, double max)
Initializes a closed interval [min, max].- 参数:
min
- the smaller endpointmax
- the larger endpoint- 抛出:
java.lang.IllegalArgumentException
- if the min endpoint is greater than the max endpointjava.lang.IllegalArgumentException
- if eithermin
ormax
isDouble.NaN
,Double.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
-
-
方法详细资料
-
left
@Deprecated public double left()
已过时。Replaced bymin()
.Returns the left endpoint of this interval.- 返回:
- the left endpoint of this interval
-
right
@Deprecated public double right()
已过时。Replaced bymax()
.Returns the right endpoint of this interval.- 返回:
- the right endpoint of this interval
-
min
public double min()
Returns the min endpoint of this interval.- 返回:
- the min endpoint of this interval
-
max
public double max()
Returns the max endpoint of this interval.- 返回:
- the max endpoint of this interval
-
intersects
public boolean intersects(Interval1D that)
Returns true if this interval intersects the specified interval.- 参数:
that
- the other interval- 返回:
true
if this interval intersects the argument interval;false
otherwise
-
contains
public boolean contains(double x)
Returns true if this interval contains the specified value.- 参数:
x
- the value- 返回:
true
if this interval contains the valuex
;false
otherwise
-
length
public double length()
Returns the length of this interval.- 返回:
- the length of this interval (max - min)
-
toString
public java.lang.String toString()
Returns a string representation of this interval.- 覆盖:
toString
在类中java.lang.Object
- 返回:
- a string representation of this interval in the form [min, max]
-
equals
public boolean equals(java.lang.Object other)
Compares this transaction to the specified object.- 覆盖:
equals
在类中java.lang.Object
- 参数:
other
- the other interval- 返回:
true
if this interval equals the other interval;false
otherwise
-
hashCode
public int hashCode()
Returns an integer hash code for this interval.- 覆盖:
hashCode
在类中java.lang.Object
- 返回:
- an integer hash code for this interval
-
main
public static void main(java.lang.String[] args)
Unit tests theInterval1D
data type.- 参数:
args
- the command-line arguments
-
-