Java.util.Vector.setSize()是Vector类的一种方法,用于设置矢量的新大小。如果向量的新大小大于当前大小,则将空元素添加到向量,如果向量的新大小小于当前大小,则删除所有更高阶的元素。如果新大小为负,则将出现运行时错误:ArrayIndexOutOfBoundsException
用法:
public void setSize(int newSize)
参数:此方法接受向量的必需参数newSize。
返回类型:此方法将Vector的大小修改为newSize,并且不返回任何内容。
异常:如果新大小为负,则此方法将引发ArrayIndexOutOfBoundsException。
下面的示例说明Vector.setSize()方法的用法方式:
示例1:
// Java program to understand
// about vector.setSize() method
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// creating vector type object
Vector<String> v = new Vector<String>();
// inserting elements into the vector
v.add("Geeks");
v.add("for");
v.add("Geeks");
v.add("Ankit");
v.add("MNNIT");
v.add("Allahabad");
// 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(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, Ankit, MNNIT, Allahabad] Size: 6 After using setSize() Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad, null, null] Size: 8
示例2:
// Java program to understand
// about vector.setSize() method
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// creating vector type object
Vector<String> v = new Vector<String>();
// inserting elements into the vector
v.add("Geeks");
v.add("for");
v.add("Geeks");
v.add("Ankit");
v.add("MNNIT");
v.add("Allahabad");
// 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, Ankit, MNNIT, Allahabad] Size: 6 After using setSize() Vector: [Geeks, for, Geeks, Ankit] Size: 4
当新尺寸为负数时
// Java program to understand
// about vector.setSize() method
// because vector is present in this package
import java.util.*;
// Driver Code
public class GFG {
// main method begins here
public static void main(String[] args)
{
// creating vector type object
Vector<String> v = new Vector<String>();
// inserting elements into the vector
v.add("Geeks");
v.add("for");
v.add("Geeks");
v.add("Ankit");
v.add("MNNIT");
v.add("Allahabad");
// printing vector before calling setSize()
System.out.println("Initially");
System.out.println("Vector: " + v);
System.out.println("Size: " + v.size());
try {
// setting new size
v.setSize(-8);
}
catch (Exception e) {
System.out.println("Trying to change "
+ "size to '-8'\n"
+ e);
}
}
}
输出:
Initially Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad] Size: 6 Trying to change size to '-8' java.lang.ArrayIndexOutOfBoundsException: -8
相关用法
- Java Stack setSize()用法及代码示例
- Java Vector contains()用法及代码示例
- Java Vector add()用法及代码示例
- Java Vector set()用法及代码示例
- Java Vector get()用法及代码示例
- Java Vector isEmpty()用法及代码示例
- Java Vector hashCode()用法及代码示例
- Java Vector firstElement()用法及代码示例
- Java Vector elementAt()用法及代码示例
- Java Vector equals()用法及代码示例
- Java Vector copyInto()用法及代码示例
- Java Vector elements()用法及代码示例
- Java Vector size()用法及代码示例
- Java Vector insertElementAt()用法及代码示例
- Java Vector addElement()用法及代码示例
注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 Vector setSize() method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。