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

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

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