Java 数组setChar()方法

Java 数组setChar()方法

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

语法

Array.setChar(Object []array, int index, char value)

参数: 该方法需要三个参数。

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

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

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

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

下面是Array.setChar()方法的实现。

程序1:

// Java code to demonstrate setChar()
// method of Array class
  
import java.lang.reflect.Array;
  
public class GfG {
  
    // main method
    public static void main(String[] args)
    {
        // Declaring and defining char array
        char ch[] = { 'a', 'b', 'c', 'd', 'e' };
  
        System.out.print("Before Set : ");
  
        // printing the array
        for (char x : ch) {
            System.out.print(x + " ");
        }
  
        char value = 'g';
  
        // setChar method of class Array
        Array.setChar(ch, 1, value);
  
        System.out.print("\nAfter Set : ");
  
        // printing array
        for (char x : ch) {
            System.out.print(x + " ");
        }
    }
}

输出:

Before Set : a b c d e 
After Set : a g c d e

程序2: 演示java.lang.NullPointerException

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

输出。

Exception : java.lang.NullPointerException

程序3: 演示java.lang.ArrayIndexOutOfBoundsException

// Java code to demonstrate
// setChar() method of Array class
  
import java.lang.reflect.Array;
public class GfG {
  
    // main method
    public static void main(String[] args)
    {
        // Declaring and defining char array
        char b[] = { 'a', 'b', 'c', 'd' };
  
        try {
            char c = 'g';
            // Passing index 5 as parameter
            // when the size is 4 instead
            Array.setChar(b, 5, c);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

输出。

Exception : java.lang.ArrayIndexOutOfBoundsException

程序4: 演示java.lang.IllegalArgumentException

// Java code to demonstrate setChar()
// method of Array class
  
import java.lang.reflect.Array;
public class GfG {
  
    // main method
    public static void main(String[] args)
    {
        // Declaring and defining char variable
        char b = 'b';
  
        try {
            char c = 'g';
  
            // Passing a variable as parameter
            // when an array is expected instead
            Array.setChar(b, 5, c);
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

输出。

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程