Java Vector setSize()方法及实例
Java.util.Vector. setSize() 是Vector类的一个方法,用来设置向量的新大小。如果向量的新大小大于当前大小,那么空元素将被添加到向量中,如果新大小小于当前大小,那么所有高阶元素将被删除。
该方法将向量的大小修改为newSize,并且不返回任何东西。
语法
public void setSize(int newSize)
参数: 该方法接受一个矢量的强制性参数 newSize。
返回类型: NA
异常: ArrayIndexOutOfBoundsException
注意: 如果新的大小是负数,那么它将抛出 运行时错误。
例子1 :
// Java Program to Illustrate setSize() method
// of Vector class
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of Vector class
// Declaring object of string type
Vector<String> v = new Vector<String>();
// Inserting elements into the vector
// using add() method
// Custom input elements
v.add("Geeks");
v.add("for");
v.add("Geeks");
v.add("Computer");
v.add("Science");
v.add("Portal");
// Printing vector before calling setSize() method
System.out.println("Initially");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
// Setting new custom size
v.setSize(8);
// Printing vector after calling setSize()
System.out.println("\nAfter using setSize()");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
}
}
输出
Initially
Vector: [Geeks, for, Geeks, Computer, Science, Portal]
Size: 6
After using setSize()
Vector: [Geeks, for, Geeks, Computer, Science, Portal, null, null]
Size: 8
例2: 当新尺寸为正时
// Java program to Illustrate setSize() method
// of Vector class
// Where size is positive
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// amin driver method
public static void main(String[] args)
{
// Creating vector object of string type
Vector<String> v = new Vector<String>();
// Inserting elements into the vector
//. using add() method
// Custom input elements
v.add("Geeks");
v.add("for");
v.add("Geeks");
v.add("Computer");
v.add("Science");
v.add("Portal");
// Printing vector before calling setSize()
System.out.println("Initially");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
// Setting new size
v.setSize(4);
// Printing vector after calling setSize()
System.out.println("\nAfter using setSize()");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
}
}
输出
Initially
Vector: [Geeks, for, Geeks, Computer, Science, Portal]
Size: 6
After using setSize()
Vector: [Geeks, for, Geeks, Computer]
Size: 4
例3: 当新尺寸为负数时
// Java program to Illustrate setSize() method
// of vector class
// Where size is negative
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating Vector class object of string type
Vector<String> v = new Vector<String>();
// Inserting elements into the vector
// using add() method
v.add("Geeks");
v.add("for");
v.add("Geeks");
v.add("Computer");
v.add("Science");
v.add("Portal");
// Printing vector before calling setSize()
System.out.println("Initially");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
// Try block to check for exceptions
try {
// Setting new size
v.setSize(-8);
}
// Catch block to handle exceptions
catch (Exception e) {
// Display message when exceptions occurred
System.out.println("Trying to change "
+ "size to '-8'\n" + e);
}
}
}
输出

极客教程