关于原始数组的Java Generics方法代码重复问题(Java Generics method code duplication issue regarding primitive arrays)

编程入门 行业动态 更新时间:2024-10-28 15:19:03
关于原始数组的Java Generics方法代码重复问题(Java Generics method code duplication issue regarding primitive arrays)

所以我正在为一个小型的个人实用程序库开发Grid类的实现。 经过大量工作,我正在整理Grid类并添加一些功能。 我希望与Grid类一起使用的关键功能之一是能够将任何给定类型的单个2D数组作为构造函数的参数。

这工作正常,直到我意识到我无法编译任何试图将任何基元数组传递给构造函数的代码。 由于在原始数组上不会发生自动装箱,因此以代码重用的形式给我提出了设计问题。 无论传入的数组类型如何,初始化Grid的方法都是相同的,即使它们是基元,但似乎我需要为所有不同类型的基元编写单独的构造函数。 假设我只使用我的基本构造函数(并且我计划让构造函数具有不同的参数,例如网格包装选项),这让我留下了9个不同的构造函数。

我是否正确地假设没有办法避免所有这些代码重复?

So I'm working on an implementation of a Grid class for a small, personal utilities library. After much work, I'm tidying up the Grid class and adding some functionality. One of the key pieces of functionality that I wish to use with my Grid class is to be able to take a single, 2D array of any given type as an argument to the constructor.

This worked fine until I realized that I can't compile any code that attempts to pass in any array of primitives to the constructor. Since autoboxing doesn't happen on primitive arrays, this presents a design problem to me in the form of code reuse. The method for initializing the Grid is the same regardless of the type of array passed in, even if they are primitives, but it appears that I need to write a separate constructor for all the different types of primitives. Which leaves me with 9 different constructors assuming I just use my base constructor (and I had planned to have constructors with different arguments for things such as grid-wrapping options).

Am I right in assuming that there is no way to avoid all this code duplication?

最满意答案

您可以使用Array类来避免(大部分)重复。 但它并不漂亮:你的构造函数参数应该是Object类型,你必须只信任你的调用者传递实际的数组,而不是Socket或Properties 。

例如,您可以像这样做自己的拳击:

<T> public T[][] boxArray(Class<T> clazz, Object array) { int height = Array.getLength(array); int width = height == 0 ? 0 : Array.getLength(Array.get(array, 0)); T[][] result = (T[][]) Array.newInstance(clazz, height, width); for(int i = 0; i < height; i ++) { Object a = Array.get(array, i); for(int j = 0; j < width; j++) { result[i][j] = (T) Array.get(a, j); } } return result; }

You can avoid (most of) the duplication by using Array class. But it's not pretty: your constructor parameter would have do be of type Object, and you'd have to just trust your caller to pass the actual array there, and not a Socket or a Properties.

For example, you could do your own boxing like this:

<T> public T[][] boxArray(Class<T> clazz, Object array) { int height = Array.getLength(array); int width = height == 0 ? 0 : Array.getLength(Array.get(array, 0)); T[][] result = (T[][]) Array.newInstance(clazz, height, width); for(int i = 0; i < height; i ++) { Object a = Array.get(array, i); for(int j = 0; j < width; j++) { result[i][j] = (T) Array.get(a, j); } } return result; }

更多推荐

本文发布于:2023-08-06 13:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   原始   代码   方法   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!