java.lang.reflect.Array.getBoolean() Method Example



Description

The java.lang.reflect.Array.getBoolean(Object array, int index) method returns the value of the indexed component in the specified array object, as a boolean.

Declaration

Following is the declaration for java.lang.reflect.Array.getBoolean(Object array, int index) method.

public static boolean getBoolean(Object array, int index)
   throws IllegalArgumentException, ArrayIndexOutOfBoundsException

Parameters

  • array − the array.

  • index − the index.

Return Value

the (possibly wrapped) value of the indexed component in the specified array.

Exceptions

  • NullPointerException − If the specified object is null.

  • IllegalArgumentException − If the specified object is not an array.

  • ArrayIndexOutOfBoundsException − If the specified index argument is negative, or if it is greater than or equal to the length of the specified array.

Example

The following example shows the usage of java.lang.reflect.Array.getBoolean(Object array, int index) method.

package com.tutorialspoint;

import java.lang.reflect.Array;

public class ArrayDemo {
   public static void main(String[] args) {

      boolean[] array = (boolean[]) Array.newInstance(boolean.class, 3);

      Array.set(array, 0, false);
      Array.set(array, 1, true);
      Array.set(array, 2, true);

      System.out.println("array[0] = " + Array.getBoolean(array, 0));
      System.out.println("array[1] = " + Array.getBoolean(array, 1));
      System.out.println("array[2] = " + Array.getBoolean(array, 2));
   }
}

Let us compile and run the above program, this will produce the following result −

array[0] = false
array[1] = true
array[2] = true
java_reflect_array.htm
Advertisements