aspnet外文翻译-jsp程序(编辑修改稿)内容摘要:

as a number of similarly named functions that perform the same tasks. For example, each collection type has a Clear method that removes all elements from the collection, and a Count property that returns the number of elements in the collection. In fact, the last subsection “Similarities Among the Collection Types” examines the mon traits found among the collection types. Working with the ArrayList Class The first type of collection we’ll look at is the ArrayList. With an ArrayList, each item is stored in sequential order and is indexed numerically. In our following examples, keep in mind that the developer need not worry himself with memory allocation. With the standard array, the developer cannot easily add and remove elements without concerning himself with the size and makeup of the array. With all the collections we’ll examine in this chapter, this is no longer a concern. 7 Adding Elements to an ArrayList We create two ArrayList class instances, aTerritories and aStates, on lines 5 and 6, respectively. We then populate the aStates ArrayList with a small subset of the 50 states of the United States using the Add method. The Add method takes one parameter, the element to add to the array, which needs to be of type Object. This Object instance is then appended to the end of the ArrayList. In this example we are simply adding elements of type String to the ArrayList aStates and aTerritories. The Add method is useful for adding one element at a time to the end of the array, but what if we want to add a number of elements to an ArrayList at once. The ArrayList class provides the AddRange method to do just this. AddRange expects a single parameter that supports the ICollection interface. A wide number of .NET Framework classes—such as the Array, ArrayList, DataView, DataSetView, and others—support this interface. We use the AddRange method to add each element of the aStates ArrayList to the end of the aTerritories ArrayList. (To add a range of elements starting at a specific index in an ArrayList, use the InsertRange method.) We add two more strings to the end of the aTerritories ArrayList. Because ArrayLists are ordered sequentially, there might be times when we want to add an element to a particular position. The Insert method of the ArrayList class provides this capability, allowing the developer to add an element to a specific spot in the ArrayList collection. The Insert method takes two parameters: an integer representing the index in which you want to add the new element, and the new element, which needs to be of type Object. In line 23 we add a new string to the start of the aTerritories ArrayList. Note that if we had simply used the Add method, “District of Columbia” would have been added to the end of a Territories. Using Insert, however, we can specify exactly where in the ArrayList this new element should reside. Removing Elements from an ArrayList The ArrayList class also provides a number of methods for removing elements. We can remove a specific element from an ArrayList with the Remove method. If you attempt to remove an element that does not exist, an ArgumentException exception will be allows you to take out a particular element from an ArrayList。 RemoveAt, allows the developer to remove an element at a specific position in the ArrayList. Both Remove and RemoveAt dissect only one element from the ArrayList at a time. We can remove a chunk of elements in one fell swoop by using the RemoveRange method. This method expects two parameters: an index to start at and a count o。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。