Use reflection to create, fill, and display an array in Java

Description

The following code shows how to use reflection to create, fill, and display an array.

Example


//from   w  w w  . java 2s  . com
import java.lang.reflect.Array;
import java.util.Random;

public class Main {
  public static void main(String args[]) {
    Object array = Array.newInstance(int.class, 3);
    printType(array);
    fillArray(array);
    displayArray(array);
  }

  private static void printType(Object object) {
    Class type = object.getClass();
    if (type.isArray()) {
      Class elementType = type.getComponentType();
      System.out.println("Array of: " + elementType);
      System.out.println("Array size: " + Array.getLength(object));
    }
  }

  private static void fillArray(Object array) {
    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
      int random = generator.nextInt();
      Array.setInt(array, i, random);
    }
  }

  private static void displayArray(Object array) {
    int length = Array.getLength(array);
    for (int i = 0; i < length; i++) {
      int value = Array.getInt(array, i);
      System.out.println("Position: " + i + ", value: " + value);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy