Java 数组set()方法

Java 数组set()方法

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

语法

Array.set(Object []array, int index, Object value)
Java

参数:

  • array : 这是一个要被更新的对象类型的数组。
  • index : 这是要被更新的数组的索引。
  • value : 这是要在给定 数组 的给定 索引 上设置的值。

返回类型: 这是一个无效类型的方法,不返回任何值。更新反映在作为参数传递的对象数组上。

异常: 该方法会抛出以下异常。

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

下面的程序说明了Array.set()方法。

程序 1 :

// Java code to demonstrate set() 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", "is", "Geeks" };
  
        System.out.print("Before Set : ");
        // printing the array
        for (String x : s) {
            System.out.print(x);
        }
  
        // set method of class Array
        Array.set(s, 1, "for");
  
        System.out.print("\nAfter Set : ");
        // printing array
        for (String x : s) {
            System.out.print(x);
        }
    }
}
Java

输出:

Before Set : GeeksisGeeks
After Set : GeeksforGeeks
Java

程序2: 演示java.lang.NullPointerException

// Java code to demonstrate set() 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[] = null;
  
        try {
            // set method of class Array
            Array.set(s, 1, "for");
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
Java

输出。

Exception : java.lang.NullPointerException
Java

程序3: 演示java.lang.ArrayIndexOutOfBoundsException

// Java code to demonstrate set() 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 {
            // set method of class Array
            Array.set(s, 4, "for");
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
Java

输出。

Exception : java.lang.ArrayIndexOutOfBoundsException
Java

程序4: 演示java.lang.IllegalArgumentException

// Java code to demonstrate set() 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 = "GeeksforGeeks";
  
        try {
            // set method of class Array
            Array.set(s, 4, "for");
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}
Java

输出。

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册