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


Java Array setShort()用法及代碼示例


java.lang.reflect.Array.setShort()是Java中的內置方法,用於將指定的short值設置為給定對象數組的指定索引。

句法

Array.setShort(Object []array,int index, short value)

參數:此方法采用3個參數:


  • array:這是要更新的Object類型的數組。
  • index:這是要更新的數組的索引。
  • value:這是要在給定數組的給定索引處設置的short值。

異常:此方法引發以下異常:

  • NullPointerException –當數組為null時。
  • IllegalArgumentException–當給定的對象數組不是數組時。
  • ArrayIndexOutOfBoundsException–如果給定的索引不在數組的大小範圍內。
  • 下麵是Array.setShort()方法的實現:

    示例1:

    // Java code to demonstrate  
    // setShort() method of Array class 
      
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) { 
              
            // Declaring and defining Short array 
            short b[] = {1,2,3,4}; 
            System.out.print("Befor Set : "); 
            // printing the array 
            for(short x : b){ 
                System.out.print(x + " "); 
            } 
            short value = 10; 
            // set method of class Array 
            Array.setShort(b, 1, value); 
              
            System.out.print("\nAfter Set : "); 
            // printing array 
            for(short x : b){ 
                System.out.print(x + " "); 
            } 
              
        } 
    }
    輸出:
    Befor Set : 1 2 3 4 
    After Set : 1 10 3 4
    

    示例2:演示java.lang.NullPointerException

    // Java code to demonstrate  
    // setShort() method of Array class 
      
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) { 
              
            // Declaring and defining Short array to null 
            short b[] = null; 
            try{ 
                short s = 10; 
                Array.setShort(b,5,s); 
            } 
            catch(Exception e){ 
                System.out.println("Exception : " + e); 
            } 
              
        } 
    }
    輸出:
    Exception : java.lang.NullPointerException
    

    示例3:演示java.lang.ArrayIndexOutOfBoundsException

    // Java code to demonstrate  
    // setShort() method of Array class 
      
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) { 
              
            // Declaring and defining Short array 
            short b[] = {1,2,3,4}; 
            try{ 
                short s = 10; 
                Array.setShort(b,5,s); 
            } 
            catch(Exception e){ 
                System.out.println("Exception : " + e); 
            } 
              
        } 
    }
    輸出:
    Exception : java.lang.ArrayIndexOutOfBoundsException
    

    示例4:演示java.lang.IllegalArgumentException

    // Java code to demonstrate  
    // setShort() method of Array class 
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) { 
              
            // Declaring and defining Short variable 
            short b = 1; 
            try{ 
                short s = 10; 
                Array.setShort(b,5,s); 
            } 
            catch(Exception e){ 
                System.out.println("Exception : " + e); 
            } 
              
        } 
    }
    輸出:
    Exception : java.lang.IllegalArgumentException: Argument is not an array
    


相關用法


注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 Array setShort() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。