是否在.NET中列出的工作方式相同的ArrayList中的Java? [英] Does the List in .NET work the same way as arraylist in Java?

查看:142
本文介绍了是否在.NET中列出的工作方式相同的ArrayList中的Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学会了Java的,我被告知ArrayList的工作是这样的:

When I learned Java, I was told that the arraylist works this way:

  • ,它创建一个房间10个元素的数组。

  • It creates an array with room for 10 elements.

在第11个元素被添加,它会创建一个新的列表室20个元素,以及10种元素复制到新的数组。这将重复,直到没有更多的元素来添加,或到最大尺寸

When the 11th element is added, it is created a new list with room for 20 elements, and the 10 elements are copied into the new array. This will repeat as until there are no more elements to add, or to a maximum size.

是.NET构建以同样的方式列表?

Is the List in .NET constructed the same way?

推荐答案

您可以很容易地通过查询列表的容量测试这个

You can easily test this by querying a List's Capacity:

    var a = new List<string>();

    Console.WriteLine(a.Capacity); // Writes 0

    a.Add("abc");

    Console.WriteLine(a.Capacity); // Writes 4

    a.Add("abc");
    a.Add("abc");
    a.Add("abc");
    a.Add("abc");

    Console.WriteLine(a.Capacity); // Writes 8

所以也没有在实例分配任何余地可言,但在第一次添加的项目。从8它长到16,32,等等...

So it doesn't allocate any room at all upon instantiation, but upon first added item. From 8 it grows to 16, 32, etc...

这篇关于是否在.NET中列出的工作方式相同的ArrayList中的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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