大多数内存成长Java中的数组有效的方法? [英] Most memory efficient way to grow an array in Java?

查看:176
本文介绍了大多数内存成长Java中的数组有效的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是太在意时间效率(该操作将是罕见的),而是有关内存效率:我成长数组但不具有临时两次的所有值

有没有长出了大阵比创建了所有的值一个新的,复制的更有效的方法?像,用一个新的串联呢?

关于有存储在另一个阵列固定大小的数组和重新分配/复制什么是顶级吗?那会留在原地的实际值?

我知道ArrayList的,但我需要大量的控制有关访问数组和接入需要非常快。举例来说,我觉得我preFER A [I] al.get(我)

我为什么关心这个主要的原因是,有问题的数组(或多个这样的阵列)很可能会占用主内存足够大的一部分是丢弃原来之前创建一个双大小的复本的一贯策略可能无法正常工作了。这可能意味着我需要重新考虑整体策略(或我的硬件建议)。


解决方案

  

有以生长更有效的方式
  大阵比创建一个新的
  并复制了所有的值?喜欢,
  用一个新的串联什么?


没有。大概没有语言,从而保证不断增长的数组总会发生,而不复制。一旦你分配给阵列的空间和做其他的事情,你极有可能在内存中的其他对象数组结束后的权利。在这一点上,这是从根本不可能成长数组不复制它。


  

大约有固定大小的数组什么
  存储在另一个数组并重新分配
  /复制顶层吗?会是
  留在原地的实际值?


您的意思是有一个数组的数组,并把它作为一个大阵由底层阵列的级联?是的,那将工作(下称做间接作假的方法),在Java中,对象[] [] 简直就是指针,以<$ C $数组C> [对象] 实例。

I'm not too concerned about time efficiency (the operation will be rare), but rather about memory efficiency: Can I grow the array without temporarily having all the values twice?

Is there a more efficient way to grow a large array than creating a new one and copying over all the values? Like, concatenating it with a new one?

What about having fixed-size arrays stored in another array and reallocate / copy that top-level one? Would that leave the actual values in place?

I'm aware of ArrayList, but I need a lot of control about accessing the array and the access needs to be very fast. For instance, I think I prefer a[i] to al.get(i).

The main reason why I care about this is that the array in question (or a number of such arrays) might very well occupy a large enough portion of main memory that the usual strategy of creating a double sized copy before discarding the original might not work out. This may mean that I need to reconsider the overall strategy (or up my hardware recommendations).

解决方案

Is there a more efficient way to grow a large array than creating a new one and copying over all the values? Like, concatenating it with a new one?

No. And probably there is no language, that guarantees growing an array will always take place without copying. Once you allocate the space for the array and do something else, you most likely have other objects in memory right after the end of the array. At that point, it's fundamentally impossible to grow the array without copying it.

What about having fixed-size arrays stored in another array and reallocate / copy that top-level one? Would that leave the actual values in place?

You mean have an array of arrays and treat it as one large array consisting of a concatenation of the underlying arrays? Yes, that would work (the "faking it by doing indirection" approach), as in Java, Object[][] is simply an array of pointers to Object[] instances.

这篇关于大多数内存成长Java中的数组有效的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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