Collection common operations Add, traverse, remove
Namespace System.Collections
ArrayList variable-length arrays, using arrays similar to
Attribute Capacity Count
Method
ADD () AddRange () Remove () RemoveAt () Clear ()
Contains () ToArray ()
A collection of Hashtable key-value pairs (KeyValuePair), similar to a dictionary
A, ArrayList operations on value types
Copy Code code as follows:
Using System;
Using System.Collections;
Namespace _08_arraylist {
Araylist action on a value type
Class Program {
static void Main (string[] args) {
ArrayList is not as big as an array. The advantage is that unlike arrays, which have a length disadvantage, the data type does not limit what type of data can be put into it, there are many errors
ArrayList arylist = new ArrayList ();
ArrayList add
Arylist. ADD (1000);
Arylist. Add ("John");//Parameter type object so you can add multiple types of parameters to remove the same need for type conversions
Arylist. ADD (3000);
Arylist. ADD (4000); Boxing action takes a value type to convert a reference type
Arylist. ADD (5000);
int [] arr = {1, 2, 3, 4};
Arylist. AddRange (arr); The AddRange parameter is an object that implements the Icollections interface and can add arrays, array, ArrayList, etc.
Number of elements in the collection using Count = array length
Console. WriteLine ("Collection content Length" + arylist.) Count);
Capacity for the capacity of the set is variable general *2 growth
Console. WriteLine (arylist. Capacity);
Accessing the first element of a collection
int firstlist = Convert. ToInt32 (Arylist[0]);
Console. WriteLine (firstlist. ToString ());
ArrayList traversal
int sum2 = 0;
for (int i = 0; i < arylist. Count; i++) {
Sum2 + = Convert.ToInt32 (Arylist[i])/unboxing operation
Console. WriteLine (Arylist[i]. ToString ());
}
foreach (object item in Arylist) {
Sum2 + = Convert. ToInt32 (item);
}
Console. WriteLine (SUM2);
ArrayList remove just remove not delete
Arylist. Remove (1000); Remove content is 1000 remove removes an internal object
Arylist. RemoveAt (1); Remove the second item by index
Note Removing elements ArrayList array will reassign the index so the removal is best a flashback remove element
If you remove all elements and use clear directly
Arylist. Clear ();
if (arylist. Contains (3000)) {
Console. WriteLine ("contained");
}
ArrayList and ToArray () but not very meaningful.
This is where you add a value type to the ArrayList. What about the reference type???? Add an object for the student class?
Console. Read ();
}
}
}
B, ArrayList operations on reference types
Copy Code code as follows:
Using System;
Using System.Collections;
Namespace _09_arraylistobject {
ArrayList action on a reference type
Class Student {
Public Student (string name, int age) {
this. Name = name;
this. Age = age;
}
private string name;
public string Name {
get {
return name;
}
set {
name = value;
}
}
private int age;
public int Age {
get {
return age;
}
set {
Age = value;
}
}
}
Class Program {
static void Main (string[] args) {
Student XYY = new Student ("Xiao Yue Yue", 14);
Student FJ = new Student ("Sister Feng", 18);
Student FR = new Student ("Sister Furong", 19);
Student XL = new Student ("Brother Sharp", 20);
ArrayList student = new ArrayList ();
Student. ADD (XYY); Add also can use AddRange
Student. ADD (FJ);
Student. Add (FR);
Student. ADD (XL);
Removed from
Student. Remove (FJ);//This is the object, not the value, that is removed.
Student. RemoveAt (1);//Index removal
Remove FJ Because remove is object by index removal
Student stu = new Student ("Sister Feng", 18);
Student. Remove (Stu);
Console.WriteLine (student. Contains (Stu));//false retrieved by index because Stu is not the same as the FJ address
Traverse
for (int i = 0; i < student. Count; i++) {
Student s = student[i] as Student; Because a boxing operation occurs before adding it now requires a unboxing student[i] cannot point name
Console. WriteLine (S.name);
}
ArrayList ary = new ArrayList ();
ary. Add ("Sister Feng");
ary. Add ("Xiao Yue Yue");
The string class is also a reference type, but there's something special here.
String name = "Sister Feng";
Console. WriteLine (ary. Contains (name);//string compares content so returns true
Get the student object according to the student's name although ArrayList can be achieved but rather complex and inefficient, then learn Hashtable
Console. Read ();
}
}
}
C, HashTable
Copy Code code as follows:
Using System;
Using System.Collections;
Namespace _10_hashtable {
Class Student {
Public Student (string name, int age) {
this. Name = name;
this. Age = age;
}
private string name;
public string Name {
get {
return name;
}
set {
name = value;
}
}
private int age;
public int Age {
get {
return age;
}
set {
Age = value;
}
}
}
Class Program {
static void Main (string[] args) {
Still uses the student class to implement the
Hashtable key value pairs form key value equivalent to a dictionary can quickly find an object based on the student's name
Student XYY = new Student ("Xiao Yue Yue", 14);
Student FJ = new Student ("Sister Feng", 18);
Student FR = new Student ("Sister Furong", 19);
Student XL = new Student ("Brother Sharp", 20);
Hashtable student = new Hashtable ();
Student. Add ("Xiao Yue Yue", XYY);
Student. Add ("Sister Feng", FJ);
Student. Add ("Sister Furong", FR);
Student. Add ("Brother Sharp", XL);
Student. Add ("Brother Sharp", XL); The keyword key in the error dictionary does not allow repetition so you can't add brother sharp.
Remove because there is no index so there is no RemoveAt ()
Student. Remove ("Xiao Yue Month");//Removed according to key
Student. Clear ();
Student. ContainsKey ("Sister Feng");/To determine if it contains this key
Traversal cannot use a for to traverse because the dictionary has no index. Only use foreach
By key through the common
foreach (Object key in student. Keys) {
Student stu = Student[key] as Student;
Console. WriteLine (key);
Console. WriteLine (Stu. Age);
}
Traversing by value
foreach (object value in student. Values) {
Student stu = value as Student;
if (stu!= null) {
Console. WriteLine (Stu. Age);
}
}
If you do not press key and do not press value to traverse the dictionary traversal is to iterate over the dictionary's key value pairs
foreach (DictionaryEntry de in student) {
Console. WriteLine (DE. Key);
Student s = de. Value as Student; Because you get the object type, you need to convert to use the
Console. WriteLine (S.age);
}
Student s2 = student["Xiao Yue Yue"] as Student;//Find this object by name get other properties
if (s2!= null) {
Console. WriteLine (S2. Age);
}
Console. Read ();
}
}
}
D, Practice
Copy Code code as follows:
Using System;
Using System.Collections;
Namespace _11_arraylist Practice {
Class Program {
Or that sentence to understand the topic after the idea and then start writing code thinking the most important
static void Main (string[] args) {
Two sets {"A", "B", "C", "D", "E"} and {"D", "E", "F", "G", "H"}, the two sets are stripped of duplicates and merged into a
ArrayList Ary1 = new ArrayList {"A", "B", "C", "D", "E"};
ArrayList Ary2 = new ArrayList {"D", "E", "F", "G", "H"};
Traverse two Sets
for (int i = 0; i < Ary2. Count; i++) {//Loop traversal ary2 element and ary1 one by one comparison if the same value is present do not add or append to Ary1
if (!ary1. Contains (Ary2[i])) {//Contains method if not, I don't know how complicated it is.
Ary1. ADD (Ary2[i]);
}
}
foreach (object item in Ary1) {
Console. Write (item);
}
Randomly generated numbers between 10 and 1-100 are placed in ArrayList, requiring that these 10 numbers cannot be duplicated and are both even
ArrayList arylist = new ArrayList ();
int numcount = 0;
while (true) {
Random ran = new Random ();
int num = ran. Next (1, 100);
if (num% 2 = 0 &&!arylist. Contains (num)) {//Add!arylist. Contains (NUM) This sentence solves the following problem
Arylist. ADD (num); Why does the direct run always show the first one to meet the criteria value and single step debugging shows the correct result???
}
if (arylist. Count = = 10) {
break;
}
}
foreach (object item in Arylist) {
Console. WriteLine (item);
}
One string is a series of integers separated by a space, writing a program to rearrange the integers in the following order: The odd number appears on the left and the even on the right. For example, ' 2 7 8 3 22 9 ' shows ' 7 3 9 2 8 ' 22
String str = "2 7 8 3 22 9";
ArrayList ary3 = new ArrayList ();
ArrayList Ary4 = new ArrayList ();
string [] s = str. Split (")";
foreach (var item in s) {
If Convert. ToInt32 (item)% 2 = 0) {
Ary4. ADD (item);
} else {
Ary3. ADD (item);
}
}
Ary3. AddRange (ARY4); Because the Ary1 type is object, you cannot use the Join method of the string class to implement a character concatenation after you have learned that a generic collection can handle
String newstr = Ary3[0]. ToString ()//simple way to remove spaces
for (int i = 1; i < Ary3. Count; i++) {
Newstr + = "" + ary3[i];
}
Console. WriteLine ("original string: {0}, filtered string {1}", str, NEWSTR + "test");
Console. Read ();
}
}
}