java.lang.reflect.Array.getDouble()是Java中Array类的内置方法,用于将指定数组中给定索引处的元素作为Double返回。
句法:
Array.getDouble(Object []array, int index)
参数:此方法接受两个强制性参数:
- array: 要返回其索引的对象数组。
- index: 给定数组的特定索引。返回给定数组中“索引”处的元素。
返回值:此方法以字节为单位返回数组的元素。
异常:此方法引发以下异常:
- NullPointerException –当数组为null时。
- IllegalArgumentException–当给定的对象数组不是数组时。
- ArrayIndexOutOfBoundsException–如果给定的索引不在数组的大小范围内。
注意:不必进行类型转换,因为返回类型为double。
以下示例程序旨在说明Array类的getDouble()方法。
程序1:
// Java code to demonstrate getDouble() method of Array class 
import java.lang.reflect.Array; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // Declaring and defining a double array 
        double a[] = { 1.0, 2.0, 3.0 }; 
  
        // Traversing the array 
        for (int i = 0; i < 3; i++) { 
  
            // Array.getDouble() method 
  
            double x = Array.getDouble(a, i); 
            // Printing the values 
            System.out.print(x + " "); 
        } 
    } 
}
输出:
1.0 2.0 3.0
程序2:
// Java code to demonstrate getDouble() method of Array class 
import java.lang.reflect.Array; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // Declaring and defining a double array 
        double a[] = { 1.0, 2.0, 3.0 }; 
  
        try { 
            double x = Array.getDouble(a, 4); 
        } 
        catch (Exception e) { 
            System.out.println("Exception : " + e); 
        } 
    } 
}
输出:
Exception : java.lang.ArrayIndexOutOfBoundsException
程序3:
// Java code to demonstrate getDouble() method of Array class 
import java.lang.reflect.Array; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // Declaring and defining a double array as null 
        double a[] = null; 
  
        try { 
            double x = Array.getDouble(a, 4); 
        } 
        catch (Exception e) { 
            System.out.println("Exception : " + e); 
        } 
    } 
}
输出:
Exception : java.lang.NullPointerException
程序4:
// Java code to demonstrate getDouble() method of Array class 
import java.lang.reflect.Array; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // Declaring and defining a double variable 
        double a = 1.0f; 
  
        try { 
            double x = Array.getDouble(a, 4); 
        } 
        catch (Exception e) { 
            System.out.println("Exception : " + e); 
        } 
    } 
}
输出:
Exception : java.lang.IllegalArgumentException: Argument is not an array
程序5:
// Java code to demonstrate getDouble() method of Array class 
import java.lang.reflect.Array; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // Declaring and defining a String array 
        String s[] = { "Geeks", "for", "Geeks" }; 
  
        try { 
            double x = Array.getDouble(s, 4); 
        } 
        catch (Exception e) { 
            System.out.println("Exception : " + e); 
        } 
    } 
}
输出:
Exception : java.lang.IllegalArgumentException: Argument is not an array
相关用法
- Java Field getDouble()用法及代码示例
- Java ByteBuffer getDouble()用法及代码示例
- Java Array set()用法及代码示例
- Java Array get()用法及代码示例
- Java Array getBoolean()用法及代码示例
- Java Array getChar()用法及代码示例
- Java Array setInt()用法及代码示例
- Java Array getShort()用法及代码示例
- Java Array getByte()用法及代码示例
- Java Array getFloat()用法及代码示例
- Java CharBuffer array()用法及代码示例
- Java Array getInt()用法及代码示例
- Java Array getLong()用法及代码示例
- Java Array setShort()用法及代码示例
- Java Array setDouble()用法及代码示例
注:本文由纯净天空筛选整理自ShivamKD大神的英文原创作品 Array getDouble() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
