📄 javautil.doc9.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javautil.doc8.html">Prev</a> | <a href="javautil.doc10.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="7669"></a>
<center><h1>21.11 The Class <code>java.util.Vector</code></h1></center>
<a name="22449"></a>
A <code>Vector</code>, like an array, contains items that can be accessed using an integer
index. However, the size of a <code>Vector</code> can grow and shrink as needed to accommodate
adding and removing items after the <code>Vector</code> has been created.
<p><pre><a name="7670"></a>public class <code><b>Vector</b></code> implements Cloneable {
<a name="7671"></a> protected Object[] <code><b>elementData</b></code>;
<a name="7672"></a> protected int <code><b>elementCount</b></code>;
<a name="7673"></a> protected int <code><b>capacityIncrement</b></code>;
<a name="7674"></a> public <code><b>Vector</b></code>(int initialCapacity, int capacityIncrement);
<a name="7675"></a> public <code><b>Vector</b></code>(int initialCapacity);
<a name="7676"></a> public <code><b>Vector</b></code>();
<a name="22275"></a> public final String <code><b>toString</b></code>();
<a name="22281"></a> public Object <code><b>clone</b></code>();
<a name="22287"></a> public final Object <code><b>elementAt</b></code>(int index)
<a name="22521"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="22328"></a> public final void <code><b>setElementAt</b></code>(Object obj, int index)
<a name="22526"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="22288"></a> public final Object <code><b>firstElement</b></code>()
<a name="22531"></a> throws NoSuchElementException;
<a name="22533"></a> public final Object <code><b>lastElement</b></code>()
<a name="22289"></a> throws NoSuchElementException;
<a name="22334"></a> public final void <code><b>addElement</b></code>(Object obj);
<a name="22292"></a> public final void <code><b>insertElementAt</b></code>(Object obj, int index)
<a name="22584"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="22346"></a> public final boolean <code><b>removeElement</b></code>(Object obj);
<a name="22340"></a> public final void <code><b>removeElementAt</b></code>(int index)
<a name="22589"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="22295"></a> public final void <code><b>removeAllElements</b></code>();
<a name="22322"></a> public final boolean <code><b>isEmpty</b></code>();
<a name="22310"></a> public final int <code><b>size</b></code>();
<a name="22301"></a> public final void <code><b>setSize</b></code>(int newSize);
<a name="22302"></a> public final int <code><b>capacity</b></code>();
<a name="22316"></a> public final void <code><b>ensureCapacity</b></code>(int minCapacity);
<a name="7678"></a> public final void <code><b>trimToSize</b></code>();
<a name="22757"></a> public final void <code><b>copyInto</b></code>(Object anArray[])
<a name="22459"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="7684"></a> public final Enumeration <code><b>elements</b></code>();
<a name="7685"></a> public final boolean <code><b>contains</b></code>(Object elem);
<a name="7686"></a> public final int <code><b>indexOf</b></code>(Object elem);
<a name="22752"></a> public final int <code><b>indexOf</b></code>(Object elem, int index)
<a name="7687"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="7688"></a> public final int <code><b>lastIndexOf</b></code>(Object elem);
<a name="22747"></a> public final int <code><b>lastIndexOf</b></code>(Object elem, int index)
<a name="7689"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="7701"></a>}
</pre><a name="7702"></a>
<p><font size=+1><strong>21.11.1 </strong> <code>protected Object[] <code><b>elementData</b></code>;</code></font>
<p>
<a name="22468"></a>
Internally, a <code>Vector</code> keeps its elements in an array that is at least large enough to
contain all the elements.
<p><a name="7703"></a>
<p><font size=+1><strong>21.11.2 </strong> <code>protected int <code><b>elementCount</b></code>;</code></font>
<p>
<a name="22469"></a>
This field holds the number of items currently in this <code>Vector</code> object. Components
<code>elementData[0]</code> through <code>elementData[elementCount-1]</code> are the actual items.
<p><a name="7704"></a>
<p><font size=+1><strong>21.11.3 </strong> <code>protected int <code><b>capacityIncrement</b></code>;</code></font>
<p>
<a name="22476"></a>
When the method <code>ensureCapacity</code> <a href="javautil.doc9.html#22436">(§21.11.22)</a> must increase the size of the data
array in the field <code>elementData</code> (by creating a new array), it increases the size by
at least the amount in <code>capacityIncrement</code>; but if <code>capacityIncrement</code> is zero,
then it at least doubles the size of the data array.
<p><a name="7705"></a>
<p><font size=+1><strong>21.11.4 </strong> <code>public <code><b>Vector</b></code>(int initialCapacity, int capacityIncrement)</code></font>
<p>
<a name="22482"></a>
This constructor initializes a newly created <code>Vector</code> so that its internal data array
has size <code>initialCapacity</code> and its standard capacity increment is the value of
<code>capacityIncrement</code>. Initially, the <code>Vector</code> contains no items.
<p><a name="7706"></a>
<p><font size=+1><strong>21.11.5 </strong> <code>public <code><b>Vector</b></code>(int initialCapacity)</code></font>
<p>
<a name="22494"></a>
This constructor initializes a newly created <code>Vector</code> so that its internal data array
has size <code>initialCapacity</code> and its standard capacity increment is zero. Initially,
the <code>Vector</code> contains no items.
<p><a name="7707"></a>
<p><font size=+1><strong>21.11.6 </strong> <code>public <code><b>Vector</b></code>()</code></font>
<p>
<a name="22500"></a>
This constructor initializes a newly created <code>Vector</code> so that its internal data array
has size <code>10</code> and its standard capacity increment is zero. Initially the <code>Vector</code> contains
no items.
<p><a name="22354"></a>
<p><font size=+1><strong>21.11.7 </strong> <code>public final String <code><b>toString</b></code>()</code></font>
<p>
<a name="22810"></a>
This <code>Vector</code> is represented in string form as a list of its items, enclosed in ASCII
square brackets and separated by the ASCII characters "<code>, </code>" (comma and space).
The <code>toString</code> method is used to convert the items to strings; a null reference is
rendered as the string "<code>null</code>".
<p><a name="22846"></a>
The example fragment:<p>
<pre><a name="22847"></a>
Vector v = new Vector();
<a name="22850"></a>v.addElement("Canberra");
<a name="22854"></a>v.addElement("Cancun");
<a name="22858"></a>v.addElement("Canandaigua");
<a name="25569"></a>System.out.println(v.toString());
<a name="25570"></a>
</pre><a name="25565"></a>
produces the output:
<p><pre><a name="22865"></a>[Canberra, Cancun, Canandaigua]
</pre><a name="22814"></a>
Overrides the <code>toString</code> method of <code>Object</code> <a href="javalang.doc1.html#1152">(§20.1.2)</a>.<p>
<a name="22363"></a>
<p><font size=+1><strong>21.11.8 </strong> <code>public Object <code><b>clone</b></code>()</code></font>
<p>
<a name="22808"></a>
A copy of this <code>Vector</code> is constructed and returned. The copy will contains a reference
to a clone of the internal data array, not a reference to the original internal
data array of this <code>Vector</code>.
<p><a name="22829"></a>
Overrides the <code>clone</code> method of <code>Object</code> <a href="javalang.doc1.html#14934">(§20.1.5)</a>.<p>
<a name="22392"></a>
<p><font size=+1><strong>21.11.9 </strong> <code>public final Object <code><b>elementAt</b></code>(int index)<br>throws <code>IndexOutOfBoundsException</code></code></font>
<p>
<a name="22511"></a>
The item of this <code>Vector</code> with the specified <code>index</code> is returned.
<p><a name="22516"></a>
If the <code>index</code> is negative or not less than the current size of this <code>Vector</code>, an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="22393"></a>
<p><font size=+1><strong>21.11.10 </strong> <code>public final void <code><b>setElementAt</b></code>(Object obj, int index)<br>throws <code>IndexOutOfBoundsException</code></code></font>
<p>
<a name="22550"></a>
The item of this <code>Vector</code> with the specified <code>index</code> is replaced with <code>obj</code>, so that <code>obj</code>
is now the item at the specified <code>index</code> within this <code>Vector</code>.
<p><a name="22551"></a>
If the <code>index</code> is negative or not less than the current size of this <code>Vector</code>, an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="22394"></a>
<p><font size=+1><strong>21.11.11 </strong> <code>public final Object <code><b>firstElement</b></code>()<br>throws NoSuchElementException</code></font>
<p>
<a name="22556"></a>
If this <code>Vector</code> is empty, a <code>NoSuchElementException</code> is thrown. Otherwise, the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -