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

推荐订阅源

Y
Y Combinator Blog
有赞技术团队
有赞技术团队
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
The Cloudflare Blog
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
IT之家
IT之家
MyScale Blog
MyScale Blog
博客园_首页
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
罗磊的独立博客

博客园 - 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