全部版块 我的主页
论坛 数据科学与人工智能 IT基础 JAVA语言开发
3164 15
2015-06-05

  • Java Examples in a Nutshell, 3rd Edition
  • By: David Flanagan

  • Publisher: O'Reilly Media, Inc.

  • Pub. Date: January 21, 2004

  • Print ISBN-13: 978-0-596-00620-4

  • Pages in Print Edition: 722

  • Subscriber Rating: [0 Ratings]



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2015-6-5 10:35:48

Computing Factorials

复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-6-5 10:37:58
Recursive Factorials

Example 1-8 shows another way to compute factorials. This example uses a programming technique called recursion. Recursion happens when a method calls itself, or in other words, invokes itself recursively. The recursive algorithm for computing factorials relies on the fact that n! is equal to n*(n-1)!. Computing factorials in this fashion is a classic example of recursion. It is not a particularly efficient technique in this case, but there are many important uses for recursion, and this example demonstrates that it is perfectly legal in Java. This example also switches from the int data type, which is a 32-bit integer, to the long data type, which is a 64-bit integer. Factorials become very large, very quickly, so the extra capacity of a long makes the factorial( ) method more useful.
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-6-5 10:40:16
Caching Factorials

Example 1-9 shows a refinement to our previous factorial examples. Factorials are ideal candidates for caching because they are slightly time consuming to compute, and more importantly, there are few factorials you actually can compute, due to the limitations of the long data type. So, in this example, once a factorial is computed, its value is stored for future use.

Besides introducing the technique of caching, this example demonstrates several new things. First, it declares static fields within the Factorial3 class:

static long[  ] table = new long[21];
static int last = 0;
A static field is kind of like a variable, but it retains its value between invocations of the factorial( ) method. This means that static fields can cache values computed in one invocation for use by the next invocation.

Second, this example shows how to create an array:

static long[  ] table = new long[21];
The first half of this line (before the = sign) declares the static field table to be an array of long values. The second half of the line actually creates an array of 21 long values using the new operator.

Finally, this example demonstrates how to throw an exception:

throw new IllegalArgumentException("Overflow; x is too large.");
An exception is a kind of Java object; it is created with the new keyword, just as the array was. When a program throws an exception object with the throw statement, it indicates that some sort of unexpected circumstance or error has arisen. When an exception is thrown, program control transfers to the nearest containing catch clause of a try/catch statement. This clause should contain code to handle the exceptional condition. If an exception is never caught, the program terminates with an error.

Example 1-9 throws an exception to notify the calling procedure that the argument it passed is too big or too small. The argument is too big if it is greater than 20, since we can’t compute factorials beyond 20!. The argument is too small if it is less than 0, as factorial is only defined for nonnegative integers. Examples later in the chapter demonstrate how to catch and handle exceptions.
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-6-5 10:41:59
Computing Big Factorials

In the previous section, we learned that 20! is the largest factorial that can fit in a 64-bit integer. But what if you want to compute 50! or100!? The java.math.BigInteger class represents arbitrarily large integer values and provides methods to perform arithmetic operations on these very large numbers. Example 1-10 uses the BigInteger class to compute factorials of any size. It also includes a simple main( )method that defines a standalone test program for our factorial( ) method. This test program says, for example, that 50! is the following 65-digit number:

30414093201713378043612608166064768844377641568960512000000000000

Example 1-10 introduces the import statement. This statement must appear at the top of a Java file, before any class is defined (but after thepackage declaration). It provides a way to tell the compiler what classes you are using in a program. Once a class like java.math.BigIntegerhas been imported, you no longer have to type its full name; instead you can refer to it simply as BigInteger. You can also import an entire package of classes, as with the line:

import java.util.*

Note that the classes in the java.lang package are automatically imported, as are the classes of the current package, which, in this case, isje3.basics.

Example 1-10 uses the same caching technique Example 1-9 did. However, because there is no upper bound on the number of factorials that can be computed with this class, you can’t use a fixed-sized array for the cache. Instead, use the java.util.ArrayList class, which is a utility class that implements an array-like data structure that can grow to be as large as you need it to be. Because an ArrayList is an object rather than an array, you use such methods as size( ), add( ), and get( ) to work with it. By the same token, a BigInteger is an object rather than a primitive value, so you can’t simply use the * operator to multiply BigInteger objects. Use the multiply( ) method instead.

复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-6-5 10:46:08
Sorting Numbers

Example 1-14 implements a simple (but inefficient) algorithm for sorting an array of numbers. This example doesn’t introduce any new elements of Java syntax, but it is interesting because it reaches a real-world level of complexity. The sorting algorithm manipulates array entries using an if statement within a for loop that is itself within another for loop. You should take the time to study this short program carefully. Make sure that you understand exactly how it goes about sorting its array of numbers.

复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群