Java 数组getFloat()方法

Java 数组getFloat()方法

java.lang.reflect.Array.getFloat() 是Java中Array类的一个内置方法,用于将指定Array中存在于给定索引的元素以Float形式返回。

语法:

Array.getFloat(Object []array, int index)

参数: 该方法接受两个强制性参数。

  • array: 对象数组,其索引将被返回。
  • index: 给定数组的特定索引。在给定数组中位于 “index “的元素将被返回。

返回值: 该方法以字节形式返回数组中的元素。

异常: 该方法会产生以下异常。

  • NullPointerException – 当数组为空时。
  • IllegalArgumentException – 当给定的对象数组不是一个数组时。
  • ArrayIndexOutOfBoundsException – 如果给定的索引不在数组的大小范围内。

注意: 由于返回类型是float,所以不需要进行类型转换。

下面的程序说明了数组类的getFloat()方法。

程序1 :

// Java code to demonstrate getFloat() method of Array class
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // Declaring and defining a float array
        float a[] = { 1.0f, 2.0f, 3.0f };
  
        // Traversing the array
        for (int i = 0; i < 3; i++) {
  
            // Array.getBoolean() method
  
            float x = Array.getFloat(a, i);
            // Printing the values
            System.out.print(x + " ");
        }
    }
}

输出:

1.0 2.0 3.0 

程序2 :

// Java code to demonstrate getFloat() method of Array class
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // Declaring and defining a float array
        float a[] = { 1.0f, 2.0f, 3.0f };
  
        try {
            float x = Array.getFloat(a, 4);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

输出:

Exception : java.lang.ArrayIndexOutOfBoundsException

程序3 :

// Java code to demonstrate getFloat() method of Array class
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // Declaring and defining a float array as null
        float a[] = null;
  
        try {
            float x = Array.getFloat(a, 4);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

输出:

Exception : java.lang.NullPointerException

程序4 :

// Java code to demonstrate getFloat() method of Array class
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // Declaring and defining a float variable
        float a = 1.0f;
  
        try {
            float x = Array.getFloat(a, 4);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

输出:

Exception : java.lang.IllegalArgumentException: Argument is not an array

程序5 :

// Java code to demonstrate getFloat() 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 {
            float x = Array.getFloat(s, 4);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

输出:

Exception : java.lang.IllegalArgumentException: Argument is not an array

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程