关于ArrayList类越界的问题,搞不明白,请大家指教.
教程上说.Capacity为ArrayList对象的读写属性.获取或设置ArrayList 可包含的元素数。但我设置了Cappacity=3;我却包含了大于三个元素,怎么没有提示出错呢?我的开发环境为vs2005,c#,WEB项目.
代码如下:
ArrayList arr = new ArrayList();
arr.Capacity = 3;
arr.Add( "a ");
arr.Add( "b ");
arr.Add( "c ");
arr.Add( "d ");
arr.Add( "e ");
arr.Add( "f ");
arr.Add( "g ");
Response.Write(arr.Count + " <br> ");
Response.Write(arr[0] + " <br> ");
Response.Write(arr[1] + " <br> ");
Response.Write(arr[2] + " <br> ");
Response.Write(arr[3] + " <br> ");
Response.Write(arr[4] + " <br> ");
Response.Write(arr[5] + " <br> ");
Response.Write(arr[6] + " <br> ");
------解决方案--------------------ArrayList是动态数组,每当你添加的元素比现有容量大的时候,容量自动增加一倍