Java Vector setSize() MethodThe 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 1Test it NowOutput: 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 2Test it NowOutput: Components of a vector: [White, Green, Black] Components of a vector after setting new size: [White, Green, Black, null, null, null] Example 3Test it NowOutput: 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 |
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