当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Vector setSize()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 Vector setSize() method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。