Annexes

Aide en ligne sur Random
java.util
Class Random java.lang.Object java.util.Random All Implemented Interfaces: Serializable Direct Known Subclasses: SecureRandom public class Randomextends Objectimplements Serializable An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.) If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods. The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits. Many applications will find the method Math.random() simpler to use. Since: 1.0 See Also: Serialized Form Constructor Summary Random() Creates a new random number generator. Random(long seed) Creates a new random number generator using a single long seed. Method Summary

protected next(int bits) Generates the next pseudorandom number. int boolean
nextBoolean() Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. nextBytes(byte[] bytes) Generates random bytes and places them into a user-supplied byte array. nextDouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. nextFloat() Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence. nextGaussian() Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence. nextInt() Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. nextInt(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. nextLong() Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

void double

float

double

int

int

long

48/56

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks. where n is the number of concurrently scheduled tasks. notify. because its stop method is invoked. If the timer's task execution thread terminates unexpectedly. Date time) void schedule(TimerTask task. Timer(String name. equals. wait. Since: 1.util.Timer public class Timerextends Object A facility for threads to schedule tasks for future execution in a background thread. so it is capable of keeping an application from terminating. the timer's task execution thread terminates gracefully (and becomes subject to garbage collection).lang. Schedules the specified task for execution at the specified time. any further attempt to schedule a task on the timer will result in an IllegalStateException. After the last live reference to a Timer object goes away and all outstanding tasks have completed execution.void setSeed(long seed) Sets the seed of this random number generator using a single long seed. Methods inherited from class java. it "hogs" the timer's task execution thread. the task execution thread does not run as a daemon thread. which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes. so the cost to schedule a task is O(log n). in turn. this can take arbitrarily long to occur. finalize. beginning at the specified time. notifyAll. Timer(boolean isDaemon) Creates a new timer whose associated thread may be specified to run as a daemon. for example. toString. However. Timer(String name) Creates a new timer whose associated thread has the specified name. This class does not offer real-time guarantees: it schedules tasks using the Object. By default. wait. and may be specified to run as a daemon. If a timer task takes excessive time to complete.3 See Also: TimerTask. sequentially. Object. discarding any currently scheduled tasks.wait(long) method. Implementation note: This class scales to large numbers of concurrently scheduled tasks (thousands should present no problem). delay the execution of subsequent tasks. or for repeated execution at regular intervals. void schedule(TimerTask task. hashCode. Internally.Object clone. wait Aide en ligne sur Timer java. Removes all cancelled tasks from this timer's task queue. Date firstTime. Implementation note: All constructors start a timer thread.wait(long) Constructor Summary Timer() Creates a new timer. Schedules the specified task for execution after the specified delay. This class is thread-safe: multiple threads can share a single Timer object without the need for external synchronization.Object java. as if the timer's cancel method had been invoked. getClass. void schedule(TimerTask task. long period) Schedules the specified task for repeated fixed-delay execution. This can. it uses a binary heap to represent its task queue. Method Summary void cancel() int purge() Terminates this timer. Tasks may be scheduled for one-time execution. Timer tasks should complete quickly. boolean isDaemon) Creates a new timer whose associated thread has the specified name. long delay) 49/56 . the caller should invoke the timer's cancel method. If a caller wants to terminate a timer's task execution thread rapidly.lang.util Class Timer java.

wait Aide en ligne sur TimerTask java. long period) void scheduleAtFixedRate(TimerTask task.Scanner All Implemented Interfaces: Iterator<String> public final class Scannerextends Objectimplements Iterator<String> A simple text scanner which can parse primitive types and strings using regular expressions. hashCode. toString. toString. Schedules the specified task for repeated fixed-rate execution. Date firstTime. wait. this code allows a user to read a number from System. finalize.Object clone. Since: 1. which by default matches whitespace. Method Summary boolean cancel() Cancels this timer task. int i = sc.lang.in).util.lang. abstract run() void long scheduledExecutionTime() Returns the scheduled execution time of the most recent actual execution of this task. beginning at the specified time.util Class TimerTask java. while (sc. As another example. A Scanner breaks its input into tokens using a delimiter pattern. equals. The resulting tokens may then be converted into values of different types using the various next methods. wait Aide en ligne sur Scanner Class Scanner java. void scheduleAtFixedRate(TimerTask task. For example. long period) Schedules the specified task for repeated fixed-delay execution.nextInt(). beginning after the specified delay.nextLong(). getClass. 50/56 . this code allows long types to be assigned from entries in a file myNumbers: Scanner sc = new Scanner(new File("myNumbers")). beginning after the specified delay.lang. equals. long delay. Schedules the specified task for repeated fixed-rate execution. getClass. long period) Methods inherited from class java. hashCode. Methods inherited from class java.hasNextLong()) { long aLong = sc.void schedule(TimerTask task.lang.Object java. finalize. wait.Object clone.util. notifyAll.Object java.in: Scanner sc = new Scanner(System. notifyAll.TimerTask All Implemented Interfaces: Runnable public abstract class TimerTaskextends Objectimplements Runnable A task that can be scheduled for one-time or repeated execution by a Timer.3 See Also: Timer Constructor Summary protected TimerTask() Creates a new timer task. long delay. notify. wait. notify. The action to be performed by this timer task. wait.

These methods will attempt to match the specified pattern with no regard to delimiters in the input and thus can be used in special circumstances where delimiters are not relevant.Locale) method.println(s.nextInt()). Scanner(InputStream source.group(i)).lang. Scanner s = new Scanner(input). When a Scanner is closed.out. System. The reset() method will reset the value of the scanner's delimiter to the default whitespace delimiter regardless of whether it was previously changed.println(result. A scanner can read text from any object which implements the Readable interface.util. Both hasNext and next methods may block waiting for further input.nextInt()). s. it may be changed via the useLocale(java. it will close its input source if the source implements the Closeable interface.out.regex. s. A scanner will default to interpreting numbers as decimal unless a different radix has been set by using the useRadix(int) method. System. String charsetName) Constructs a new Scanner that produces values scanned from the specified file. findWithinHorizon(java. i++) System. s. The reset() method will reset the value of the scanner's radix to 10 regardless of whether it was previously changed. Localized numbers An instance of this class is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale.close(). For example.out. empty tokens may be returned. The delimiting pattern "\\s" could return empty tokens since it only passes one space at a time. so that it may be retrieved or skipped via some other method. System.Pattern) methods operate independently of the delimiter pattern.groupCount(). A scanner's initial locale is the value returned by the Locale. A scanning operation may block waiting for input.CharBuffer) method throws an IOException then the scanner assumes that the end of the input has been reached. Scanner(File source. These methods may block waiting for more input.read(java.String). MatchResult result = s.} The scanner can also use delimiters other than whitespace.useDelimiter("\\s*fish\\s*"). When a scanner throws an InputMismatchException. A Scanner is not safe for multithreaded use without external synchronization. the pattern "\\s+" will return no empty tokens since it matches multiple instances of the delimiter. The default whitespace delimiter used by a scanner is as recognized by Character. Scanner s = new Scanner(input). Constructor Summary Scanner(File source) Constructs a new Scanner that produces values scanned from the specified file. Unless otherwise mentioned. prints the following output: 1 2 red blue The same output can be generated with this code. Depending upon the type of delimiting pattern. Scanner(InputStream source) Constructs a new Scanner that produces values scanned from the specified input stream. the scanner will not pass the token that caused the exception.println(s.out.println(s.getDefault() method. The reset() method will reset the value of the scanner's locale to the initial locale regardless of whether it was previously changed. System.next()). The findInLine(java. This example reads several items in from a string: String input = "1 fish 2 fish red fish blue fish".String. passing a null parameter into any method of a Scanner will cause a NullPointerException to be thrown. and then attempt to return the next token.match().findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)").lang.next()). The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method. String charsetName) Constructs a new Scanner that produces values scanned from the specified input 51/56 . Whether a hasNext method blocks has no connection to whether or not its associated next method will block.util.isWhitespace.println(s. for (int i=1. which uses a regular expression to parse all four tokens at once: String input = "1 fish 2 fish red fish blue fish". The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern. int). i<=result.nio. If an invocation of the underlying readable's Readable.close().out. and skip(java.

