获取类中运行时提供的类名的n维数组 [英] Getting the class of an n dimensional array of an runtime supplied class name

查看:82
本文介绍了获取类中运行时提供的类名的n维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个完全合格的类名,一个维数,我想这个类得到的类名。我相信我能做到这一点像这样

Given a fully qualified class name, and a number of dimensions, i would like to get the Class name for this class. I believe i can do this like such

public Class<?> getArrayClass(String className, int dimensions) throws ClassNotFoundException {
    Class<?> elementType = Class.forName(className);
    return Array.newInstance(elementType, new int[dimensions]).getClass();
}

然而,这需要我创建类的不需要实例。有没有办法做到这一点,而无需创建实例?

However this requires me to create an unneeded instance of the class. Is there a way to do this without creating the instance?

它不会出现的Class.forName([[[[Ljava /朗/字符串;)(或算法生成的版本)正常工作在各种博客文章我见过的所有实例。 (bugs.sun.com/bugdatabase/view_bug.do?bug_id=6434149)

It does not appear that Class.forName("[[[[Ljava/lang/String;") (or a algorithmically generated version) works correctly in all instances from various blog posts i've seen. (bugs.sun.com/bugdatabase/view_bug.do?bug_id=6434149)

推荐答案

使用 [[[Ljava.lang.String; 符号是再$ p的标准方法$ psenting数组类的名称。

Using the [[[Ljava.lang.String; notation is the standard way of representing array class names.

该XMLEn coder和XMLDe codeR使用标准的Java功能写入和读取数组:

The XMLEncoder and XMLDecoder use standard Java functionality to write and read arrays:

String[][][] foo = new String[3][4][5];
foo[0][0][0] = "a";
foo[2][3][4] = "z";
XMLEncoder encoder = new XMLEncoder(System.out);
encoder.writeObject(foo);
encoder.flush();
encoder.close();

得到以下结果:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_22" class="java.beans.XMLDecoder"> 
 <array class="[[Ljava.lang.String;" length="3"> 
  <void index="0"> 
   <array class="[Ljava.lang.String;" length="4"> 
    <void index="0"> 
     <array class="java.lang.String" length="5"> 
      <void index="0"> 
        <string>a</string> 
        ...

我目前看不出有什么问题,在使用Class.forName()与自建类名(如prepending的 [L ,因为它是也以同样的方式使用由Java标准类

I currently don't see any problem in using Class.forName() with a self-built class name (e.g. prepending the [L, since it's also used by the Java standard classes in the same way.

Class arrayClass = Class.forName("[L" + "java.lang.String" + ";");

和作为<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#forName%28java.lang.String,%20boolean,%20java.lang.ClassLoader%29\"相对=nofollow> JavaDoc中的Class.forName 介绍,类加载,但没有初始化,您也不必为它创建一个实例。

And as the JavaDoc for Class.forName describes, the class is loaded but not initialized, and you also don't need to create an instance for it.

如果名称表示
  数组类的组件类型
  Array类加载但不
  初始化。

If name denotes an array class, the component type of the array class is loaded but not initialized.

您有关各种博客文章最后一句话,这些都不会工作将是有趣的调查。

Your last remark about various blog posts where this would not work would be interesting to investigate.

这篇关于获取类中运行时提供的类名的n维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