Use Array.setInt to fill an array in Java

Description

The following code shows how to use Array.setInt to fill an array.

Example


//from  ww  w  .  j  a v  a2  s .c  om
import static java.lang.System.err;

import java.lang.reflect.Array;

public class Main {
  public static void main(String... args) {
    Integer[] ary = new Integer[2];
    try {
      Array.setInt(ary, 0, 1); // IllegalArgumentException

      // production code should handle these exceptions more gracefully
    } catch (IllegalArgumentException x) {
      err.format("Unable to box%n");
    } catch (ArrayIndexOutOfBoundsException x) {
      x.printStackTrace();
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy