Java Reference
In-Depth Information
through n 1 of array A , function swap(A,i,j) exchanges elements i and
j in array A , and Random(n) returns an integer value in the range 0 to n1 (see
the Appendix for more information on swap and Random ).
/ ** RandomlypermutethevaluesinarrayA * /
static<E>voidpermute(E[]A){
for(inti=A.length;i>0;i--)//foreachi
swap(A,i-1,DSutil.random(i));// swapA[i-1]with
}
// arandomelement
Boolean variables: A Boolean variable is a variable (of type boolean in Java)
that takes on one of the two values true and false . These two values are often
associated with the values 1 and 0, respectively, although there is no reason why
this needs to be the case. It is poor programming practice to rely on the corre-
spondence between 0 and false , because these are logically distinct objects of
different types.
Logic Notation: We will occasionally make use of the notation of symbolic or
Boolean logic. A ) B means “A implies B” or “If A then B.” A , B means “A
if and only if B” or “A is equivalent to B.” A_B means “A or B” (useful both in
the context of symbolic logic or when performing a Boolean operation). A ^ B
means “A and B.” A and A both mean “not A” or the negation of A where A is a
Boolean variable.
Floor and ceiling: The floor of x (written bxc) takes real value x and returns the
greatest integer x. For example, b3:4c = 3, as does b3:0c, while b3:4c = 4
and b3:0c = 3. The ceiling of x (written dxe) takes real value x and returns
the least integer x. For example, d3:4e = 4, as does d4:0e, while d3:4e =
d3:0e = 3.
Modulus operator: The modulus (or mod) function returns the remainder of an
integer division. Sometimes written n mod m in mathematical expressions, the
syntax for the Java modulus operator is n%m . From the definition of remainder,
n mod m is the integer r such that n = qm + r for q an integer, and jrj < jmj.
Therefore, the result of n mod m must be between 0 and m1 when n and m are
positive integers. For example, 5 mod 3 = 2; 25 mod 3 = 1, 5 mod 7 = 5, and
5 mod 5 = 0.
There is more than one way to assign values to q and r, depending on how in-
teger division is interpreted. The most common mathematical definition computes
the mod function as n mod m = n mbn=mc. In this case, 3 mod 5 = 2.
However, Java and C ++ compilers typically use the underlying processor's ma-
chine instruction for computing integer arithmetic. On many computers this is done
by truncating the resulting fraction, meaning n mod m = n m(trunc(n=m)).
Under this definition, 3 mod 5 = 3.
Search WWH ::




Custom Search