这个 JAVA朗,反思一下。大堆getBoolean() 将指定数组中的给定索引作为短索引返回。
null
语法 :
Array.getBoolean(Object []array,int index)
参数:
- 数组: 要返回其索引的对象数组。
- 索引: 给定数组的特定索引。返回给定数组中“index”处的元素。
返回类型: 此方法将数组的元素返回为布尔值。
注: Typecast不是必需的,因为返回类型是布尔型的。
例外情况: 此方法引发以下异常
- 空指针异常 –当数组为空时。
- 非法数据异常 –当给定的对象数组不是数组时。
- 数组下标越界异常 –如果给定索引不在数组大小的范围内。
下面的程序演示了Array类的getBoolean()方法:
方案1 :
// Java code to demonstrate getBoolean() method of Array class import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining a boolean array boolean a[] = { true , true , false }; // Traversing the array for ( int i = 0 ;i< 3 ;i++){ // Array.getBoolean() method boolean x = Array.getBoolean(a, i); // Printing the values System.out.print(x + " " ); } } } |
输出:
true true false
方案2 :
// Java code to demonstrate getBoolean() method in Array import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining a boolean array boolean a[] = { true , true , false }; try { // invalid index boolean x = Array.getBoolean(a, 6 ); System.out.println(x); } catch (Exception e) { // throws Exception System.out.println( "Exception : " + e); } } } |
输出:
Exception : java.lang.ArrayIndexOutOfBoundsException
方案3 :
// Java code to demonstrate getBoolean() method in Array import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining a boolean array to null boolean a[] = null ; try { // null Object array boolean x = Array.getBoolean(a, 6 ); System.out.println(x); } catch (Exception e) { // throws Exception System.out.println( "Exception : " + e); } } } |
输出:
Exception : java.lang.NullPointerException
方案4 :
// Java code to demonstrate getBoolean() method in Array import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining a boolean variable boolean a = true ; try { // illegalArgument boolean x = Array.getBoolean(a, 6 ); System.out.println(x); } catch (Exception e) { // throws Exception System.out.println( "Exception : " + e); } } } |
输出:
Exception : java.lang.IllegalArgumentException: Argument is not an array
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END