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


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


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

用法:

Array.setDouble(Object []array, int index, double value)

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


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

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

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

    示例1:

    // Java code to demonstrate setDouble() 
    // method of Array class 
      
    import java.lang.reflect.Array; 
      
    public class GfG { 
      
        // main method 
        public static void main(String[] args) 
        { 
            // Declaring and defining double array 
            double f[] = { 1.0, 2.0, 3.0 }; 
      
            // printing the array 
            System.out.print("Before Set : "); 
            for (double x : f) { 
                System.out.print(x + " "); 
            } 
      
            double value = 400.0; 
      
            // setDouble method of class Array 
            Array.setDouble(f, 1, value); 
      
            // printing array 
            System.out.print("\nAfter Set : "); 
            for (double x : f) { 
                System.out.print(x + " "); 
            } 
        } 
    }
    輸出:
    Before Set : 1.0 2.0 3.0 
    After Set : 1.0 400.0 3.0
    

    示例2:演示java.lang.NullPointerException

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

    示例3:演示java.lang.ArrayIndexOutOfBoundsException

    // Java code to demonstrate setDouble() 
    // method of Array class 
      
    import java.lang.reflect.Array; 
      
    public class GfG { 
      
        // main method 
        public static void main(String[] args) 
        { 
            // Declaring and defining double array 
            double b[] = { 1.0, 2.0, 3.0 }; 
      
            try { 
                double c = 1.0; 
      
                // setDouble method of class Array 
                // passing index 5 when size is 3 
                Array.setDouble(b, 5, c); 
            } 
            catch (Exception e) { 
                System.out.println("Exception : " + e); 
            } 
        } 
    }
    輸出:
    Exception : java.lang.ArrayIndexOutOfBoundsException
    

    示例4:演示java.lang.IllegalArgumentException

    // Java code to demonstrate setDouble() 
    // method of Array class 
      
    import java.lang.reflect.Array; 
      
    public class GfG { 
      
        // main method 
        public static void main(String[] args) 
        { 
            // Declaring and defining double variable 
            double b = 2.0; 
            try { 
                double c = 1.0; 
                // setDouble method of class Array 
                // passing a variable as parameter 
                // when an array is required instead 
                Array.setDouble(b, 5, c); 
            } 
            catch (Exception e) { 
                System.out.println("Exception : " + e); 
            } 
        } 
    }
    輸出:
    Exception : java.lang.IllegalArgumentException: Argument is not an array
    


相關用法


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