Close

Java Reflection - Array.getInt() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Array

java.lang.Objectjava.lang.Objectjava.lang.reflect.Arrayjava.lang.reflect.ArrayLogicBig

Method:

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

Returns the value of the indexed component in the specified array object, as an int .


Examples


package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetIntExample {

public static void main(String... args) {
int[] arr = {1, 4, 9};
int i = Array.getInt(arr, 1);
System.out.println(i);
}
}

Output

4




package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetIntExample2 {

public static void main(String... args) {
Integer[] arr = {1, 4, 9};
int i = Array.getInt(arr, 1);
System.out.println(i);
}
}

Output

Caused by: java.lang.IllegalArgumentException: Argument is not an array of primitive type
at java.base/java.lang.reflect.Array.getInt(Native Method)
at com.logicbig.example.array.GetIntExample2.main(GetIntExample2.java:14)
... 6 more




See Also