C # language basics-set (ArrayList set ),
Set and special set
Basic information of the Set:
The System. Collections namespace contains interfaces and classes that define a set of objects (such as lists, queues, bit arrays, hash tables, and dictionaries.
System. collections. the Generic namespace contains interfaces and classes that define a Generic set. A Generic set allows you to create a strongly typed set. It provides better type security and performance than a non-Generic strong set.
The ystem. Collections. Specialized namespace contains Specialized and strong Collections, such as list dictionaries for links, bitvectors, and Collections that only contain strings.
The commonly used set is the ArrayList class. Special sets generally use the Queue set, Stack set, and Hashtable hash table set.
A set is a combination of variable numbers of elements, which may share certain features and must be operated together in a certain way. In general, these elements are of the same type.
The difference between a set and an array: An array is a continuous area of the same type of data, and a set can be discontinuous and of multiple data types.
I,ArrayListSet:
ArrayList implements the IList, ICollection, and IEnumerable interfaces;
The ArrayList and Array names are very similar. Now we will compare the similarities and differences between the two.
Similarities:
(1) Both implement the IList, ICollection, and IEnumerable interfaces;
(2) Both of them can use integer indexes to access the elements in the Set, including reading and assigning values, and the indexes in the set start from 0.
Differences:
(1) ArrayList is a set, while Array is an Array;
(2) ArrayList is a specific class and Array is an abstract class;
(3) The number of elements must be specified in the array during instantiation. Once the number is determined, it cannot be changed, and ArrayList extends this point, when instantiating an ArrayList instance, you can specify the initial capacity without specifying the number of elements in the Set (with the default initial capacity;
(4) use the Length attribute to get the number of elements in the array, and use the Count attribute to get the number of elements in the ArrayList set;
(5) arrays can have multiple dimensions, while ArrayList can only be one dimension.
ArrayListSpecific functions:
ArrayList al = new ArrayList (); // Initialization
Al. Add (3 );
Al. Add (5 );
Al. Add (1 );
Al. Add (8 );
Al. Add (4 );
Error: because no index is available before index 5, direct assignment is not allowed.
Al [5] = 9;
Al [4] = 9;
View count
Console. WriteLine (al. Count );
Clear collection
Al. Clear ();
Clone set
ArrayList al1 = new ArrayList ();
Al1 = (ArrayList) al. Clone ();
Console. WriteLine (al1 [2]);
Determines whether an element is contained.
Console. WriteLine (al. Contains (6 ));
If no index number is displayed for the first time,-1 is returned.
Console. WriteLine (al. IndexOf (1 ));
View the index number of the last element
Console. WriteLine (al. LastIndexOf (9 ));
Insert a 4 index on Index 1
Al. Insert (1, 4 );
Console. WriteLine (al [1]);
Removes the first occurrence value of an element.
Al. Remove (4 );
Console. WriteLine (al [1]);
Removes an element from 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 (); // Sort in ascending order
For (int I = 0; I <al. Count; I ++)
{
Console. WriteLine (al [I]);
}
Change to descending order
Al. Reverse (); // flip set
Example: 1. Enter the number of people, enter the score of each person, calculate the average score, calculate the highest score, calculate the lowest score, and write it into a set.
ArrayList a = new ArrayList ();
Double m = 0;
Console. Write ("Enter the number of students :");
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 score:" + (m/B ));
A. Sort ();
Console. Write ("shard:" + a [0]);
Console. Write ("Maximum score:" + a [B-1]);
Console. ReadLine ();
2. Store the names and ages of each person in a collection, and sort the names from age to age. You need to know who is the largest age.
Method 1:
Console. Write ("Enter the number of students :");
Int n = int. Parse (Console. ReadLine ());
ArrayList xm = new ArrayList ();
ArrayList age = new ArrayList ();
For (int I = 0; I <n; I ++)
{
Console. Write ("enter the name of the {0} person (s):", (I + 1 ));
Xm. Add (Console. ReadLine ());
Console. Write ("Enter the age of {0} individuals:", (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 largest age is {0}, Which is {1} years old", xm [0], age [0]);
Console. ReadLine ();
Method 2:
Console. WriteLine ("Enter the number of students :");
Int a = int. Parse (Console. ReadLine ());
ArrayList objects = new ArrayList ();
For (int I = 0; I <a; I ++)
{
Console. Write ("enter the name of person {0}:", I + 1 );
Logging. Add (Console. ReadLine ());
Console. Write ("Enter the age of person {0}:", I + 1 );
Logging. 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 (parts [I]. ToString () <int. Parse (parts [j + 2]. ToString ()))
{
Int huan = int. Parse (parts [I]. ToString ());
Pipeline [I] = pipeline [j + 2];
Pipeline [j + 2] = huan;
String o = bytes [I-1]. ToString ();
Pipeline [I-1] = pipeline [j + 1];
Pipeline [j + 1] = o;
}
}
}
Console. WriteLine ("the largest age is:" + region [0] + "Age:" + region [1]);
Console. ReadLine ();
Note: Borrow the definition set name from the previous question
● Traversal set: foreach (object aa in bytes)
{
Console. WriteLine (aa );
}
● Traverse the array: int [] array = new int [] {2, 3, 4, 6, 7, 8, 9, 2 };
Foreach (int aa in array)
{
Console. WriteLine (aa + 2 );
}