简体   繁体   English

ArrayList.TrimToSize()和Array之间的区别?

[英]Difference between ArrayList.TrimToSize() and Array?

Generally, They say that we have moved from Array to ArrayList for the following Reason 通常,他们说我们出于以下原因从Array移到ArrayList

Arrays are fixed size where as Array Lists are not . 数组是固定大小的,而数组列表则不是。

One of the disadvantages of ArrayList is: ArrayList的缺点之一是:

When it reaches it's capacity , ArrayList becomes 3/2 of it's actual size. 当达到其容量时,ArrayList变为其实际大小的3/2。 As a result , Memory can go wasted if we donot utilize the space properly.In this scenario, Arrays are preferred. 结果,如果我们没有正确利用空间,内存可能会浪费掉。在这种情况下,首选数组。

If we use ArrayList.TrimSize(), will that make Array List a unanimous choice? 如果我们使用ArrayList.TrimSize(),将使Array List成为一个一致的选择吗? Eliminating the only advantage(fixed size) Array has over it? 消除唯一的优势(固定大小)阵列比它有优势吗?

One short answer would be: trimToSize doesn't solve everything, because shrinking an array after it has grown - is not the same as preventing growth in the first place; 一个简短的答案是:trimToSize不能解决所有问题,因为在数组增长之后缩小数组-不同于首先阻止增长; the former has the cost of copying + garbage collection. 前者的成本是复制+垃圾回收。

The longer answer would be: int[] is low level, ArrayList is high level which means it's more convenient but gives you less control over the details. 更长的答案是:int []是低级的,ArrayList是高级别的,这意味着它更方便,但对细节的控制较少。 Thus in a business-oriented code (eg manipulating a short list of "Products") i'll prefer ArrayList so that I can forget about the technicalities and focus on the business. 因此,在面向业务的代码中(例如,操作“产品”的简短列表),我将更喜欢ArrayList,这样我就可以忽略技术性并专注于业务。 In a mathematically-oriented code i'll probably go for int[]. 在面向数学的代码中,我可能会选择int []。

There are additional subtle differences, but i'm not sure how relevant they are to you. 还有其他细微的差异,但我不确定它们与您的相关性如何。 Eg concurrency: if you change the data of ArrayList from several threads simultaneously, it will intentionally fail, because that's the intuitive requirement for most business code. 例如,并发性:如果您同时从多个线程更改ArrayList的数据,则将有意失败,因为这是大多数业务代码的直观要求。 An int[] will allow you to do whatever you want, leaving it up to you to make sure it makes sense. 一个int []将允许您做任何您想做的事情,由您自己来确定是否有意义。 Again, this can all be summarized as "low level"... 同样,这些都可以概括为“低级” ...

If you are developing an extremely memory critical application, need resizability as well and performance can be traded off, then trimming array list is your best bet. 如果您正在开发对内存至关重要的应用程序,则还需要可调整大小,并且可以权衡性能,那么修剪数组列表是您的最佳选择。 This is the only time, array list with trimming will be unanimous choice. 这是唯一一次带有修剪的数组列表的一致选择。

In other situations, what you are actually doing is: 在其他情况下,您实际正在执行的操作是:

  1. You have created an array list. 您已经创建了一个数组列表。 Default capacity of the list is 10. 列表的默认容量为10。
  2. Added an element and applied trim operation. 添加了元素并应用了修剪操作。 So both size and capacity is now 1. How trim size works? 因此,大小和容量现在都是1.修剪尺寸如何工作? It basically creates a new array with actual size of the list and copies old array data to new array. 它基本上会创建一个具有列表实际大小的新数组,并将旧的数组数据复制到新的数组。 Old array is left for grabage collection. 旧阵列留作抓斗收集。
  3. You again added a new element. 您再次添加了一个新元素。 Since list is full, it will be reallocated with more 50% spaces. 由于列表已满,将重新分配50%的空间。 Again, procedure similar to 2 will be followed. 同样,将遵循类似于2的过程。
  4. Again you call TrimSize and it follows same procedure as 2. 再次调用TrimSize,它遵循与2相同的过程。
  5. Things repeats... 事情重复了...

So you see, we are incurring lots of performance overhead just to keep list capacity and size same. 因此,您会发现,为了保持列表容量和大小不变,我们会承担很多性能开销。 Fixed size is not offering you anything advantageous here except saving few more extra spaces which is hardly an issue in modern machines. 固定大小在这里没有提供任何优势,只是节省了很少的额外空间,这在现代机器中几乎不是问题。

In a nutshell, if you want resizability without writing lots of boilerplate code, then array list is unanimous choice. 简而言之,如果您希望在不编写大量样板代码的情况下实现可调整性,那么数组列表是一致的选择。 But if size never changes and you don't need any dynamic function such as removal operation, then array is better choice. 但是,如果大小从不改变,并且您不需要任何动态功能(例如删除操作),那么数组是更好的选择。 Few extra bytes are hardly an issue. 很少有额外的字节几乎不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM