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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。