當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。