EarthWeb sites:
|
|
appendix L
java.util
Package Reference
CONTENTS
The java.util package contains
a variety of classes representing data structures and other miscellaneous
features such as support for date and time.
The Enumeration interface
defines methods that can be used to iterate through a set of objects.
hasMoreElements
public abstract boolean hasMoreElements()
Can be used to determine if the enumeration has more
elements.
Returns: true
if there are more elements, false
if not.
nextElement
public abstract Object nextElement()
This method returns the next element in the enumeration.
Calling it repeatedly will move through the enumeration.
Returns: The next element in the enumeration.
Throws: NoSuchElementException
if there are no more elements in the enumeration.
The Observer interface defines
an update method that is
invoked by an Observable
object whenever the Observable
object has changed and wants to notify its observers.
update
public abstract void update(Observable
o, Object arg)
This method is called whenever an Observable
instance that is being observed invokes either of its notifyObservers
methods.
Parameters:
o-the Observable
object that is generating this message.
arg-any additional information
passed by the Observable
object's notifyObservers
method.
Extends: Object
Implements: Cloneable
This class represents a dynamically sized set of bits. Two constructors
are provided, one that creates an empty set of unspecified size
and one that creates a set of a specified size. The set method
can be used to set an individual bit, or clear can be used to
clear an individual bit.
BitSet Constructor
public BitSet()
This constructor creates an empty bit set.
BitSet Constructor
public BitSet(int nbits)
This constructor creates an empty bit set with the
specified number of bits.
Parameters: nbits-the
number of bits in the set.
and
public void and(BitSet set)
This method logically ANDs
the bit set with another bit set.
Parameters: set-the
bit set to AND with the current
set.
clear
public void clear(int bit)
Clears the specified bit.
Parameters: bit-the
bit to clear.
clone
public Object clone()
This method overrides the clone
method in Object. It can
be used to clone the bit set.
equals
public boolean equals(Object obj)
This method can be used to compare the contents of
two bit sets. If the same bits are set in the two bit sets, they
are considered equal.
Parameters: obj-the
bit set to compare against.
Returns: true
if the set bits are the same, false
otherwise.
get
public boolean get(int bit)
Gets the value of a specified bit in the set.
Parameters: bit-the
bit to get.
Returns: true
if the bit is set; false
if it is clear.
hashCode
public int hashCode()
This method overrides the hashCode
method in Object and can
be used to get a hash code for the instance.
Returns: A hash code for the instance.
or
public void or(BitSet set)
This method logically ORs
the bit set with another.
Parameters: set-the
bit set to OR with the current
set.
set
public void set(int bit)
Sets the specified bit.
Parameters: bit-the
bit to set.
size
public int size()
This method returns the amount of space, in bits,
used to store the set. Space for a bit set is allocated in 64-bit
increments.
Returns: The amount of space, in bits, used to store
the bit set.
toString
public String toString()
This method formats the bit set as a string. The string
will consist of an opening curly brace, comma-separated values
representing each set bit, and a closing curly brace.
Returns: A string representing the bits in the bit
set that are set.
xor
public void xor(BitSet set)
This method logically XORs
the bit set with another bit set.
Parameters: set-the
bit set to XOR with the current
set.
Extends: Object
The Date class stores a representation of a date and
time and provides methods for manipulating the date and time components.
Constructors are provided that will create a new Date instance
based on the current date and time, the UNIX-standard milliseconds
since midnight on January 1, 1970, a string, or from integers
representing the year, month, day, hours, minutes, and seconds.
Date Constructor
public Date()
This method creates a new Date
object using today's date.
Date Constructor
public Date(long date)
This method creates a date from a long
that represents the number of milliseconds since January 1, 1970.
Parameters: date-the
number of milliseconds since January 1, 1970.
Date Constructor
public Date(int year, int month, int
date)
This method creates a new Date
object that corresponds to the year, month, and day passed to
it. The first month of the year is month zero. The day of the
month is normalized so that impossible dates become real dates.
Parameters:
year-the number of years
since 1900.
month-the zero-based month,
from 0 to 11.
date-the day of the month.
Date Constructor
public Date(int year, int month, int
date, int hrs, int min)
This method creates a new Date
object that corresponds to the year, month, day, hours, and minutes
passed to it. As with the prior constructor, the day of the month
is normalized so that impossible dates become real dates.
Parameters:
year-the number of years
since 1900.
month-the zero-based month,
from 0 to 11.
date-the day of the month.
hrs-the zero-based number
of hours (0-23).
min-the zero-based number
of minutes (0-59).
Date Constructor
public Date(int year, int month, int
date, int hrs, int min,
int sec)
This method creates a new Date
object that corresponds to the year, month, day, hour, minute,
and seconds passed to it. As with the other constructors, the
day of the month is normalized so that impossible dates become
real dates.
Parameters:
year-the number of
years since 1900.
month-the zero-based month,
from 0 to 11.
date-the day of the month.
hrs-the zero-based number
of hours (0-23).
min-the zero-based number
of minutes (0-59).
sec-the zero-based number
of seconds (0-59).
Date Constructor
public Date(String s)
This method creates a new date based on the date string
passed to it.
Parameters: s-a
time string in the format passed to java.util.Date.Parse,
as described later in this appendix.
UTC
public static long UTC(int year, int
month, int date, int
hrs, int min, int sec)
This method calculates the time in UTC (Coordinated
Universal Time) format based on the specified parameters. Parameters
are expected to be given in UTC values, not the time in the local
time zone.
Parameters:
year-the number of years
since 1900.
month-the zero-based month,
from 0 to 11.
date-the day of the month.
hrs-the zero-based number
of hours (0-23).
min-the zero-based number
of minutes (0-59).
sec-the zero-based number
of seconds (0-59).
Returns: A UTC time value.
parse
public static long parse(String s)
This method calculates the time in UTC format based
on the string passed to it.
Parameters: s-a
formatted time string such as Mon, 8
Apr 1996 21:32:PM PST.
Returns: A UTC time value.
after
public boolean after(Date when)
Determines whether the Date
occurs after the specified date.
Parameters: when-the
date to compare against.
Returns: true
if the object's date occurs after the specified date; false
otherwise.
before
public boolean before(Date when)
This method determines whether the Date
occurs before the specified date.
Parameters: when-the
date to compare against.
Returns: true
if the object's date occurs before the specified date; false
otherwise.
equals
public boolean equals(Object obj)
This method determines whether two Date
objects are the same by comparing the dates represented by each
object.
Parameters: obj-the
object to compare against.
Returns: true
if the dates are the same; false
otherwise.
getDate
public int getDate()
This method returns the day (or date) portion of a
Date object.
Returns: The day of the month, from 1 to 31.
getDay
public int getDay()
This method returns the day of the week. Sunday is
assigned a value of 0.
Returns: The day of the week from 0 (Sunday) to
6 (Saturday).
getHours
public int getHours()
This method returns the hour.
Returns: The hour from 0 to 23.
getMinutes
public int getMinutes()
This method returns the minutes.
Returns: The minutes from 0 to 59.
getMonth
public int getMonth()
This method returns the month.
Returns: The month from 0 (January) to 11 (December).
getSeconds
public int getSeconds()
This method returns the seconds.
Returns: The seconds from 0 to 59.
getTime
public long getTime()
This method returns the number of milliseconds since
midnight on January 1, 1970.
Returns: The time expressed in elapsed milliseconds.
getTimezoneOffset
public int get`TimezoneOffset()
This method returns the offset in minutes of the current
time zone from the UTC.
Returns: The number of minutes difference between
the time zone of the object and UTC.
getYear
public int getYear()
This method returns the year after 1900.
Returns: The year after 1900.
hashCode
public int hashCode()
This method overrides the hashCode
method in Object and can
be used to get a hash code for the instance.
Returns: A hash code for the instance.
setDate
public void setDate(int date)
This method sets the day of the month portion of a
Date object.
Parameters: date-the
day value.
setHours
public void setHours(int hours)
This method sets the hours portion of a Date
object.
Parameters: hours-the
hour from 0 (midnight) to 23.
setMinutes
public void setMinutes(int minutes)
This method sets the minutes portion of a Date
object.
Parameters: minutes-the
minutes from 0 to 59.
setMonth
public void setMonth(int month)
This method sets the month portion of a Date
object.
Parameters: month-the
zero-based month from 0 (January) to 11 (December).
setSeconds
public void setSeconds(int seconds)
This method sets the seconds portion of a Date
object.
Parameters: seconds-the
seconds from 0 to 59.
setTime
public void setTime(long time)
This method sets the time to the time represented
by the number of milliseconds in the time
parameter. It is frequently used in conjunction with the getTime
method that returns a number of milliseconds.
Parameters: time-the
new time in milliseconds since January 1, 1970.
setYear
public void setYear(int year)
This method sets the year portion of a Date
instance.
Parameters: year-the
year after 1900 (for 1996, use 96).
toGMTString
public String toGMTString()
This method creates a string that contains the date
and time formatted according to GMT (Greenwich Mean Time) conventions.
Returns: A string representing the date in GMT format,
such as 14 Nov 1995 08:00:00 GMT.
toLocaleString
public String toLocaleString()
This method creates a string that contains the date
and time in the format of the current locale.
Returns: A string representing the date as formatted
for the locale of the instance, such as
11/14/95 00:00:00.
toString
public String toString()
This method creates a string that contains the day
of the week, the date, and the time.
Returns: A string representing the day of the week,
date and time of the instance, such as
Tue Nov 14 00:00:00 1995.
Extends: Object
The Dictionary class is an abstract class. Each element
in a Dictionary consists of a key and value. Elements are added
to a Dictionary using put and are retrieved using get. Elements
may be deleted with remove. The methods elements and keys each
return an enumeration of the values and keys, respectively, stored
in the Dictionary.
Dictionary Constructor
public Dictionary()
This is a default constructor that will create an
empty dictionary.
elements
public abstract Enumeration elements()
This abstract method returns an enumeration of all
elements in a dictionary.
Returns: An enumeration of each of the elements
in the dictionary. The methods of Enumeration
can be used to iterate through the elements.
get
public abstract Object get(Object key)
This abstract method retrieves an object from a dictionary
based on its key.
Parameters: key-the
key of the object to be retrieved.
Returns: The value associated with the key, if found;
null if not.
isEmpty
public abstract boolean isEmpty()
This abstract method can be used to determine if the
dictionary is empty.
Returns: true
if the dictionary is empty; false
if not.
keys
public abstract Enumeration keys()
This abstract method returns an enumeration of all
keys in a dictionary.
Returns: An enumeration of each of the keys in the
dictionary. The methods of Enumeration
can be used to iterate through the keys.
put
public abstract Object put(Object key,
Object value)
This abstract method inserts a new element into the
dictionary. To retrieve an element, use the get
method.
Parameters:
key-the key to be added.
value-the value associated
with the key.
Returns: If the key was already in the dictionary,
the old value associated with it is returned. If not, null is
returned.
Throws: NullPointerException
if the value is null.
remove
public abstract Object remove(Object
key)
This abstract method removes an object from a dictionary.
Parameters: key-the
key of the element to be removed.
Returns: If the key is found, the value associated
with it is returned; if not, null is returned.
size
public abstract int size()
This abstract method returns the number of elements
in the dictionary.
Returns: The number of items stored in the dictionary.
Extends: Dictionary
The Hashtable class is used for mapping keys to values.
Each element in a hash table consists of a key and a value. Elements
are added to a hash table using the put method and are retrieved
using get. Elements may be deleted from a hash table with remove.
A hash table will expand in size as elements are added to it.
When creating a new hash table, you can specify an initial capacity
and a load factor. The hash table will increase in size whenever
adding a new element would move the hash table past its threshold.
A hash table's threshold is its capacity multiplied by
its load factor. For example, a hash table with a capacity of
100 and a load factor of 0.75 would have a threshold of 75 items.
Hashtable Constructor
public Hashtable(int initialCapacity,
float loadFactor)
This constructor creates a new instance of a hash
table with the specified initial capacity and load factor. Although
an initial capacity is specified, the hash table will grow as
needed when new items are added. The initial capacity specifies
how many elements could be stored in the hash table if the load
factor is 1.0. The load factor is a number between 0.0 and 1.0
and specifies the percentage of the hash table that must be full
before the size is automatically increased.
Parameters:
initialCapacity-the initial
capacity of the hash table.
loadFactor-a value between
0.0 and 1.0 that specifies the percent of available hash slots
that can be filled before the table is automatically rehashed
into a large hash table.
Hashtable Constructor
public Hashtable(int initialCapacity)
This constructor creates a new hash table with the
specified initial capacity and a default load factor of 0.75.
Parameters: initialCapacity-the
initial capacity of the hash table.
Hashtable Constructor
public Hashtable()
This constructor creates a new hash table using default
values for the initial capacity and the load factor. A default
of 101 is used for the initial capacity, and 0.75 is used for
the load factor.
clear
public synchronized void clear()
This method will remove all elements from a hash table.
clone
public synchronized Object clone()
This method clones the hash table into a new hash
table. The keys and values themselves are not cloned.
Returns: A cloned hash table.
contains
public synchronized boolean contains(Object
value)
This method searches the hash table to determine if
a specific value is stored.
Parameters: value-the
value to search for.
Returns: true
if the value is found; false
if not.
Throws: NullPointerException
if the value is null.
containsKey
public synchronized boolean containsKey(Object
key)
This method searches the hash table to determine if
a specific key occurs.
Parameters: key-the
key to search for.
Returns: true
if the key is found; false
if not.
elements
public synchronized Enumeration elements()
This method returns an enumeration of all of the element
values in the instance.
Returns: An enumeration of each of the keys in the
hash table. The methods of Enumeration
can be used to iterate through the keys.
get
public synchronized Object get(Object
key)
This method retrieves the object associated with the
specified key.
Parameters: key-the
key of the object to be retrieved.
Returns: The value associated with the key, if found;
null if not.
isEmpty
public boolean isEmpty()
This method can be used to determine if the hash table
is empty.
Returns: true
if the hash table is empty; false
if not.
keys
public synchronized Enumeration keys()
This method returns an enumeration of all the keys
in the instance.
Returns: An enumeration of each of the keys in the
hash table. The methods of Enumeration
can be used to iterate through the keys.
put
public synchronized Object put(Object
key, Object value)
This method inserts a new element into the hash table.
To retrieve an element, use the get
method.
Parameters:
key-the key to be added.
value-the value associated
with the key.
Returns: If the key was already in the hash table,
the old value associated with it is returned. If not, null is
returned.
Throws: NullPointerException
if the value is null.
rehash
protected void rehash()
This method rehashes the hash table into a larger
hash table. It is not normally necessary to call this method directly
because it is invoked automatically based on the capacity and
load factor of the hash table.
remove
public synchronized Object remove(Object
key)
This method removes an object from a hash table.
Parameters: key-the
key of the element to be removed.
Returns: If the key is found, the value associated
with it is returned; if not, null is returned.
size
public int size()
This method returns the number of elements in the
hash table.
Returns: The number of items stored in the hash
table.
toString
public synchronized String toString()
This method overrides the toString
method in Object and formats
the contents of the hash table as a string.
Returns: A string representation of the hash table.
Extends: Object
An Observable class is a class that can be watched
or monitored by another class that implements the Observer interface.
Associated with an Observable instance is a list of observers.
Whenever the Observable instance changes it can notify each of
its observers. By using Observable and Observer classes you can
achieve a better partitioning of your code by decreasing the reliance
of one class on another.
Observable Constructor
public Observable()
This is an empty, default constructor.
addObserver
public synchronized void addObserver(Observer
o)
This method will add an observer to the list of objects
that are observing this instance. The observer must implement
the Observer interface.
Parameters: o-the
observer to add.
clearChanged
protected synchronized void clearChanged()
This method clears the internal flag that indicates
an Observable instance has
changed.
countObservers
public synchronized int countObservers()
This method counts the numbers of observers who are
observing the instance.
Returns: The number of observers for the instance.
deleteObserver
public synchronized void deleteObserver(Observer
o)
This method will delete an observer from the list
of observers that are monitoring an Observable
object. The observer must have been previously added with addObserver.
Parameters: o-the
observer to delete.
deleteObservers
public synchronized void deleteObservers()
This method will delete all observers of the Observable
instance.
hasChanged
public synchronized boolean hasChanged()
This method can be used to query if an Observable
has changed.
Returns: true
if an observable change has occurred; false
otherwise.
notifyObservers
public void notifyObservers()
This method will notify all observers that a change
has occurred in the Observable
object. This will result in a call to the update
method in each observer.
notifyObservers
public synchronized void notifyObservers(Object
arg)
This method will notify all observers that a change
has occurred in the Observable
object. This will result in a call to the update
method in each observer to which arg
will be passed.
Parameters: arg-any
object that can be used to convey information to the observers.
setChanged
protected synchronized void setChanged()
This method sets an internal flag to indicate that
an observable change has occurred within the instance.
Extends: Hashtable
The Properties class can be used to store keys and
associated values. Through its save and load methods, Properties
can be written to disk. This makes this class an excellent mechanism
for storing configuration information between runs of a program.
Member Variables
protected Properties defaults
This member stores the default property values.
Properties Constructor
public Properties()
This constructor is used to create an empty, new instance
of Properties.
Properties Constructor
public Properties(Properties defaults)
This constructor will create a new instance of Properties
and will establish a set of default properties.
getProperty
public String getProperty(String key)
This method is used to retrieve a property based on
its key. If no matching key is found, the defaults are searched.
If no match is found there either, null is returned.
Parameters: key-the
key of the property to retrieve.
Returns: The property associated with the key or
null if there is no matching key.
getProperty
public String getProperty(String key,
String defaultValue)
This method is used to retrieve a property based on
its key. If no match is found, defaultValue
is returned.
Parameters:
key-the key of the property
to retrieve.
defaultValue-the value to
use if no matching key is found.
Returns: The property associated with the key or
the defaultValue if there
is no matching key.
list
public void list(PrintStream out)
This method will list all of the properties to the
specified PrintStream. It
is useful mainly while debugging.
Parameters: out-the
PrintStream where the properties
are to be printed.
load
public synchronized void load(InputStream
in) throws IOException
This method reads a set of properties from the specified
InputStream. Used in conjunction
with the save method, Properties
can be written to disk at the end of a program run and then reloaded
at the start of the next run.
Parameters: in-the
input stream from which the properties are to be read.
Throws: IOException
if the specified file is not found or cannot be read.
propertyNames
public Enumeration propertyNames()
This method returns an enumeration of all of the property
names in the instance.
Returns: An enumeration of each of the property
names. The methods of Enumeration
can be used to iterate through the property names.
save
public synchronized void save(OutputStream
out, String header)
This method saves the properties to an output stream.
Since FileOutputStream is
a subclass of OutputStream,
this method can be used to write to a file.
Parameters:
out-the output stream to
which the properties are to be written.
header-a header that will
be sent to the output stream before the properties.
Extends: Object
The Random class represents a pseudo-random number
generator. Two constructors are provided, one taking a seed value
as a parameter and the other taking no parameters and using the
current time as a seed.
random Constructor
public random()
This constructor creates a new random number generator
that is seeded based on the current time.
random Constructor
public random(long seed)
This constructor creates a new random number generator
based on the specified seed value. A program can reset the seed
of an already created instance by using the setSeed
method.
Parameters: seed-the
seed value.
nextDouble
public double nextDouble()
This method retrieves the next number from the random
number generator. The number will be a pseudo-random, uniformly
distributed double between
0.0D and 1.0D.
Returns: A randomly distributed double
between 0.0D and 1.0D.
nextFloat
public float nextFloat()
This method retrieves the next number from the random
number generator. The number will be a pseudo-random, uniformly
distributed float between
0.0F and 1.0F.
Returns: A randomly distributed float
between 0.0F and 1.0F.
nextGaussian
public synchronized double nextGaussian()
This method retrieves the next value from the pseudo-random
number generator. The value will be returned as a Gaussian-distributed
double that has a mean of
0 and a standard deviation of 1.
Returns: A Gaussian-distributed double.
nextInt
public int nextInt()
This method retrieves the next number from the random
number generator. The number will be a pseudo-random int
with a value that is uniformly distributed among all possible
int values.
Returns: A randomly distributed int.
nextLong
public long nextLong()
This method retrieves the next number from the random
number generator. The number will be a pseudo-random long
with a value that is uniformly distributed among all possible
long values.
Returns: A randomly distributed long.
setSeed
public synchronized void setSeed(long
seed)
This method sets a seed value for the pseudo-random
number generator. The seed value is used to determine the values
that are generated. By setting a specific seed value, the random
number generator can be coerced into generating a specific sequence
of values.
Parameters: seed-the
seed value.
Extends: Vector
The Stack class implements a simple last-in-first-out
stack. An item is stored on a stack by "pushing" it
onto the stack. An item may subsequently be "popped"
off the stack and used. The item popped off a stack will always
be the most recently pushed item.
Stack Constructor
public Stack()
This is the default constructor.
empty
public boolean empty()
This method can be used to determine if the stack
contains items.
Returns: true
if the stack is empty; false
otherwise.
peek
public Object peek()
This method can be used to peek at the top item on
the stack. It is similar to pop
but does not remove the item from the stack.
Returns: The item at the top of the stack.
Throws: EmptyStackException
if the stack is empty.
pop
public Object pop()
This method retrieves the last item added to the stack.
To examine, but not remove, the top item in the stack use the
peek method.
Returns: The item at the top of the stack.
Throws: EmptyStackException
if the stack is empty.
push
public Object push(Object item)
This method adds a new item to the stack.
Parameters: item-the
item to push onto the stack.
Returns: The item that was pushed onto the stack.
search
public int search(Object o)
This method examines the stack to see if the specified
object is in the stack.
Parameters: o-the
object to search for.
Returns: The distance from the top of the stack,
or -1 if the item is not
in the stack.
Extends: Object
Implements: Enumeration
A StringTokenizer can be used to parse a string into its constituent
tokens. For example, each word in a sentence could be considered
a token. However, the StringTokenizer class goes beyond the parsing
of sentences. You can create a fully customized tokenizer by specifying
the set of token delimiters when the string tokenizer is created.
StringTokenizer Constructor
public StringTokenizer(String str, String
delim, boolean returnTokens)
This constructor creates a new instance based on the
string to be tokenized, the set of delimiters, and a flag indicating
if delimiters should be returned as tokens.
Parameters:
str-the string to
be tokenized.
delim-a string containing
the delimiters to use when tokenizing the string.
returnTokens-true
if the string tokenizer should return delimiters as tokens; false
if not.
StringTokenizer Constructor
public StringTokenizer(String str, String
delim)
This constructor creates a new instance based on the
string to be tokenized and a set of delimiters.
Parameters:
str-the string to be tokenized.
delim-a string containing
the delimiters to use when tokenizing the string.
StringTokenizer Constructor
public StringTokenizer(String str)
This constructor creates a new instance based on the
string to be tokenized and the default set of delimiters. The
default delimiters are the space, tab, newline, and carriage-return
characters.
countTokens
public int countTokens()
This method returns the number of remaining tokens.
Returns: The quantity of tokens remaining in the
string being tokenized.
hasMoreElements
public boolean hasMoreElements()
This method can be used to determine if the string
tokenizer contains more elements (tokens). This method is identical
to hasMoreTokens and is a
member of StringTokenizer
because StringTokenizer implements
the Enumeration interface.
Returns: true
if there are more elements; false
otherwise.
hasMoreTokens
public boolean hasMoreTokens()
This method can be used to determine if the string
tokenizer contains more tokens. It is identical to hasMoreElements.
Returns: true
if there are more tokens; false
otherwise.
nextElement
public Object nextElement()
This method overrides nextElement
in the Enumeration interface
and exists because StringTokenizer
implements that interface. It is identical to nextToken
and returns the next token in the enumeration.
Returns: The next token in the enumeration.
Throws: NoSuchElementException
if there are no more elements.
nextToken
public String nextToken()
This method returns the next token in the string that
is being tokenized. It is typically used inside a loop that processes
each token.
Returns: The next token in the string being tokenized.
Throws: NoSuchElementException
if there are no more tokens.
nextToken
public String nextToken(String delim)
This method changes the set of delimiter characters
and then returns the next token. The new delimiter set will remain
in effect after this method completes.
Parameters: delim-a
string containing the new set of delimiters.
Returns: The next token in the string being tokenized.
Throws: NoSuchElementException
if there are no more tokens.
Extends: Object
Implements: Cloneable
A vector is analogous to a linked list in other languages or class
libraries. A vector stores items of type Object so it can be used
to store instances of any Java class. A single vector may store
different elements that are instances of different classes.
Vector Constructor
public Vector(int initialCapacity, int
capacityIncrement)
This constructor will create a new instance of a vector
with space for initialCapacity
elements initially. Memory for additional elements will be allocated
in blocks that will each hold capacityIncrement
elements.
Parameters:
initialCapacity-the number
of elements to allocate space for when the object is created.
capacityIncrement-the number
of additional elements to allocate space for whenever additional
space is needed.
Vector Constructor
public Vector(int initialCapacity)
This constructor will create a new instance of a vector
with space for initialCapacity
elements initially. Whenever a new element is added that would
have exceeded this capacity, the size of the vector is doubled.
Parameters: initialCapacity-the
number of elements to allocate space for when the object is created.
Vector Constructor
public constructorVector()
This constructor will create a new instance of a vector.
Initially, the vector will have room for storing 10 elements,
but this will increase automatically to accommodate new elements.
Whenever a new element is added that would have exceeded this
capacity, the size of the vector is doubled.
Member Variables
protected int capacityIncrement
This member stores the amount by which the vector
will be incremented each time it needs to grow. If capacityIncrement
is 0, the buffer does not
grow by a fixed amount but instead doubles whenever it needs to
grow.
protected int elementCount
This member stores the number of elements in the vector.
protected Object elementData[]
This member is the array where the Vector
elements are stored.
addElement
public final synchronized void addElement(Object
obj)
This method is used to insert new elements into the
vector; a vector can store objects of different types.
Parameters: obj-the
object to add to the vector.
capacity
public final int capacity()
This method returns the number of elements that will
fit in the vector before more space is allocated.
Returns: The number of elements that will fit in
the currently allocated portion of the vector.
clone
public synchronized Object clone()
This method overrides clone
in Object and will clone
the vector. Only the vector itself is cloned; the elements of
the vector are not cloned.
Returns: A cloned copy of the vector.
contains
public final boolean contains(Object
elem)
This method determines if an object is stored in a
vector.
Returns: true
if the object is stored in the vector; false
otherwise.
copyInto
public final synchronized void copyInto(Object
anArray[])
This method copies the elements of the vector into
an array.
Parameters: anArray-the
array into which the vector elements will be copied.
elementAt
public final synchronized Object elementAt(int
index)
This method retrieves the element located at the specified
index within the vector.
Parameters: index-the
zero-based index number of the element to retrieve.
Returns: The element at the specified zero-based
index.
Throws: ArrayIndexOutOfBoundsException
if an invalid index is specified.
elements
public final synchronized Enumeration
elements()
This method returns an Enumeration
of the elements in the vector, making it easy to iterate through
the elements.
Returns: An Enumeration
consisting of all the elements in the vector.
ensureCapacity
public final synchronized void ensureCapacity(int
minCapacity)
This method ensures that the vector has at least the
specified minimum capacity. If the current capacity of the vector
is less than minCapacity,
the size of the vector is increased to hold at least minCapacity.
Parameters: minCapacity-the
minimum capacity of the vector.
firstElement
public final synchronized Object firstElement()
This method retrieves the first element in the vector.
If the vector is empty, an exception is thrown. It performs the
same function as elementAt(0).
Returns: The element at the specified zero-based
index.
Throws: NoSuchElementException
if the vector is empty.
indexOf
public final int indexOf(Object elem)
This method searches the vector and returns the zero-based
index number of the first matching object.
Parameters: elem-the
element to find the index of.
Returns: The element number of the first element
that matches elem, or -1
if no match is found.
indexOf
public final synchronized int indexOf(Object
elem, int index)
This method finds the first element in the vector
that matches elem starting
at the element given by index.
It is very useful for traversing a vector searching for all elements
matching a specific object.
Parameters:
elem-the element to find
the index of.
index-the index number at
which to start the search.
Returns: The element number of the first element
that matches elem, or -1
if no match is found.
insertElementAt
public final synchronized void insertElementAt(Object
obj,
int index)
This method, like addElement,
is used to add a new element to a vector. However, this method
can be used to specify where in the vector the new element should
be added. All Vector elements
with index numbers greater than or equal to index
are moved to make room for the new element.
Parameters:
obj-the object to add to
the vector.
index-the zero-based index
at which the object is to be inserted.
Throws: ArrayIndexOutOfBoundsException
if the specified index is invalid.
isEmpty
public final boolean isEmpty()
This method is used to determine if the vector contains
any elements.
Returns: true
if the vector has no elements; false
otherwise.
lastElement
public final synchronized Object lastElement()
This method retrieves the last element in the vector.
If the vector is empty an exception is thrown.
Returns: The element at the specified zero-based
index.
Throws: NoSuchElementException
if the vector is empty.
lastIndexOf
public final int lastIndexOf(Object elem)
This method searches the vector and returns the zero-based
index number of the last matching object.
Parameters: elem-the
element to find the index of.
Returns: The element number of the last element
that matches elem, or -1
if no match is found.
lastIndexOf
public final synchronized int lastIndexOf(Object
elem, int index)
This method finds the last element in the vector that
matches elem starting at
the element given by index.
It is very useful for traversing a vector backward searching for
all elements matching a specific object.
Parameters:
elem-the element to find
the last index of.
index-the index number at
which to start the search.
Returns: The element number of the last element
that matches elem, or -1
if no match is found.
removeAllElements
public final synchronized void removeAllElements()
This method can be used to remove all elements from
the vector.
removeElement
public final synchronized boolean removeElement(Object
obj)
This method can be used to remove a specific element
from the vector. Only the first element that matches obj
is removed.
Parameters: obj-the
object to remove.
Returns: true
if the element was found and deleted; false
otherwise.
removeElementAt
public final synchronized void removeElementAt(int
index)
This method removes the element at the specified zero-based
index.
Parameters: index-the
index number of the element to remove from the vector.
Throws: ArrayIndexOutOfBoundsException
if the specified index is invalid.
setElementAt
public final synchronized void setElementAt(Object
obj, int index)
This method replaces an element in the vector with
another element.
Parameters:
obj-the object to be placed
in the vector.
index-the index number of
the element to be replaced.
Throws: ArrayIndexOutOfBoundsException
if the specified index is invalid.
setSize
public final synchronized void setSize(int
newSize)
This method sets the size of the vector. If the specified
size makes the vector too small to hold its current elements,
elements from the end of the vector are removed. If the new size
is larger than the current size, empty elements are added at the
end of the vector.
Parameters: newSize-the
desired size of the vector.
size
public final int size()
The method returns the number of elements currently
in the vector.
Returns: The number of elements in the vector.
toString
public final synchronized String toString()
This method overrides the toString
method in Object and formats
the contents of the vector as a string.
Returns: A string representation of the vector.
trimToSize
public final synchronized void trimToSize()
This method will remove any excess capacity from the
vector by resizing it to hold only the quantity of elements it
currently holds. If new items are added, the size of the vector
will be increased.
Extends: RuntimeException
This exception signals when the stack is empty.
Extends: RuntimeException
This exception signals when an enumeration is empty.
|