OReilly.Java.Cook Book.pdf

(3107 KB) Pobierz
JavaCookbook.doc
My stupid release 2002 J
For all the people which doesn’t have money to buy a good book
374508835.001.png
Book Description
The Java Cookbook is a comprehensive collection of problems, solutions, and practical
examples for anyone programming in Java. Developers will find hundreds of tried-and-
true Java "recipes" covering all of the major APIs as well as some APIs that aren't as well
documented in other Java books.
The Java Cookbook, like the bestselling Perl Cookbook, covers a lot of ground, and
offers Java developers short, focused pieces of code that can be easily incorporated into
other programs. The idea is to focus on things that are useful, tricky, or both. The book
includes code segments covering many specialized APIs--like media and servlets--and
should serve as a great "jumping-off place" for Java developers who want to get started in
areas outside of their specialization.
The book provides quick solutions to particular problems that can be incorporated into
other programs, but that aren't usually programs in and of themselves.
2
Preface ......................................................................................................................... 11
Who This Book Is For ............................................................................................ 12
What's in This Book? ............................................................................................. 12
Platform Notes ........................................................................................................ 14
Other Books ............................................................................................................ 15
Conventions Used in This Book ........................................................................... 17
Getting the Source Code ....................................................................................... 18
Acknowledgments .................................................................................................. 19
Chapter 1. Getting Started: Compiling, Running, and Debugging ..................... 21
1.1 Introduction ....................................................................................................... 21
1.2 Compiling and Running Java: JDK ............................................................... 22
1.3 Editing and Compili ng with a Color-Highlighting Editor ............................. 26
1.4 Compiling, Running, and Testing with an IDE ............................................ 27
1.5 Using Classes from This Book ...................................................................... 31
1.6 Automating Compilation with jr ...................................................................... 32
1.7 Automating Compilation with make .............................................................. 33
1.8 Automating Compilation with Ant .................................................................. 34
1.9 Running Applets ............................................................................................... 36
1.10 Dealing with Deprecation Warnings ........................................................... 38
1.11 Conditional Debugging without #ifdef ......................................................... 40
1.12 Debugging Printouts ..................................................................................... 41
1.13 Using a Debugger ......................................................................................... 42
1.14 Unit Testing: Avoid the Need for Debuggers ............................................ 44
1.15 Decompiling Java Class Files ..................................................................... 46
1.16 Preventing Others from Decompiling Your Java Files ............................. 48
1.17 Getting Readable Tracebacks ..................................................................... 49
1.18 Finding More Java Source Code ................................................................ 50
1.19 Program: Debug ............................................................................................ 52
Chapter 2. Interacting with the Environment .......................................................... 53
2.1 Introduction ....................................................................................................... 53
2.2 Getting Environment Variables ...................................................................... 53
2.3 System Properties ........................................................................................... 55
2.4 Writing JDK Release-Dependent Code ....................................................... 56
2.5 Writing Operating System-Dependent Code ............................................... 57
2.6 Using CLASSPATH Effectively ..................................................................... 59
2.7 Using Extensions or Other Packaged APIs ................................................. 61
2.8 Parsing Command -Line Arguments ............................................................. 62
Chapter 3. Strings and Things ................................................................................. 66
3.1 Introduction ....................................................................................................... 66
3.2 Taking Strings Apart with Substrings ........................................................... 68
3.3 Taking Strings Apart with StringTokenizer .................................................. 69
3.4 Putting Strings Together with + and StringBuffer ....................................... 72
3.5 Processing a String One Character at a Time ............................................ 73
3.6 Aligning Strings ................................................................................................ 74
3.7 Converting Between Unicode Characters and Strings .............................. 76
3
3.8 Reversing a String by Word or Character .................................................... 78
3.9 Expanding and Compressing Tabs ............................................................... 79
3.10 Controlling Case ............................................................................................ 82
3.11 Indenting Text Documents ........................................................................... 83
3.12 Entering Non-Printable Characters ............................................................. 85
3.13 Trimming Blanks from the End of a String ................................................. 86
3.14 Parsing Comma-Separated Data ................................................................ 87
3.15 Program: A Simple Text Formatter ............................................................. 91
3.16 Program: Soundex Name Comparisons .................................................... 93
Chapter 4. Pattern Matching with Regular Expressions ...................................... 96
4.1 Introduction ....................................................................................................... 96
4.2 Regular Expression Syntax ............................................................................ 98
4.3 How REs Work in Practice ........................................................................... 100
4.4 Using Regular Expressions in Java ............................................................ 101
4.5 Testing REs Interactively .............................................................................. 103
4.6 Finding the Matching Text ............................................................................ 104
4.7 Replacing the Matching Text ....................................................................... 105
4.8 Printing All Occurrences of a Pattern ......................................................... 106
4.9 Printing Lines Containing a Pattern ............................................................ 107
4.10 Controlling Case in match( ) and subst( ) ................................................ 109
4.11 Precompiling the RE ................................................................................... 109
4.12 Matching Newlines in Text ......................................................................... 110
4.13 Program: Data Mining ................................................................................. 112
4.14 Program: Full Grep ...................................................................................... 114
Chapter 5. Numbers ................................................................................................. 118
5.1 Introduction ..................................................................................................... 119
5.2 Checking Whether a String Is a Valid Number ......................................... 121
5.3 Storing a Larger Number in a Smaller ........................................................ 122
5.4 Taking a Fraction of an Integer Without Using Floating Point ................ 123
5.5 Ensuring the Accuracy of Floating-Point Numbers .................................. 124
5.6 Comparing Floating -Point Numbers ........................................................... 126
5.7 Rounding Floating-Point Numbers .............................................................. 127
5.8 Formatting Numbers ..................................................................................... 128
5.9 Converting Between Binary, Octal, Decimal, and Hexadecimal ............ 130
5.10 Operating on a Series of Integers ............................................................. 131
5.11 Working with Roman Numerals ................................................................. 132
5.12 Formatting with Correct Plurals ................................................................. 136
5.13 Generating Random Numbers .................................................................. 137
5.14 Generating Better Random Numbers ....................................................... 138
5.15 Calculating Trigonometric Functions ........................................................ 139
5.16 Taking Logarithms ....................................................................................... 139
5.17 Multiplying Matrixes ..................................................................................... 140
5.18 Using Complex Numbers ........................................................................... 142
5.19 Handling Very Large Numbers .................................................................. 144
5.20 Program: TempConverter .......................................................................... 145
5.21 Program: Number Palindromes ................................................................. 149
4
Chapter 6. Dates and Times ................................................................................... 152
6.1 Introduction ..................................................................................................... 152
6.2 Finding Today's Date .................................................................................... 153
6.3 Printing Date/Time in a Specified Format .................................................. 155
6.4 Representing Dates in Other Epochs ......................................................... 156
6.5 Converting YMDHMS to a Calendar or Epoch Seconds ......................... 157
6.6 Parsing Strings into Dates ............................................................................ 157
6.7 Converting Epoch Seconds to DMYHMS .................................................. 159
6.8 Adding to or Subtracting from a Date or Calendar ................................... 160
6.9 Difference Between Two Dates ................................................................... 161
6.10 Comparing Dates ......................................................................................... 162
6.11 Day of Week/Month/Year or Week Number ............................................ 164
6.12 Calendar Page ............................................................................................. 165
6.13 High-Resolution Timers .............................................................................. 167
6.15 Program: Reminder Service ...................................................................... 170
Chapter 7. Structuring Data with Java .................................................................. 172
7.1 Introduction ..................................................................................................... 172
7.2 Data Structuring Using Arrays ..................................................................... 173
7.3 Resizing an Array .......................................................................................... 174
7.4 Like an Array, but More Dynamic ................................................................ 176
7.5 Data-Independent Access with Iterators .................................................... 177
7.6 Structuring Data in a Linked List ................................................................. 178
7.7 Mapping with Hashtable and HashMap ..................................................... 181
7.8 Storing Strings in Properties and Preferences .......................................... 182
7.9 Sorting a Collection ....................................................................................... 184
7.10 Sorting in Java 1.1 ....................................................................................... 188
7.11 Avoiding the Urge to Sort ........................................................................... 189
7.12 Sets ................................................................................................................ 190
7.13 Finding an Object in a Collection .............................................................. 191
7.14 Converting a Collection to an Array .......................................................... 193
7.15 Rolling Your Own Iterator ........................................................................... 194
7.16 Stack .............................................................................................................. 195
7.17 Multidimensional Structures ....................................................................... 196
7.18 Finally, Collections ...................................................................................... 198
7.19 Program: Timing Comparisons ................................................................. 200
Chapter 8. Object-Oriented Techniques ............................................................... 202
8.1 Introduction ..................................................................................................... 202
8.2 Printing Objects: Formatting with toStri ng( ) ............................................. 204
8.3 Overriding the Equals Method ..................................................................... 205
8.4 Overriding the Hashcode Method ............................................................... 207
8.5 The Clone Method ......................................................................................... 209
8.6 The Finalize Method ...................................................................................... 211
8.7 Using Inner Classes ...................................................................................... 212
8.8 Providing Callbacks via Interfaces .............................................................. 213
8.9 Polymorphism/Abstract Methods ................................................................ 216
8.10 Passing Values ............................................................................................ 217
5
Zgłoś jeśli naruszono regulamin