Learning to use ASP. NET's system. Collections. arraylist class

Source: Internet
Author: User
Arraylist is a dynamic array of. net.
Main members:
 
/* Static Method */arraylist. adapter () // wrap other ilist objects as arraylist and use arraylist. fixedsize () // The package is a fixed-capacity arraylist. readonly () // The package is read-only arraylist. repeat () // create an arraylist/* attribute */capacity // capacity count // number of elements isfixedsize // is it packaged as a fixed capacity based on the specified number of repeated values? Isreadonly // is it encapsulated as read-only? /* Method */Add () // Add addrange () // Add a set binarysearch () // use the binary method to quickly search. Sort the set before searching. If the set is not found, a result is returned.
Element values can be of different types, can be repeated, and can be null:
Protected void button#click (Object sender, eventargs e) {arraylist arr = new arraylist (); arr. add ("ABC"); arr. add (1, 123); arr. add (1, 3.14); arr. add (null); arr. add (null); string S = arr [0]. tostring (); int n = (INT) Arr [1]; double F = (double) Arr [2]; textbox1.text = string. concat (S, ",", N, ",", f); // ABC, 123, 3.14 // textbox1.text = string. concat (ARR [0], ",", arr [1], ",", arr [2]); // same as above}
Arraylist. Repeat (), addrange () and traversal:
Protected void button#click (Object sender, eventargs e) {arraylist arr1 = arraylist. repeat ("AAA", 3); arraylist arr2 = arraylist. repeat ("BBB", 4); arr1.addrange (arr2); string S1, S2; S1 = S2 = ""; foreach (var x in arr1) {S1 + = x. tostring () + ""; // AAA aaa bbb BBB} For (INT I = 0; I
Getrange (), setrange ():
Protected void button#click (Object sender, eventargs e) {arraylist arr1 = new arraylist (6); // specify the capacity arr1.add (1); arr1.add (2); arr1.add (3 ); arr1.add (4); arr1.add (5); arr1.add (6); arraylist arr2 = arr1.getrange (2, 3); string S1 = string. join (",", arr2.toarray (); // 3, 4, 5 arr1.setrange (3, arr2); // The elements in arr2 overwrite the specified location of arr1, arr1 must have a location. String S2 = string. join (",", arr1.toarray (); // 1, 2, 3, 3, 4, 5 textbox1.text = S1 + "\ n" + S2 ;}
Flip and sort:
Protected void button#click (Object sender, eventargs e) {arraylist arr = new arraylist (10); // specify the ARR capacity. add (1); arr. add (3); arr. add (5); arr. add (7); arr. add (9); arr. add (2); arr. add (4); arr. add (6); arr. add (8); arr. add (0); arr. reverse (); string S1 = string. join (",", arr. toarray (); // 0, 8, 6, 4, 2, 9, 7, 5, 3, 1 arr. reverse (0, arr. count/2); string S2 = string. join (",", arr. toarray (); // 2, 4, 6, 8, 0, 9, 7, 5, 3, 1 arr. sort (); string S3 = string. join (",", arr. toarray (); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 textbox1.text = S1 + "\ n" + S2 + "\ n" + S3 ;}
Insert and delete:
 
Protected void button#click (Object sender, eventargs e) {arraylist arr = new arraylist (9); // specify the capacity for (INT I = 0; I
Indexof (), lastindexof ():
 
Protected void button#click (Object sender, eventargs e) {char [] chararr = "google ". tochararray (); arraylist arr = new arraylist (chararr); // Its creation parameter can be an icollection string S1 = string. join (",", arr. toarray (); // G, O, O, G, L, E int n1 = arr. indexof ('G'); // 0 int n2 = arr. lastindexof ('G'); // 3 textbox1.text = string. concat (S1, "\ n", N1, "\ n", N2 );}
Binarysearch ():
Protected void button#click (Object sender, eventargs e) {string [] strarr = {"one", "two", "three", "four", "five ", "Six", "Seven", "eight", "Nine", "Ten"}; arraylist arr = new arraylist (strarr); arr. sort (); int I = arr. binarysearch ("Nine"); // 3 string STR = string. join (",", arr. toarray (); // eight, five, four, nine, one, seven, six, ten, three, two textbox1.text = string. concat (I, "\ n", STR );}
Fixedsize (), readonly (), isfixedsize, isreadonly:
Protected void button#click (Object sender, eventargs e) {arraylist arr = new arraylist (); arr. add ("one"); arr = arraylist. fixedsize (ARR); try {arr. add ("two");} catch (exception ERR) {response. write (err. message);} // The set size is fixed. Finally {textbox1.text = "fixedsize =" + arr. isfixedsize. tostring ();} // fixedsize = true} protected void button2_click (Object sender, eventargs e) {arraylist arr = arraylist. repeat ("000", 3); arr [0] = "AAA"; bool b1 = arr. isreadonly; // false arr = arraylist. readonly (ARR); bool b2 = arr. isreadonly; // true // arr [0] = "BBB"; // if the value is re-assigned, textbox1.text = string is abnormal. concat (B1, "\ n", B2 );}
Related Article

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.