Asp.net Array Operation Entry instance

Source: Internet
Author: User

An array can be a one-dimensional, multi-dimensional, or staggered array.
The default value of the value array element is 0, and the default value of the referenced element is null. [Remember that many if statements need to be used]
An interleaved array is an array. Therefore, the starting element is of the reference type and is initialized to null.
The index of an array starts from zero: The index of an array with n elements ranges from 0 to n-1. [This newbie often makes mistakes]

 

Class testarraysclass

{

Static void main ()

{

// Declare a single-dimen=aray

Int [] array1 = new int [5];

// Declare and set array element values

Int [] array2 = new int [] {1, 2, 3, 4, 5 };

// Alternative syntax

Int [] array3 = {1, 2, 3, 4, 5 };

// Declare a two dimen1_array

Int [,] multidimensionalarray1 = new int [2, 3];

// Declare and set array element values

Int [,] multidimensionalarray2 = {1, 2, 3}, {2, 3 }};

// Declare a jarged array

Int [] [] jaggedarray = new int [6] [];

// Set the values of the first array in the jarged array structure

Jargedarray [0] = new int [4] {1, 2, 3, 4 };

}

}

Some. net Common Array Operations

Response. write (array. indexof (abc, "3", 1); // search for "3" in the abc array, starting from abc [1]

Response. write (array. lastindexof (abc, "3"); // search for "3" in the abc array.

-------------------------------------------------------------

String [] arrstr = new string [8] {"1", "4", "3", "2", "16", "14", "12 ", "14"}; // arrstr [0] = "1 "... arrstr [7] = "14"

Array. reverse (arrstr); // reverse the arrstr array. In this case, arrstr [0] = "14"... arrstr [7] = "1"

Array. sort (arrstr); // sort the array. The order is, or (because it is sorted by string)

-------------------------------------------------------------

To redefine the size of an array, redim (vb) must be used. large arrays are very slow, and elements cannot be inserted in the middle. They cannot be cleared (they can only be set to null or 0)

Arraylist is slower than array but does not need to be redefined. You can use myarrlist. add ("dog") s to conveniently add data.

Arraylist myarrlist = new arraylist (); // you do not need to specify the size of the array. Each element can be of any data type;

Myarrlist. insert (1, "abc"); // insert an element before array [1]

Myarrlist. removeat (1); // delete an array element [1]

Myarrlist. remove ("abc"); // Delete the array elements whose content is "abc" only once. If you want to delete all elements, perform a loop.

-------------------------------------------------------------

Listitem newitem = new listitem (); newitem. text = "a"; newitem. value = "B ";

Mydropdown. items. add (newitem); // use listitem to add an item to the list box.

-------------------------------------------------------------

Hashtable ht = new hashtable (); ht ["1"] = "a"; ht. add ("2", "a"); // hashtable usage

Sortedlist sl = new sortedlist (); sl ["1"] = "a"; sl. add ("2", "a"); // sortedlist usage, which is automatically sorted by key

Foreach (dictionaryentry abc in sl) // You can traverse sortedlist.

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.