Java中的向量 setSize() 方法与示例
Java.util.Vector .setSize()是 Vector 类的一个方法,用于设置向量的新大小。如果向量的新大小大于当前大小,则将空元素添加到向量中,如果新大小小于当前大小,则删除所有高阶元素。
此方法将 Vector 的大小修改为 newSize 并且不返回任何内容。
句法:
public void setSize(int newSize)
参数:此方法接受向量的强制参数newSize 。
返回类型: NA
例外: ArrayIndexOutOfBoundsException
Note: If new size is negative then it will throw Runtime Error
示例 1:
Java
// 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 v = new Vector();
// 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());
}
}
Java
// 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 v = new Vector();
// 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());
}
}
Java
// 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 v = new Vector();
// 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 occured
System.out.println("Trying to change "
+ "size to '-8'\n" + e);
}
}
}
输出
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
// 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 v = new Vector();
// 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
// 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 v = new Vector();
// 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 occured
System.out.println("Trying to change "
+ "size to '-8'\n" + e);
}
}
}
输出: