當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Array setBoolean()用法及代碼示例


java.lang.reflect.Array.setBoolean()方法是一種內置方法,用於將指定的布爾值設置為給定對象數組的指定索引。

用法:


Array.setBoolean(Object []array, int index, boolean value)

參數:此方法采用三個參數:

  • array:要更新的Object類型的數組。
  • index:要更新的數組的索引。
  • value:給定的布爾值指數給定的數組

返回類型:這是一個void(空)類型的方法,不會返回任何值。此更新生效到作為參數傳遞的Object數組上。

異常:此方法引發以下異常:

  • NullPointerException –當數組為null時。
  • IllegalArgumentException–當給定的對象數組不是數組時。
  • ArrayIndexOutOfBoundsException–如果給定的索引不在數組的大小範圍內。
  • 下麵是Array.setBoolean()方法的實現:

    示例1:

    // Java code to demonstrate setBoolean() 
    // method of Array class 
      
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) 
        { 
            // Declaring and defining boolean array 
            boolean b[] = { true, false, true }; 
      
            // array before using setBoolean() 
            System.out.print("Before Set : "); 
      
            // printing the array 
            for (boolean x : b) { 
                System.out.print(x + " "); 
            } 
      
            // boolean value to be set 
            boolean value = true; 
      
            // setBoolean method of class Array 
            Array.setBoolean(b, 1, value); 
      
            // array after using setBoolean() 
            System.out.print("\nAfter Set : "); 
      
            // printing array 
            for (boolean x : b) { 
                System.out.print(x + " "); 
            } 
        } 
    }
    輸出:
    Before Set : true false true 
    After Set : true true true
    

    示例2:演示java.lang.NullPointerException

    // Java code to demonstrate setBoolean() 
    // method of Array class 
      
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) 
        { 
            // Declaring and defining boolean array to null 
            boolean b[] = null; 
      
            try { 
                // boolean value to be set 
                boolean c = false; 
      
                // passing a null array as parameter 
                Array.setBoolean(b, 5, c); 
            } 
            catch (Exception e) { 
                System.out.println("Exception : " + e); 
            } 
        } 
    }
    輸出:
    Exception : java.lang.NullPointerException
    

    示例3:演示java.lang.ArrayIndexOutOfBoundsException

    // Java code to demonstrate setBoolean() 
    // method of Array class 
      
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) 
        { 
            // Declaring and defining boolean array 
            boolean b[] = { true, false, true }; 
      
            try { 
                // value to be set 
                boolean c = false; 
      
                // passing index as 5 when size is 3 
                Array.setBoolean(b, 5, c); 
            } 
            catch (Exception e) { 
                System.out.println("Exception : " + e); 
            } 
        } 
    }
    輸出:
    Exception : java.lang.ArrayIndexOutOfBoundsException
    

    示例4:演示java.lang.IllegalArgumentException

    // Java code to demonstrate setBoolean() 
    // method of Array class 
      
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) 
        { 
            // Declaring and defining boolean variable 
            boolean b = true; 
      
            try { 
                // value to be set 
                boolean c = false; 
      
                // passing variable in the place of an array 
                Array.setBoolean(b, 5, c); 
            } 
            catch (Exception e) { 
                System.out.println("Exception : " + e); 
            } 
        } 
    }
    輸出:
    Exception : java.lang.IllegalArgumentException: Argument is not an array
    


相關用法


注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 Array setBoolean() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。