Java中的数组setFloat()方法

null

这个 JAVA朗,反思一下。大堆setFloat() 是Java中的内置方法,用于将指定的浮点值更改为给定对象数组的指定索引。

语法:

Array.setFloat(Object []array, int index, float value)

参数: 此方法需要三个参数:

  • 数组: 这是一个要更新的Object类型的数组。
  • 索引: 这是要更新的数组的索引。
  • 价值: 这是要在给定时间设置的浮点值 指数 给定的 大堆 .

返回值: 因为这个方法有一个void返回类型,所以它不返回任何值。更新将反映作为参数传递的对象数组。

例外情况: 此方法引发以下异常:

  • 空指针异常 –当数组为空时。
  • 非法数据异常 –当给定的对象数组不是数组时。
  • 数组下标越界异常 –如果给定索引不在数组大小的范围内。

    下面是数组的实现。setFloat()方法:

    项目1:

    // Java code to demonstrate
    // setFloat() method of Array class
    import java.lang.reflect.Array;
    public class GfG {
    // main method
    public static void main(String[] args)
    {
    // Declaring and defining float array
    float f[] = { 1 .0f, 2 .0f, 3 .0f };
    System.out.print( "Before Set : " );
    // printing the array
    for ( float x : f) {
    System.out.print(x + " " );
    }
    float value = 4 .0f;
    // setFloat method of class Array
    Array.setFloat(f, 1 , value);
    System.out.print( "After Set : " );
    // printing array
    for ( float x : f) {
    System.out.print(x + " " );
    }
    }
    }

    
    

    输出:

    Before Set : 1.0 2.0 3.0 
    After Set : 1.0 4.0 3.0
    

    项目2: 来演示java。lang.NullPointerException

    // Java code to demonstrate
    // setFloat() method of Array class
    import java.lang.reflect.Array;
    public class GfG {
    // main method
    public static void main(String[] args)
    {
    // Declaring and defining float array to null
    float b[] = null ;
    try {
    float c = 1 .0f;
    // Passing null array as parameter
    Array.setFloat(b, 5 , c);
    }
    catch (Exception e) {
    System.out.println( "Exception : " + e);
    }
    }
    }

    
    

    输出:

    Exception : java.lang.NullPointerException
    

    方案3: 来演示java。lang.ArrayIndexOutOfBoundsException

    // Java code to demonstrate setFloat()
    // method of Array class
    import java.lang.reflect.Array;
    public class GfG {
    // main method
    public static void main(String[] args)
    {
    // Declaring and defining float array
    float b[] = { 1 .0f, 2 .0f, 3 .0f };
    try {
    float c = 1 .0f;
    // Passing index as 5 in parameter
    // when the size of array is 3
    Array.setFloat(b, 5 , c);
    }
    catch (Exception e) {
    System.out.println( "Exception : " + e);
    }
    }
    }

    
    

    输出:

    Exception : java.lang.ArrayIndexOutOfBoundsException
    

    方案4: 来演示java。lang.IllegalArgumentException

    // Java code to demonstrate setFloat()
    // method of Array class
    import java.lang.reflect.Array;
    public class GfG {
    // main method
    public static void main(String[] args)
    {
    // Declaring and defining float variable
    float b = 2 .0f;
    try {
    float c = 1 .0f;
    // Passing variable as parameter
    // where an array is expected.
    Array.setFloat(b, 5 , c);
    }
    catch (Exception e) {
    System.out.println( "Exception : " + e);
    }
    }
    }

    
    

    输出:

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

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享