Java 数组getShort()方法

Java 数组getShort()方法

java.lang.reflect.Array.getShort() 是Java中Array类的一个内置方法,用于从指定的Array中返回给定索引处的元素,作为一个短语。

语法:

Array.getShort(Object []array,int index)

参数

  • array: 要返回其索引的对象数组。
  • index: 给定数组的特定索引。在给定数组中位于’index’的元素被返回。

返回类型: 该方法返回数组中的元素为short。

注意: 由于返回类型是short,所以不需要进行类型转换。

异常: 该方法会产生以下异常。

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

下面的程序说明了数组类的getShort()方法。

程序1 :

// Java code to demonstrate getShort() method of Array class
import java.lang.reflect.Array;
public class GfG  {
    // main method
    public static void main(String[] args) {
           
       // Declaring and defining a short array
       short a[] = {1,2,3,4,5};
          
       // Traversing the array
       for(int i = 0;i<5;i++){
              
           // Array.getShort() method
              
           short x = Array.getShort(a, i);
           // Printing the values
           System.out.print(x + " ");
       }
           
    }
}

输出。

1 2 3 4 5

程序2 :演示java.lang.ArrayIndexOutOfBoundsException。

// Java code to demonstrate getShort() method in Array
import java.lang.reflect.Array;
    
public class GfG {
    
    // main method
    public static void main(String[] args) {
        // Declaring and defining a short array
        short a[] = {1, 2, 3, 4, 5};
    
        try {
            // invalid index
            short x =  Array.getShort(a, 6);
            System.out.println(x);
        } catch (Exception e) {
            // throws Exception
            System.out.println("Exception : " + e);
        }
    }
}

输出。

Exception : java.lang.ArrayIndexOutOfBoundsException

程序3 :演示java.lang.NullPointerException。

// Java code to demonstrate getShort() method in Array
import java.lang.reflect.Array;
    
public class GfG {
    
    // main method
    public static void main(String[] args) {
        // Declaring and defining a short array to null
        short a[] = null;
    
        try {
            // null Object Array
            short x =  Array.getShort(a, 6);
            System.out.println(x);
        } catch (Exception e) {
            // throws Exception
            System.out.println("Exception : " + e);
        }
    }
}

输出。

Exception : java.lang.NullPointerException

程序4 :演示java.lang.IllegalArgumentException。

// Java code to demonstrate getShort() method in Array
import java.lang.reflect.Array;
    
public class GfG {
    
    // main method
    public static void main(String[] args) {
        // Declaring and defining a short variable
        short a = 10;
    
        try {
            // illegalArgument
            short x =  Array.getShort(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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程