Friday, December 27, 2024
Google search engine
HomeLanguagesJavaArray get() Method in Java

Array get() Method in Java

The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array.

Syntax

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

Parameters : This method accepts two mandatory parameters:

  • array: The object array whose index is to be returned.
  • index: The particular index of the given array. The element at ‘index’ in the given array is returned.

Return Value: This method returns the element of the array as type of Object class.

Exceptions: This method throws following exceptions:

  • NullPointerException – when the array is null.
  • IllegalArgumentException – when the given object array is not an Array.
  • ArrayIndexOutOfBoundsException – if the given index is not in the range of the size of the array.

Below programs illustrate the get() method of Array class:

Program 1:




import java.lang.reflect.Array;
  
public class GfG {
    // main method
    public static void main(String[] args)
    {
        // Declaring and defining an int array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Traversing the array
        for (int i = 0; i < 5; i++) {
  
            // Array.get method
            // Note : typecasting is essential
            // as the return type in Object.
            int x = (int)Array.get(a, i);
  
            // Printing the values
            System.out.print(x + " ");
        }
    }
}


Output:

1 2 3 4 5

Program 2: To demonstrate ArrayIndexOutOfBoundsException.




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


Output:

Exception : java.lang.ArrayIndexOutOfBoundsException

Program 3: To demonstrate NullPointerException.




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


Output:

Exception : java.lang.NullPointerException

Program 4: To demonstrate IllegalArgumentException.




import java.lang.reflect.Array;
  
public class GfG {
    // main method
    public static void main(String[] args)
    {
        // int (Not an array)
        int y = 0;
  
        try {
            // illegalArgument
            int x = (int)Array.get(y, 6);
  
            System.out.println(x);
        }
        catch (Exception e) {
            // Throws exception
            System.out.println("Exception : " + e);
        }
    }
}


Output:

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

Most Popular

Recent Comments