当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Array setLong()用法及代码示例


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

句法

Array.setLong(Object []array, int index, long value)

参数:


  • array : 这是要更新的Object类型的数组。
  • index : 这是要更新的数组的索引。
  • value : 这是要在给定数组的给定索引处设置的long值。

返回类型:这是一个void(空)类型的方法,不会返回任何值。此更新生效到作为参数传递的Object数组上。

异常:此方法引发以下异常。

  • NullPointerException –当数组为null时。
  • IllegalArgumentException–当给定的对象数组不是数组时。
  • ArrayIndexOutOfBoundsException–如果给定的索引不在数组的大小范围内。
  • 以下示例程序旨在说明Array.setLong()方法:

    示例1:

    // Java code to demonstrate setLong() method of Array class 
    import java.lang.reflect.Array; 
    public class GfG { 
        // main method 
        public static void main(String[] args) 
        { 
      
            // Declaring and defining long array 
            long b[] = { 1, 2, 3, 4 }; 
            System.out.print("Befor Set : "); 
            // printing the array 
            for (long x : b) { 
                System.out.print(x + " "); 
            } 
            long value = 10; 
            // setLong method of class Array 
            Array.setLong(b, 1, value); 
      
            System.out.print("\nAfter Set : "); 
            // printing array 
            for (Long x : b) { 
                System.out.print(x + " "); 
            } 
        } 
    }

    输出

Befor Set : 1 2 3 4 
After Set : 1 10 3 4 

示例2:演示java.lang.NullPointerException

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

输出

Exception : java.lang.NullPointerException

示例3:演示java.lang.ArrayIndexOutOfBoundsException

// Java code to demonstrate setLong() method of Array class 
import java.lang.reflect.Array; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // Declaring and defining long array 
        long b[] = { 1, 2, 3, 4 }; 
        try { 
            Array.setLong(b, 5, 10); 
        } 
        catch (Exception e) { 
            System.out.println("Exception : " + e); 
        } 
    } 
}

输出

Exception : java.lang.ArrayIndexOutOfBoundsException

示例4:演示java.lang.IllegalArgumentException。

// Java code to demonstrate setLong() method of Array class 
  
import java.lang.reflect.Array; 
public class GfG { 
    // main method 
    public static void main(String[] args) 
    { 
  
        // Declaring and defining long variable 
        long b = 1; 
        try { 
            Array.setLong(b, 5, 10); 
        } 
        catch (Exception e) { 
            System.out.println("Exception : " + e); 
        } 
    } 
}

输出

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


相关用法


注:本文由纯净天空筛选整理自ShivamKD大神的英文原创作品 Array setLong() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。