1.54. C# dynamic arrayList

发布时间 : 2025-10-25 13:34:32 UTC      

Page Views: 9 views

A dynamic arrayList represents an ordered collection of objects thatcan be indexed separately. It can basically replace an array. However, unlike an array, you can use the index to add and remove items at a specified location, and the dynamic array automatically resizes it. It also allows dynamic memory allocation, adding, searching, and sorting items in the list.

1.54.1. Methods and properties of the ArrayList class #

The following table lists some ArrayList class common properties:

Attribute

Description

Capacity

Gets or sets the number of elements that an ArrayList can contain.

Count

Gets the number of elements actually contained in the ArrayList.

IsFixedSize

Gets a value indicating whether the ArrayList has a fixed size.

IsReadOnly

Gets a value indicating whether the ArrayList is read-only.

IsSynchronized

Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).

Item [Int32]

Gets or sets the element at the specified index.

SyncRoot

Gets an object to access the ArrayList synchronously.

The following table lists some ArrayList class common methods:

Serial number

Method name & description

1

Public virtual int Add (object value); add an object at the end of the ArrayList.

2

Public virtual void AddRange (ICollection c); add the element of ICollectionat the end of the ArrayList.

3

Public virtual void Clear (); removes all elements from the ArrayList.

4

Public virtual bool Contains (object item); determines whether an element isin ArrayList.

5

Public virtual ArrayList GetRange (int index, int count); returns an ArrayList that represents a subset of the elements in the source ArrayList.

6

Public virtual int IndexOf (object); returns the index at which a value first appears in ArrayList, starting at zero.

7

Public virtual void Insert (int index, object value); insert an element at the specified index of ArrayList.

8

Public virtual void InsertRange (int index, ICollection c); inserts an element of a collection at the specified index of ArrayList.

9

Public virtual void Remove (object obj); removes the first occurrence of thespecified object from the ArrayList.

10

Public virtual void RemoveAt (int index); removes the element at the specified index of the ArrayList.

11

Public virtual void RemoveRange (int index, int count); removes a range of elements from the ArrayList.

12

Public virtual void Reverse (); reverses the order of elements in ArrayList.

13

Public virtual void SetRange (int index, ICollection c); copy the elements of a collection to a range of elements in ArrayList.

14

Public virtual void Sort (); sorts the elements in the ArrayList.

15

Public virtual void TrimToSize (); sets the capacity to the actual number ofelements in the ArrayList.

1.54.2. Example #

The following example demonstrates the concept of dynamic arrayList:

Example #

using System; using System.Collections; namespace CollectionApplication { class Program { static void Main(string[] args) { ArrayList al = new ArrayList(); Console.WriteLine("Adding some numbers:"); al.Add(45); al.Add(78); al.Add(33); al.Add(56); al.Add(12); al.Add(23); al.Add(9); Console.WriteLine("Capacity: {0} ", al.Capacity); Console.WriteLine("Count: {0}", al.Count); Console.Write("Content: "); foreach (int i in al) { Console.Write(i + " "); } Console.WriteLine(); Console.Write("Sorted Content: "); al.Sort(); foreach (int i in al) { Console.Write(i + " "); } Console.WriteLine(); Console.ReadKey(); } } } 

When the above code is compiled and executed, it produces the following results:

Adding some numbers: Capacity: 8 Count: 7 Content: 45 78 33 56 12 23 9 Content: 9 12 23 33 45 56 78 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。