C # Language Foundation-collection (ArrayList collection)

Source: Internet
Author: User

Collections and Special Collections

Basic information about the collection:

The System.Collections namespace contains interfaces and classes that define a collection of various objects such as lists, queues, bit arrays, hash tables, and dictionaries.
The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type security and performance than non-generic strongly typed collections.
Ystem. The Collections.specialized namespace contains a dedicated and strongly typed collection, such as a linked list dictionary, a bit vector, and a collection that contains only strings.

Common collections are ArrayList classes, and special collections typically use the queue collection, stack stack collection, and Hashtable Hashtable collection.

Collections, unlike arrays, are a combination of a variable number of elements that may share certain characteristics and need to be manipulated in some way. In general, the types of these elements are the same.

The difference between a collection and an array is that an array is a contiguous area of data of the same type, and the collection can be discontinuous and multiple data types.

First, ArrayList Collection:

ArrayList implements the IList, ICollection, IEnumerable interface;

ArrayList is similar to the name of array, now compare the similarities and differences between the two.

Same point:

(1) Both implementations of IList, ICollection, IEnumerable interface;

(2) Both can use an integer index to access elements in the collection, including reads and assignments, and the indexes in the collection start at 0.

Different points:

(1) A ArrayList is a collection, and an array is a set;

(2) ArrayList is a concrete class, array is an abstract class;

(3) The array must specify the number of elements at instantiation, which cannot be changed once the number is determined, and ArrayList expands this by not specifying the number of collection elements (with default initial capacity) when instantiating an ArrayList instance, and of course you can specify the initial capacity;

(4) Use the Length property when getting the number of elements of an array, and use the Count property when getting the number of elements of the ArrayList collection;

(5) Arrays can have multidimensional, and ArrayList can only be one dimension.

ArrayList specific features provided:

ArrayList al = new ArrayList ();//initialization

Al. ADD (3);

Al. ADD (5);

Al. ADD (1);

Al. ADD (8);

Al. ADD (4);

Error because no 5th index is available, so direct assignment is not possible

AL[5] = 9;

AL[4] = 9;

View number

Console.WriteLine (al. Count);

Emptying the collection

Al. Clear ();

Clone collection

ArrayList al1 = new ArrayList ();

Al1 = (ArrayList) al. Clone ();

Console.WriteLine (Al1[2]);

Determine if an element is included

Console.WriteLine (al. Contains (6));

View the index number of the first occurrence of the element if not, return-1

Console.WriteLine (al. IndexOf (1));

View the index number of the last occurrence of this element

Console.WriteLine (al. LastIndexOf (9));

In index 1th, insert a 4

Al. Insert (1,4);

Console.WriteLine (Al[1]);

Remove the first occurrence of a value from an element

Al. Remove (4);

Console.WriteLine (Al[1]);

Remove an element on an index number

Al. RemoveAt (3);

Console.WriteLine (Al[3]);

for (int i = 0; i < al. Count; i++)

{

Console.WriteLine (Al[i]);

}

Console.WriteLine ();

Al. Sort ();//sorting, ascending

for (int i = 0; i < al. Count; i++)

{

Console.WriteLine (Al[i]);

}

Change to descending order

Al. Reverse ();//Flip Collection

Example: 1, enter the number of people, enter the score of each person, the average score, to find the highest score, the lowest score, written set.

ArrayList a = new ArrayList ();

Double m = 0;

Console.Write ("Please enter the number of people:");

int b = Int. Parse (Console.ReadLine ());

for (int i = 0; i < b; i++)

{

Console.Write ("Input score:");

A.add (double. Parse (Console.ReadLine ()));

M + = Double. Parse (A[i]. ToString ());

}

Console.Write ("Average divided into:" + (m/b));

A.sort ();

Console.Write ("Lowest divided into:" +a[0]);

Console.Write ("Up to:" + a[b-1]);

Console.ReadLine ();

2, each person's name, age deposit collection, according to the age from the big to the small arrangement, the name also needs to arrange, needs to know the age biggest is who.

Law One:

Console.Write ("Please enter the number of people:");

int n = Int. Parse (Console.ReadLine ());

ArrayList XM = new ArrayList ();

ArrayList age = new ArrayList ();

for (int i = 0; i < n; i++)

{

Console.Write ("Please enter the first name of {0} person:", (i + 1));

Xm. ADD (Console.ReadLine ());

Console.Write ("Please enter {0} The age of the individual:", (i + 1));

Age. ADD (int. Parse (Console.ReadLine ()));

}

for (int i = 0; i < n; i++)

{

for (int j = i; J < n-1; J + +)

{

if (int. Parse (Age[i]. ToString ()) < int. Parse (Age[j + 1]. ToString ()))

{

int zh = Int. Parse (Age[i]. ToString ());

Age[i] = age[j + 1];

Age[j + 1] = zh;

String z = xm[i]. ToString ();

Xm[i] = xm[j + 1];

Xm[j + 1] = Z;

}

}

}

Console.WriteLine ("The oldest is {0}, is {1} years old", Xm[0], age[0]);

Console.ReadLine ();

Law II:

Console.WriteLine ("Please enter the number of people:");

int a = Int. Parse (Console.ReadLine ());

ArrayList JH = new ArrayList ();

for (int i = 0; i < A; i++)

{

Console.Write ("Please enter the name of {0} person:", i + 1);

Jh. ADD (Console.ReadLine ());

Console.Write ("Please enter the age of the person {0}:", i + 1);

Jh. ADD (Console.ReadLine ());

}

Console.WriteLine ();

for (int i = 1; i < A * 2; i = i + 2)

{

for (int j = i; j < A * 2-2; j = j + 2)

{

if (int. Parse (Jh[i]. ToString ()) < int. Parse (Jh[j + 2]. ToString ()))

{

int huan = Int. Parse (Jh[i]. ToString ());

Jh[i] = jh[j + 2];

Jh[j + 2] = Huan;

String o = jh[i-1]. ToString ();

Jh[i-1] = jh[j + 1];

Jh[j + 1] = O;

}

}

}

Console.WriteLine ("The oldest is:" + jh[0] + "Age:" + jh[1]);

Console.ReadLine ();

Note: To borrow the definition set name of the previous question

Traversal collection: foreach (Object aa in JH)

{

Console.WriteLine (AA);

}

Traversal array: int [] array = new int[]{2,3,4,6,7,8,9,2};

foreach (int aa in array)

{

Console.WriteLine (aa+2);

}

C # Language Foundation-collection (ArrayList collection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.