
AC
/**
* This programe demonstrates a triangular array
* @version 1.30 2021-12-11
*/
public class LotteryArray {
public static void main(String[] args) {
final int NMAX = 10;
//allocate triangular array
int[][] odds = new int[NMAX + 1][];
for (int n = 0; n <= NMAX; n++)
odds[n] = new int[n + 1];
//fill triangular array
for (int n = 0; n < odds.length; n++)
for (int k = 0; k < odds[n].length; k++) {
/*
*compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
*/
int lotteryOdds = 1;
for (int i = 1; i <= k; i++)
lotteryOdds = lotteryOdds * (n - i + 1) / i;
odds[n][k] = lotteryOdds;
}
//print triangular array
for (int[] row : odds) {
for (int odd : row)
System.out.printf("%4d", odd);
System.out.println();
}
}
}
C++ NOTE: In C++, the Java declaration
double [][] balances = new double[10][6]; //Java
is not the same as
double balences[10][6]; //C++
or even
double (*balences)[6] = new double [10][6]; //C++
Instead, an array of ten pointers is allocated:
double** balences = new double*[10]; //C++
Then, each elements in the pointer array is filled with an array of six numbers:
for(i = 0;i < 10;i++)
balences[i] = new double[6];
Mercifully, this loop is automatic when you ask for a new double[10][6]. When you want ragged arrays, you allocate the row arrays separately.
The “for each” Loop
Java has a powerful looping construct that allows you to loop through each element in an array(or any other collection of elements)without having to fuss with index values.
The enhanced for loop
for (variable : collection) statement
sets the given variable to each element of the collection and then executes the statement(which, of course, may be a block). The collection expression must be an array or an object of a class that implements the Iterable interface, such as ArrayList. We discuss array lists in Chapter 5 and the Iterable interface in Chapter 2 of Volume II.
For example,
for (int element : a)
System.out.println(element);
prints each element of the array a on a seperate line.
You should read this loop as “for each element in a”. Then designers of the Java languages considered using keyboards, such as foreach and in. But this loop was a late addition to the Java language, and in the end nobody wanted to break the old code that already contained methods or variable with these names(such as System.in).
Of course, you could achieve the same effect with a traditional for loop:
for (int i = 0;i < a.length;i++)
System.out.println(a[i]);
NOTE: The loop variable of the “for each” loop traverses the elements of the array, not the index values.
Additional notes:
java.math.BigInteger
- BigInteger subtract (BigInteger other)
- BigInteger multiply (BigInteger other)
- BigInteger divide (BigInteger other)
- BigInteger mod (BigInteger other)
returns the difference, product, quotient, remainder of this big integer and other.
- int compareTo (BigInteger other)
returns 0 if this big integer equals other, a negative result if this big integer is less than other, and a positive result otherwise.
- static BigInteger valueof (long x)
reuturns a big integer whose value equals x.
java.math.BigDecimal
- BigDecimal add (BigDecimal other)
- BigDecimal subtract (BigDecimal other)
- BigDecimal multiply (BigDecimal other)
- BigDecimal divide (BigDecimal other, RoundingMode mode)
returns the sum, difference, product, or quotient of this big decimal and other.
To compute the quotient, you must supply a rounding mode. The mode RoundingMode. HALF_UP is the rounding mode that you learned in school: round down the digits 0 to 4, round up the digits 5 to 9. It is appropriate fore routine calculations.
See the API documentation for other rounding modes.
- static BigDecimal valueOf (long x, int scale)
returns a big decimal whose value equals x or x/10^scale.
END