从数组中删除项目并收缩数组 [英] Delete item from array and shrink array

查看:37
本文介绍了从数组中删除项目并收缩数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数组中删除一个项目,然后将数组的大小调整为较小的大小?同样,如果我需要添加另一个项目,我该如何增加容量?

How can I delete an item from an array, and then resize the array to the smaller size? Likewise, how can I increase the capacity if I need to add another item?

推荐答案

Java 数组的大小在分配时是固定的,不能更改.

The size of a Java array is fixed when you allocate it, and cannot be changed.

  • 如果要增长"或缩小"现有数组,则必须分配一个适当大小的新数组并复制数组元素;例如使用 System.arraycopy(...)Arrays.copyOf(...).复制循环也能工作,虽然它看起来有点笨重...... IMO.

  • If you want to "grow" or "shrink" an existing array, you have to allocate a new array of the appropriate size and copy the array elements; e.g. using System.arraycopy(...) or Arrays.copyOf(...). A copy loop works as well, though it looks a bit clunky ... IMO.

如果你想从数组中删除"一个或多个项目(真正意义上的......不仅仅是用null替换它们),你需要分配一个新的更小的数组并复制要保留的元素.

If you want to "delete" an item or items from an array (in the true sense ... not just replacing them with null), you need to allocate a new smaller array and copy across the elements you want to retain.

最后,您可以通过为引用类型的数组分配 null 来擦除"它.但这会带来新的问题:

Finally, you can "erase" an element in an array of a reference type by assigning null to it. But this introduces new problems:

  • 如果您使用 null 元素来表示某事,则不能这样做.
  • 所有使用数组的代码现在都必须以适当的方式处理 null 元素的可能性.更多的复杂性和潜在的错误1.
  • If you were using null elements to mean something, you can't do this.
  • All of the code that uses the array now has to deal with the possibility of a null element in the appropriate fashion. More complexity and potential for bugs1.

有 3rd 方库形式的替代方案(例如 Apache Commons ArrayUtils),但您可能需要考虑是否值得添加一个库依赖只是为了一种你可以用 5-10 行代码自己实现的方法.

There are alternatives in the form of 3rd-party libraries (e.g. Apache Commons ArrayUtils), but you may want to consider whether it is worth adding a library dependency just for the sake of a method that you could implement yourself with 5-10 lines of code.

使用 List 类而不是数组更好(即更简单......并且在许多情况下更有效2).这将负责(至少)增加后备存储.还有一些操作负责在列表中的任何位置插入和删除元素.

It is better (i.e. simpler ... and in many cases, more efficient2) to use a List class instead of an array. This will take care of (at least) growing the backing storage. And there are operations that take care of inserting and deleting elements anywhere in the list.

例如,ArrayList 类使用数组作为支持,并根据需要自动增长数组.它不会自动减小后备数组的大小,但您可以使用 trimToSize() 方法告诉它这样做;例如

For instance, the ArrayList class uses an array as backing, and automatically grows the array as required. It does not automatically reduce the size of the backing array, but you can tell it to do this using the trimToSize() method; e.g.

ArrayList l = ...
l.remove(21);
l.trimToSize();  // Only do this if you really have to.

<小时>

1 - 但请注意,显式 if (a[e] == null) 检查本身可能是免费的",因为它们可以与隐式 null 检查当您取消引用 a[e] 的值时发生的情况.


1 - But note that the explicit if (a[e] == null) checks themselves are likely to be "free", since they can be combined with the implicit null check that happens when you dereference the value of a[e].

2 - 我说它在许多情况下更有效",因为 ArrayList 在需要增加后备数组时使用简单的双倍大小"策略.这意味着如果通过重复追加来增加列表,每个元素平均将被复制一次.相比之下,如果您对数组执行此操作,您最终会平均复制每个数组元素接近 N/2 次.

2 - I say it is "more efficient in many cases" because ArrayList uses a simple "double the size" strategy when it needs to grow the backing array. This means that if grow the list by repeatedly appending to it, each element will be copied on average one extra time. By contrast, if you did this with an array you would end up copying each array element close to N/2 times on average.

这篇关于从数组中删除项目并收缩数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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