- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Vector setSize() Method
Description
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.
Declaration
Following 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 Value
NA
Exception
ArrayIndexOutOfBoundsException − This exception is thrown if the new size is negative.
Setting Size of a Vector of Integer 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 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 = null
Setting 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 = 3
Setting 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