大数组“int”类型需要传递给通用数组 &收藏品

发布于 2024-10-22 18:48:11 字数 387 浏览 4 评论 0原文

我正在从函数生成一个包含 int 类型元素的大型数组(大小>1000)。我需要将此数组传递给泛型类型数组,但由于泛型类型数组不接受原始类型数组,因此我无法这样做。

我害怕使用 Integer 类型数组,因为在创建、性能、使用空间(12 字节对象的数组)方面这样做的成本很高。大尺寸数组。当我需要对数组元素执行一些加法操作时,它会创建不可变的 Integer

最好的搭配是什么?

编辑只是为了消除一些混乱,我需要将 int[] 传递给签名类型的方法:void setKeys(K...keys)

I am generating a large arrays(size>1000) with elements of int type, from a function. I need to pass this array to a generic type array but since the generic type array doesnt accept arrays of primitive type, I am unable to do so.

I fear to use the Integer type array since it will be costly, in terms of creation, performance, space used(an array of 12 byte objects) when doing so for a large size arrays. More it will create immutable Integer s when I need to perform some addition operations on the array elements.

What would be the best way to go with ?

EDIT Just to remove some confusions around, I need to pass int[] to a method of signature type: void setKeys(K... keys).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丢了幸福的猪 2024-10-29 18:48:11

我想将 int[] 传递给此函数:public Query; setKeys(K...键);

我假设您的意思是 int[] 应该是一组键...而不仅仅是一个键。

那是不可能的。泛型类型的类型参数必须是引用类型。您的用例要求 Kint

您有两种选择:

  • 使用 Integer(或可变的 int 持有者类)并付出性能损失,或者
  • 放弃使用泛型并更改该方法的签名。

顺便说一句,Integer 类为较小的 int 值保留了 Integer 对象的缓存。如果您使用 Integer.valueOf(int) 创建对象,那么您很有可能会获得对预先存在的对象的引用。 (当然,这只有效,因为 Integer 对象是不可变的。)

I want to pass an int[] to this function: public Query<K> setKeys(K... keys);

I assume that you mean that int[] should be the set of keys ... not just one key.

That is impossible. The type parameters of a generic type have to be reference types. Your use-case requires K to be a int.

You have two choices:

  • use Integer (or a mutable int holder class) and pay the performance penalty, or
  • forgo the use of generics and change the signature of that method.

Incidentally, the Integer class keeps a cache of Integer objects for small int values. If you create your objects using Integer.valueOf(int) there's a good chance that you will get a reference to an pre-existing object. (Of course, this only works because Integer objects are immutable.)

不知在何时 2024-10-29 18:48:11

如果您的数组大约有 1000 个(甚至 10,000 或 100,000 个)元素,则内存和性能方面的成本差异可能不会明显,除非您每个数组都处理数千次。用 Integer 编写代码,如果出现性能问题,稍后进行优化。

If your arrays are on the order of 1000 (or even 10,000 or 100,000) elements, the cost difference in terms of memory and performance probably wouldn't be noticeable unless you're processing the arrays thousands of times each. Write the code with Integer and optimize later if you have performance problems.

素染倾城色 2024-10-29 18:48:11

如果您关心性能,您可以编写一个包装公共int的简单类,这意味着您可以进行调用并仍然根据需要改变它。话虽如此,我确实同意您在执行此操作之前要绝对确定需要这种性能改进。

If you're that concerned about performance, you could write a simple class that wraps a public int, thus meaning you can make your call and still mutate it as needed. Having said that, I do agree that you want to make absolute sure you need this performance improvement before doing it.

尝蛊 2024-10-29 18:48:11

如果您确实需要担心装箱/拆箱整数的性能影响,您可以考虑GNU Trove,特别是他们的 TIntArrayList。它可以让您在基元的支持下模拟 ArrayList的功能。也就是说,我不确定您是否需要这个,也不确定这是否正是您正在寻找的。

If you actually do need to worry about the performance implications of boxing/unboxing integers, you could consider GNU Trove, specifically their TIntArrayList. It lets you mimic the functionality of an ArrayList<Integer> while being backed by primitives. That said, I'm not certain you need this, and I'm not certain this is exactly what you are looking for.

风筝在阴天搁浅。 2024-10-29 18:48:11

如果您不希望整数永久装箱,可以传入 Google Collections 库 (http://guava-libraries.googlecode.com/ svn/tags/release08/javadoc/com/google/common/primitives/Ints.html#asList(int...)),这将是一个 List 支持数组。这些值在被访问时会被装箱,因此只有当这些值没有被多次访问时这才有意义。

If you don't want the integers permanently boxed, you could pass in the result of Ints.asList() from the Google Collections library (http://guava-libraries.googlecode.com/svn/tags/release08/javadoc/com/google/common/primitives/Ints.html#asList(int...)), which would be a List<Integer> backed by the array. The values will get boxed as they're accessed, so this only makes sense if the values are not being accessed lots of times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文