The Java Vector setSize(int newSize) method is used to set the size of this vector. If the new size is greater than the current size, new null items are added to the end of the vector. If the new size is less than the current size, all components at index newSize and greater are discarded.
DeclarationFollowing is the declaration for java.util.Vector.setSize() method
public void setSize(int newSize)Parameters
newSize − This is the new size of the vector
Return ValueNA
ExceptionArrayIndexOutOfBoundsException − This exception is thrown if the new size is negative.
Setting Size of a Vector of Integer ExampleThe following example shows the usage of Java Vector setsize(int) method. We're adding couple of Integers to the Vector object using add() method calls per element and using setSize(int) method, we're setting the new size as greater than vector current sizeand printing all the elements of vector including null values.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Integer> vec = new Vector<>(4); // use add() method to add elements in the vector vec.add(4); vec.add(3); vec.add(2); vec.add(1); // set new size to 8 vec.setSize(8); // let us print all the elements available in vector System.out.println("Added numbers are :- "); for (Integer number : vec) { System.out.println("Number = " + number); } } }Output
Let us compile and run the above program, this will produce the following result.
Added numbers are :- Number = 4 Number = 3 Number = 2 Number = 1 Number = null Number = null Number = null Number = nullSetting Size of a Vector of String Example
The following example shows the usage of Java Vector setsize(int) method. We're adding couple of Integers to the Vector object using add() method calls per element and using setSize(int) method, we're setting the new size as less than vector current size and printing all the elements of vector.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Integer> vec = new Vector<>(4); // use add() method to add elements in the vector vec.add(4); vec.add(3); vec.add(2); vec.add(1); // set new size to 2 vec.setSize(2); // let us print all the elements available in vector System.out.println("Added numbers are :- "); for (Integer number : vec) { System.out.println("Number = " + number); } } }Output
Let us compile and run the above program, this will produce the following result.
Added numbers are :- Number = 4 Number = 3Setting Size of a Vector of Object Example
The following example shows the usage of Java Vector setsize(int) method. We're adding couple of Integers to the Vector object using add() method calls per element and using setSize(int) method, we're setting the new size as same as vector current size and printing all the elements of vector.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Integer> vec = new Vector<>(4); // use add() method to add elements in the vector vec.add(4); vec.add(3); vec.add(2); vec.add(1); // set new size to 4 vec.setSize(4); // let us print all the elements available in vector System.out.println("Added numbers are :- "); for (Integer number : vec) { System.out.println("Number = " + number); } } }Output
Let us compile and run the above program, this will produce the following result.
Added numbers are :- Number = 4 Number = 3 Number = 2 Number = 1
java_util_vector.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4