什么是由一个ArrayList参数(INT初始容量)的意思 [英] What's meant by parameter (int initial capacity) in an arraylist

查看:187
本文介绍了什么是由一个ArrayList参数(INT初始容量)的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是由参数意思(INT参数:initialCapacity)的ArrayList ,我认为这是元素的数量,但它我这样做的时候没有工作:

 公共类MyClass的{
    私人的ArrayList<整数GT; ARR;
    公共MyClass的(INT n_elements){
        ARR =新的ArrayList<整数GT;(n_elements);
    }
}


解决方案

这是最初的容量,换言之的项目数的ArrayList 将分配与开始作为内部存储项目。

的ArrayList 可以包含任何数量的项目(只要你有这方面的记忆),并做大量的初始插入的时候,你可以告诉的ArrayList 来分配更大的存储开始与当它试图为下一个项目分配更多的空间不浪费CPU周期。

例如:

  ArrayList的列表=新的ArrayList<整数GT;(2);
list.add(1); //尺寸()== 1
list.add(2); //大小()== 2,列表是装
list.add(3); //尺寸()== 3,列表展​​开以腾出空间给所述第三元件

What's meant by parameter (int initialCapacity) in an ArrayList, I thought it's the number of elements but it didn't work when I did this:

public class MyClass {
    private ArrayList<Integer> arr;
    public MyClass(int n_elements) {
        arr = new ArrayList<Integer>(n_elements);
    }
}

解决方案

It's the initial capacity, i.e. the number of items that ArrayList will allocate to begin with as the internal storage of items.

ArrayList can contain "any number of items" (as long you have the memory for it) and when doing large initial insertions you can tell ArrayList to allocate a larger storage to begin with as to not waste CPU cycles when it tries to allocate more space for the next item.

Example:

ArrayList list = new ArrayList<Integer>(2);
list.add(1); // size() == 1
list.add(2); // size() == 2, list is "filled"
list.add(3); // size() == 3, list is expanded to make room for the third element

这篇关于什么是由一个ArrayList参数(INT初始容量)的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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