编写通用方法来复制数组 [英] Write a generic method to copy an array

查看:76
本文介绍了编写通用方法来复制数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的编程任务中,我被要求编写一种通用的复制方法,以从一个数组复制到相同大小和类型的数组.在Java中甚至有可能吗?我尝试的所有操作最终都出现了一些通用数组创建"错误.我迷路了,不知道如何解决这个问题!

For my programming assignment, I was asked to write a generic copy method to copy from one array to an identically sized and typed array. Is this even possible in Java? Everything I tried ended up with some "generic array creation" error. I am lost and don't know how to solve this problem!

public class copyArray<AnyType>{

   public copyArray(AnyType[] original){ 

     AnyType[] newarray = new AnyType[original.length];  

     for(int i =0; i<original.length; i++){ 
        newarray[i] = original[i]; } 
}

推荐答案

您可以使用反射的概念来编写可在运行时确定类型的通用复制方法.简而言之,反射就是在运行时检查类,接口,字段和方法的能力,而无需在编译时知道类,方法等的名称.

You can use the concept of reflection to write a generic copy method that can determine type at runtime. In a nutshell, reflection is the ability to inspect classes, interfaces, fields and methods at runtime without knowing the names of classes, methods etc at compile time.

java.lang.Reflect java.lang.Class 包含Java Reflection API.此方法同时使用这些类及其某些方法来创建通用的arrayCopy方法,该方法将为我们找出类型.

java.lang.Reflect together with java.lang.Class comprise the Java Reflection API. This method uses both of those classes and some of their methods to make a generic arrayCopy method that will find out the type for us.

更多信息:什么是反射,为什么反射有用?/a>

More info: What is reflection and why is it useful?

  • Class<?>使用通配符运算符?,该运算符基本上说我们可以拥有一个未知类型的Class对象-类Class的通用版本.
  • <T>是代表

  • Class<?> is using a wildcard operator ? which basically says that we can have a Class object of unknown type - a generic version of class Class.
  • <T> is a generic operator that stands for raw type
  • ArrayThe Array class provides static methods to dynamically create and access Java arrays. i.e. This class contains methods that allow you to set and query the values of array elements, determine the length of the array, and create new instances of arrays. We are going to use Array.newInstance()
  • getClass ()-返回一个数组,其中包含Class对象,这些Class对象代表所有公共类和接口,这些公共类和接口都是所表示的类对象的成员.
  • getComponentType()-返回表示数组的组件类型(什么类型,即int等)的类.
  • newInstance()-获取数组的新实例.
  • getClass () - returns an array containing Class objects representing all public classes and interfaces that are members of the represented class object.
  • getComponentType() - returns the class representing the component type (what type i.e. int, , etc) of the array.
  • newInstance() - Gets a new instance of an array.
  • private <T> T[] arrayCopy(T[] original) {
    
        //get the class type of the original array we passed in and determine the type, store in arrayType
        Class<?> arrayType = original.getClass().getComponentType();
    
        //declare array, cast to (T[]) that was determined using reflection, use java.lang.reflect to create a new instance of an Array(of arrayType variable, and the same length as the original
        T[] copy = (T[])java.lang.reflect.Array.newInstance(arrayType, original.length);
    
        //Use System and arraycopy to copy the array
        System.arraycopy(original, 0, copy, 0, original.length);
        return copy;
    }
    

    这篇关于编写通用方法来复制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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