Java Vector setSize() Method24 Mar 2025 | 2 min read The setSize() method of Java Vector class is used to set the size of a vector. If the new size is greater than the current size, null items are added to the end of the vector. Otherwise, all components at index newSize and greater are discarded. SyntaxFollowing is the declaration of setSize() method: Parameter
ReturnThe setSize() method does not return anything. It only set the new size of the vector. ExceptionsArrayIndexOutOfBoundsException- This method has thrown an exception if the new size of the vector is negative i.e. newSize < 0. Compatibility VersionJava 1.2 and above Example 1Output: Components of the vector: [1, 2, 3] Components of the vector after setting new size: Number = 1 Number = 2 Number = 3 Number = null Number = null Number = null Example 2Output: Components of a vector: [White, Green, Black] Components of a vector after setting new size: [White, Green, Black, null, null, null] Example 3Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -15 at java.base/java.util.Vector.setSize(Vector.java:314) at myPackage.VectorSetSizeExample3.main(VectorSetSizeExample3.java:12) Next TopicJava Vector Size Method |
Java Method The set() method of Java Vector class is used to replace the element at the specified position in the vector with the specified element. Syntax Following is the declaration of set() method: Public E set(int index, E element) Parameter Parameter Description Required/Optional index It is an index of the element which will...
2 min read
Java Method The lastElement() method of Java Vector class is used to get the last component of the vector. Syntax Following is the declaration of lastElement() method: Public E lastElement() Parameter This method does not accept any parameter. Return The lastElement() method returns the last element of the vector i.e. the component...
2 min read
Java Method The subList() method of Java Vector class is used to get a view of the portion of the list between fromIndex, inclusive, and toIndex, exclusive. If fromIndex and toIndex are equal, the returned List is empty. Syntax Following is the declaration of subList() method: Public List<E>...
3 min read
Java Method The add() is a Java Vector class method which is used to insert the specified element in the given Vector. There are two different types of Java add() method which can be differentiated depending on its parameter. These are: Java Vector add(int index, E...
3 min read
Java Method The clone() method of Java Vector class is used to get a clone of the vector which is in use. Syntax: Following is the declaration of clone() method: Public object clone() Parameter: This method does not accept any parameter. Returns: This method returns a clone of the vector which is...
2 min read
Java Method The containsAll() method of Java Vector class is used to check the vector which is in use contains all of the elements in the specified Collection or not. It returns true if this vector contains all of the elements in the Collection, otherwise...
3 min read
Java Method The capacity() method of Java Vector class is used to get the current capacity of the vector which is in use. Syntax: Following is the declaration of capacity() method: Public int capacity() Parameter: This method does not accept any parameter. Returns: This method returns the current capacity of the vector...
2 min read
Java Method The equals() method of Java Vector class used to compare the specified object with this Vector for equality. It returns true if and only if the specified object is also a list, both lists have the same size and all corresponding pairs of...
2 min read
Java Method The elements() method of Java Vector class used to get an enumeration of the elements of the vector which is in use. The returned enumeration object will generate all items in this vector at the same location. Syntax: Following is the declaration of elements() method: public...
2 min read
Java Method The size() method of Java Vector class is used to get the number of components presents in the vector. Syntax Following is the declaration of size() method: public int size() Parameter This method does not accept any parameter. Return The size() method returns the number of components presents in a...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India