`

实例185 - 自定义泛型化数组类

 
阅读更多

心法领悟185:Java泛型的局限。

Java泛型使用起来有很多的局限性,如不能使用基本类型作为其类型参数、不能抛出或捕获泛型类型的实例、不能直接使用泛型数组、不能实例化类型变量等。希望读者在使用泛型时多加注意。对于其中的某些不足,可以使用Java的反射机制进行弥补。虽然反射的效率不高,但是也只能忍受了。

 

package com.mingrisoft.generic;

import java.lang.reflect.Array;

public class GenericArray<T> {
    
    private T[] array;
    private int size;
    
    @SuppressWarnings("unchecked")
    public GenericArray(Class<T> type, int size) {
        this.size = size;
        array = (T[]) Array.newInstance(type, size);
    }
    
    public void put(int index, T item) {
        if (size > index) {
            array[index] = item;
        }
    }
    
    public T get(int index) {
        
        if (size > index) {
            return array[index];
        } else {
            return null;
        }
    }
}

 

package com.mingrisoft.generic;

public class GenericArrayTest {
    public static void main(String[] args) {
        System.out.println("创建String类型的数组并向其添加元素:明日科技");
        GenericArray<String> stringArray = new GenericArray<String>(String.class, 10);
        stringArray.put(0, "明日科技");
        System.out.println("String类型的数组元素:" + stringArray.get(0));
        System.out.println("创建Integer类型的数组并向其添加元素:123456789");
        GenericArray<Integer> integerArray = new GenericArray<Integer>(Integer.class, 10);
        integerArray.put(0, 123456789);
        System.out.println("Integer类型的数组元素:" + integerArray.get(0));
    }
}

 运行结果:

创建String类型的数组并向其添加元素:明日科技
String类型的数组元素:明日科技
创建Integer类型的数组并向其添加元素:123456789
Integer类型的数组元素:123456789

 补充阅读:

 java.lang.Class<T>

 

 

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. 

 

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader. 

 

The following example uses a Class object to print the class name of an object: 

 

 

     void printClassName(Object obj) {

         System.out.println("The class of " + obj +

                            " is " + obj.getClass().getName());

     }

 

It is also possible to get the Class object for a named type (or for void) using a class literal. See Section 15.8.2 of The Java™ Language Specification. For example: 

 

 

System.out.println("The name of class Foo is: "+Foo.class.getName()); 

Parameters:

<T> the type of the class modeled by this Class object. For example, the type of String.class is Class<String>. Use Class<?> if the class being modeled is unknown.

Since:

JDK1.0

Author:

unascribed

See Also:

java.lang.ClassLoader.defineClass(byte[], int, int)

分享到:
评论

相关推荐

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    实例163 自定义最大化、最小化和关闭按钮 213 实例164 手动改变自制窗体的大小 215 实例165 禁止改变窗体的大小 218 7.3 设置窗体的标题栏 218 实例166 使窗体标题栏文字右对齐 218 实例167 没有标题栏也可以更改...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例185 自定义泛型化数组类 235 实例186 泛型方法与数据查询 236 实例187 泛型化方法与最小值 238 实例188 泛型化接口与最大值 239 实例189 使用通配符增强泛型 240 实例190 泛型化的折半查找法 241 第9章 编程常用...

    java范例开发大全

    实例130 对象的序列化与反序列化 185 实例131 同时显示多个文件 187 实例132 生成zip压缩文件 189 实例133 解压缩zip文件 192 实例134 生成Excel文件 194 实例135 读取Excel文件中的内容 198 实例136 生成PDF文件 ...

    明日科技C#开发入门及项目实战

    实例122 使用泛型去掉数组中的重复数字 实例123 通过重写虚方法实现加法运算 实例124 使用迭代器实现倒序遍历 实例125 通过泛型查找数组中的元素 第12章 ado.net数据访问技术 实例126 连接加密的access数据库 实例...

    《C#经典编程220例》.(明日科技).【带书签】-共3部分

    实例122 使用泛型去掉数组中的重复数字 204 实例123 通过重写虚方法实现加法运算 205 实例124 使用迭代器实现倒序遍历 207 实例125 通过泛型查找数组中的元素 209 第12章 ado.net数据访问技术 212 实例126 连接加密...

    Java范例开发大全(全书源程序)

    实例130 对象的序列化与反序列化 185 实例131 同时显示多个文件 187 实例132 生成zip压缩文件 189 实例133 解压缩zip文件 192 实例134 生成Excel文件 194 实例135 读取Excel文件中的内容 198 实例136 生成PDF...

    Java范例开发大全 (源程序)

     实例130 对象的序列化与反序列化 185  实例131 同时显示多个文件 187  实例132 生成zip压缩文件 189  实例133 解压缩zip文件 192  实例134 生成Excel文件 194  实例135 读取Excel文件中的内容 198  ...

    java范例开发大全(pdf&源码)

    实例130 对象的序列化与反序列化 185 实例131 同时显示多个文件 187 实例132 生成zip压缩文件 189 实例133 解压缩zip文件 192 实例134 生成Excel文件 194 实例135 读取Excel文件中的内容 198 实例136 生成PDF文件 ...

    java范例开发大全源代码

     实例130 对象的序列化与反序列化 185  实例131 同时显示多个文件 187  实例132 生成zip压缩文件 189  实例133 解压缩zip文件 192  实例134 生成Excel文件 194  实例135 读取Excel文件中的内容 198 ...

    JAVA 范例大全 光盘 资源

    实例75 自定义异常类 187 实例76 使用finally避免资源漏洞 189 实例77 反射机制 191 第10章 I/O及文件操作 196 实例78 创建文件和目录 196 实例79 查找文件 199 实例80 删除文件夹和文件 201 实例81 文件复制...

Global site tag (gtag.js) - Google Analytics