Results 1 to 1 of 1
-
Using reflection to create, fill, and display an array
Java Code:import java.lang.reflect.Array; import java.util.Random; public class ArrayCreate { 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 sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Array Fill Test
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:45 PM -
Using reflection to check array type and length
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:42 PM -
How do I create an Array of Images
By wco5002 in forum New To JavaReplies: 3Last Post: 03-21-2008, 03:45 PM -
Getting methods of a Class - Reflection
By Java Tip in forum Java TipReplies: 0Last Post: 11-15-2007, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks