📜  Java中的数组 setByte() 方法

📅  最后修改于: 2022-05-13 01:55:20.728000             🧑  作者: Mango

Java中的数组 setByte() 方法

Java.lang.reflect.Array.setByte() 是Java中的一个内置方法,用于将指定的字节值设置为给定对象数组的指定索引。语法

Array.setByte(Object []array, int index, byte value)

参数:此方法采用 3 个参数:

  • array:这是要更新的 Object 类型的数组。
  • index:这是要更新的数组的索引。
  • value:这是要在给定数组的给定索引处设置的字节值。

异常:此方法抛出以下异常:

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

下面的程序说明了 Array.setByte() 方法:程序 1:

Java
// Java code to demonstrate
// setByte() method of Array class
 
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
 
        // Declaring and defining Byte array
        byte b[] = { 1, 2, 3, 4 };
 
        System.out.print("Before Set : ");
 
        // printing the array
        for (byte x : b) {
            System.out.print(x + " ");
        }
        byte value = 10;
 
        // set method of class Array
        Array.setByte(b, 1, value);
 
        System.out.print("\nAfter Set : ");
 
        // printing array
        for (byte x : b) {
            System.out.print(x + " ");
        }
    }
}


Java
// Java code to demonstrate
// setByte() method of Array class
 
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
 
        // Declaring and defining Byte array to null
        byte b[] = null;
 
        try {
            byte s = 10;
            Array.setByte(b, 5, s);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}


Java
// Java code to demonstrate
// setByte() method of Array class
 
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
 
        // Declaring and defining Byte array
        byte b[] = { 1, 2, 3, 4 };
 
        try {
            byte s = 10;
            Array.setByte(b, 5, s);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}


Java
// Java code to demonstrate
// setByte() method of Array class
 
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
 
        // Declaring and defining Byte variable
        byte b = 1;
 
        try {
            byte s = 10;
            Array.setByte(b, 5, s);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}


方案二:演示Java.lang.NullPointerException

Java

// Java code to demonstrate
// setByte() method of Array class
 
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
 
        // Declaring and defining Byte array to null
        byte b[] = null;
 
        try {
            byte s = 10;
            Array.setByte(b, 5, s);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
输出:
Exception : java.lang.NullPointerException

程序3:演示Java.lang.ArrayIndexOutOfBoundsException

Java

// Java code to demonstrate
// setByte() method of Array class
 
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
 
        // Declaring and defining Byte array
        byte b[] = { 1, 2, 3, 4 };
 
        try {
            byte s = 10;
            Array.setByte(b, 5, s);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
输出:
Exception : java.lang.ArrayIndexOutOfBoundsException

程序4:演示Java.lang.IllegalArgumentException

Java

// Java code to demonstrate
// setByte() method of Array class
 
import java.lang.reflect.Array;
public class GfG {
    // main method
    public static void main(String[] args)
    {
 
        // Declaring and defining Byte variable
        byte b = 1;
 
        try {
            byte s = 10;
            Array.setByte(b, 5, s);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
输出:
Exception : java.lang.IllegalArgumentException: Argument is not an array