当我们删除元素时,ArrayList 的容量会减少吗? [英] Does the capacity of ArrayList decrease when we remove elements?

查看:27
本文介绍了当我们删除元素时,ArrayList 的容量会减少吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ArrayList 的默认容量为 10 个对象.当大小超过 10 个对象时,ArrayList 将在内部增加其容量.当我们从 ArrayList 中删除对象时,容量是否会减少.

ArrayList have a default capacity of 10 objects. As the size exceeds 10 objects, an ArrayList will internally increase its capacity. Does the capacity decrease when we remove the object from ArrayList.

如果 ArrayList 容量没有减少,是否会导致性能问题?

If the ArrayList capacity doesn't decrease, could this lead to performance issues?

推荐答案

它不会自动减少这个.来自文档.

It doesn't decrease this automatically. From the doc.

    public void trimToSize() 

将此 ArrayList 实例的容量修剪为列表的当前大小.应用程序可以使用此操作来最小化 ArrayList 实例的存储空间.

Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.

这篇关于当我们删除元素时,ArrayList 的容量会减少吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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