/*
Output:
Position: 0, value: 1040988893
Position: 1, value: -2073064706
Position: 2, value: -808861084
* */
import java.lang.reflect.Array;
import java.util.Random;
public class MainClass {
public static void main (String args[]) {
Object array = Array.newInstance(int.class, 3);
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);
}
for (int i=0; i<length; i++) {
int value = Array.getInt(array, i);
System.out.println("Position: " + i + ", value: " + value);
}
}
}
|