Array reflection

In this chapter you will learn:

  1. How to get the length of an Array
  2. How to get value from an Array
  3. How to set value to an Array
  4. How to create new Array instance

Get the length of an Array

The java.lang.reflect.Array class provides static methods to dynamically create and access Java arrays.

static int getLength(Object array) returns the length of the specified array object, as an int.

import java.lang.reflect.Array;
import java.util.Arrays;
/*from j  av a2s  .  c  o  m*/
public class Main{
  public static void main(String[] args) {
    int[] sourceInts = { 12, 78 };
    int[] destInts = new int[2];
  
    for (int i = 0; i < Array.getLength(sourceInts); i++) {
      Array.set(destInts, i, Array.get(sourceInts, i));
      System.out.println(Array.get(destInts, i));
    }
    System.out.println(Arrays.toString(destInts));
  } 
}

The output:

Get value from an Array

  • static Object get(Object array, int index)
    returns the value of the indexed component in the specified array object.
  • static boolean getBoolean(Object array, int index)
    returns array element as a boolean.
  • static byte getByte(Object array, int index)
    returns array element as a byte.
  • static char getChar(Object array, int index)
    returns array element as a char.
  • static double getDouble(Object array, int index)
    returns array element as a double.
  • static float getFloat(Object array, int index)
    returns array element as a float.
  • static int getInt(Object array, int index)
    returns array element as an int.
  • static long getLong(Object array, int index)
    returns array element as a long.
  • static short getShort(Object array, int index)
    returns array element as a short.
import java.lang.reflect.Array;
/* j a v a  2s.  c  o m*/
public class Main {
  public static void main(String[] argv) throws Exception {
    int[] array = { 1, 2, 3 };
    // Get the value of the third element.
    Object o = Array.get(array, 2);
    System.out.println("o:"+o);
  }
}

The output:

Set value to an Array

  • static void set(Object array, int index, Object value)
    sets array element to the new value.
  • static void setBoolean(Object array, int index, boolean z)
    sets array element to the boolean value.
  • static void setByte(Object array, int index, byte b)
    sets array element to the byte value.
  • static void setChar(Object array, int index, char c)
    sets array element to the char value.
  • static void setDouble(Object array, int index, double d)
    sets array element to the double value.
  • static void setFloat(Object array, int index, float f)
    sets array element to the float value.
  • static void setInt(Object array, int index, int i)
    sets array element to the int value.
  • static void setLong(Object array, int index, long l)
    sets array element to the long value.
  • static void setShort(Object array, int index, short s)
    sets array element to the short value.
import java.lang.reflect.Array;
import java.util.Arrays;
/*from  j  a v a  2 s  .c om*/
public class Main {
  public static void main(String[] argv) throws Exception {
    int[] array = { 1, 2, 3 };
    // Get the value of the third element.
    Object o = Array.get(array, 2);
    System.out.println("o:"+o);
    // Set the value of the third element.
    Array.set(array, 2, 1);
    System.out.println(Arrays.toString(array));
  }
}

The output:

Create new Array instance

  • static Object newInstance(Class<?> componentType, int... dimensions) creates a new array with the component type and dimensions.
  • static Object newInstance(Class<?> componentType, int length) creates a new array with the component type and length.
import java.lang.reflect.Array;
/*  ja v a 2 s . c om*/
public class Main {
  public static void main(String[] argv) throws Exception {
    int[] ints = (int[]) Array.newInstance(int.class, 10);
    
    String[] stringArray = (String[]) Array.newInstance(String.class, 10);
  }
}

The following code creates multidimensional array.

import java.lang.reflect.Array;
//j  a  va2s  .com
public class Main {
  public static void main(String[] argv) throws Exception {
    int[][] ints = (int[][]) Array.newInstance(int.class, 10,5);
    
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to get the annotation type
Home » Java Tutorial » Reflection
Reflection
Class Reflection
Class modifier, package and string presentation
Constructor reflection
Field Reflection
Get/Set field value
Java method reflection
Modifier
Package
Array reflection
Get annotation type
Method annotation