int horizon) Attempts to find the next occurrence of the specified pattern. Scanner(ReadableByteChannel source) Constructs a new Scanner that produces values scanned from the specified channel. hasNextBoolean() Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".stream. String String String String boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean boolean 52/56 . String charsetName) Constructs a new Scanner that produces values scanned from the specified channel. hasNextBigDecimal() Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method. ignoring delimiters. hasNext(Pattern pattern) Returns true if the next complete token matches the specified pattern. hasNextDouble() Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. Method Summary void Pattern close() Closes this scanner. hasNext(String pattern) Returns true if the next token matches the pattern constructed from the specified string. ignoring delimiters. findWithinHorizon(String pattern. hasNextFloat() Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method. findWithinHorizon(Pattern pattern. Scanner(String source) Constructs a new Scanner that produces values scanned from the specified string. hasNextByte() Returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method. findInLine(Pattern pattern) Attempts to find the next occurrence of the specified pattern ignoring delimiters. findInLine(String pattern) Attempts to find the next occurrence of a pattern constructed from the specified string. delimiter() Returns the Pattern this Scanner is currently using to match delimiters. Scanner(ReadableByteChannel source. hasNextByte(int radix) Returns true if the next token in this scanner's input can be interpreted as a byte value in the specified radix using the nextByte() method. int horizon) Attempts to find the next occurrence of a pattern constructed from the specified string. hasNext() Returns true if this scanner has another token in its input. hasNextBigInteger() Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the nextBigInteger() method. Scanner(Readable source) Constructs a new Scanner that produces values scanned from the specified source. hasNextBigInteger(int radix) Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the specified radix using the nextBigInteger() method.

boolean boolean boolean boolean boolean boolean IOException Locale MatchResult String String String BigDecimal BigInteger BigInteger boolean byte byte double float int int String 53/56 . next() Finds and returns the next complete token from this scanner. match() Returns the match result of the last scanning operation performed by this scanner. hasNextInt(int radix) Returns true if the next token in this scanner's input can be interpreted as an int value in the specified radix using the nextInt() method. hasNextLong(int radix) Returns true if the next token in this scanner's input can be interpreted as a long value in the specified radix using the nextLong() method. next(Pattern pattern) Returns the next token if it matches the specified pattern. nextInt() Scans the next token of the input as an int. ioException() Returns the IOException last thrown by this Scanner's underlying Readable. nextDouble() Scans the next token of the input as a double. hasNextLong() Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method. nextFloat() Scans the next token of the input as a float. nextBoolean() Scans the next token of the input into a boolean value and returns that value. hasNextShort(int radix) Returns true if the next token in this scanner's input can be interpreted as a short value in the specified radix using the nextShort() method. nextBigInteger(int radix) Scans the next token of the input as a BigInteger. hasNextLine() Returns true if there is another line in the input of this scanner.boolean hasNextInt() Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. nextByte() Scans the next token of the input as a byte. nextInt(int radix) Scans the next token of the input as an int. locale() Returns this scanner's locale. nextBigDecimal() Scans the next token of the input as a BigDecimal. nextLine() Advances this scanner past the current line and returns the input that was skipped. hasNextShort() Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method. next(String pattern) Returns the next token if it matches the pattern constructed from the specified string. nextByte(int radix) Scans the next token of the input as a byte. nextBigInteger() Scans the next token of the input as a BigInteger.

nextShort(int radix) Scans the next token of the input as a short. ignoring delimiters.lang. toString() Returns the string representation of this Scanner. wait. Scanner Scanner Scanner String Scanner Scanner Scanner Scanner Methods inherited from class java. and a utility method for quickly copying a portion of an array. beginning 54/56 . in The "standard" input stream. finalize. hashCode. useLocale(Locale locale) Sets this scanner's locale to the specified locale. and error output streams. int srcPos. Method Summary static void arraycopy(Object src. Object dest. notify. radix() Returns this scanner's default radix. notifyAll. a means of loading files and libraries. skip(Pattern pattern) Skips input that matches the specified pattern. wait.lang.lang Class System java. useRadix(int radix) Sets this scanner's default radix to the specified radix. It cannot be instantiated. skip(String pattern) Skips input that matches a pattern constructed from the specified string. nextLong(int radix) Scans the next token of the input as a long.long long short short int void nextLong() Scans the next token of the input as a long.System public final class Systemextends Object The System class contains several useful class fields and methods. int destPos.Object clone. nextShort() Scans the next token of the input as a short. reset() Resets this scanner. out The "standard" output stream.lang. Since: JDK1.0 Field Summary static PrintStream static InputStream static PrintStream err The "standard" error output stream. access to externally defined properties and environment variables.Object java. useDelimiter(String pattern) Sets this scanner's delimiting pattern to a pattern constructed from the specified String. equals. wait Aide en ligne sur System java. int length) Copies an array from the specified source array. standard output. Among the facilities provided by the System class are standard input. remove() The remove operation is not supported by this implementation of Iterator. useDelimiter(Pattern pattern) Sets this scanner's delimiting pattern to the specified pattern. getClass.

mapLibraryName(String libname) Maps a library name into a platform-specific string representing a native library. Returns an unmodifiable string map view of the current system environment. getProperty(String key) Gets the system property indicated by the specified key. String def) Gets the system property indicated by the specified key. in nanoseconds. static String static Console clearProperty(String key) Removes the system property indicated by the specified key. resulting in erratic behavior or deadlock. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects. to the specified position of the destination array. setIn(InputStream in) Reassigns the "standard" input stream. getSecurityManager() Gets the system security interface. setErr(PrintStream err) Reassigns the "standard" error output stream. console() Returns the unique Console object associated with the current Java virtual machine. setProperties(Properties props) Sets the system properties to the Properties argument. getProperties() Determines the current system properties. whether or not the given object's class overrides hashCode(). nanoTime() Returns the current value of the most precise available system timer.Strin getenv() g> static String static Properties static String static String static SecurityManager static int static Channel static void static void static String static long static void static void static void static void static void static void 55/56 . exit(int status) Terminates the currently running Java Virtual Machine. gc() Runs the garbage collector. runFinalization() Runs the finalization methods of any objects pending finalization. identityHashCode(Object x) Returns the same hash code for the given object as would be returned by the default method hashCode(). load(String filename) Loads a code file with the specified filename from the local file system as a dynamic library. if any. runFinalizersOnExit(boolean value) Deprecated.at the specified position. static long static void static void static Map<String. setOut(PrintStream out) Reassigns the "standard" output stream. loadLibrary(String libname) Loads the system library specified by the libname argument. inheritedChannel() Returns the channel inherited from the entity that created this Java virtual machine. currentTimeMillis() Returns the current time in milliseconds. getenv(String name) Gets the value of the specified environment variable. This method is inherently unsafe. getProperty(String key.

wait.static String setProperty(String key. String value) Sets the system property indicated by the specified key. getClass. wait. notifyAll.lang. wait 56/56 .Object clone. hashCode. toString. equals. notify. Methods inherited from class java. finalize.