惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
博客园 - Franky
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
爱范儿
爱范儿
罗磊的独立博客
博客园_首页
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
V
Visual Studio Blog
T
Tailwind CSS Blog

博客园 - daniel-shen

精通EJB-概述 硬件基础知识大全 教程:关于XMLBeans的初步 在客户端进行xslt对xml的xuan渲染 jaxp笔记2007-4-17 6.ZK用户接口标记语言 5.事件的监听和处理 - daniel-shen - 博客园 4.组件的生命周期 3.zk体系结构 "tomcat启动分析"文章读后笔记 jdk5.0 范型说明 HTML小技巧 MessageFormat初步 ResourceBundle 与locale的使用 jni中关于dll的装载问题 java线程-消费者vs生产者 HttpSessionBindingListener实现与应用 用Java Servlets 2.4实现过滤 加和不加java:comp/env/前缀有什么区别?
java 数组反射例子
daniel-shen · 2006-12-07 · via 博客园 - daniel-shen

清单 2-3. 使用反射检查数组类型和长度
public class ArrayReflection {
  public static void main (String args[]) {
    printType(args);
  }
  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(" Length: " + Array.getLength(object));
    }
  }
}

运行时创建数组:
int array[] = (int[])Array.newInstance(int.class, 5);

int dimensions[] = {5};
int array[] = (int[])Array.newInstance(int.class, dimensions);

int dimensions[] = {3, 4};
int array[][] = (int[][])Array.newInstance(int.class, dimensions);

int dimensions[] = {5,4,3,2,1};

int array[][][][][] = (int[][][][][])Array.newInstance(int.class, dimensions);

清单 2-4. 使用反射创建、填充和显示数组


            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);
            }
            }
            }
            

运行时,输出将如下所示(尽管随机数会不同):


            Array of: int
            Array size: 3
            Position: 0, value: -54541791
            Position: 1, value: -972349058
            Position: 2, value: 1224789416