[Collection] array tips

Source: Internet
Author: User
It was recorded that a lot of time was wasted for a small problem. Just like yesterday, it took nearly half an hour to tell a colleague that the lower limit is not a zero-dimensional array. Haha ~ Using system;
Using system. Collections. Generic;
Using system. text;

Namespace dotnetframework
{
Class Program
{
Static void main (string [] ARGs)
{
//-------------------------------------------------------------
// Create an array whose lower limit is not 0
// Defines the lower limit of the first and second dimensions of the array.
Int32 [] lowerbounds ={ 1995, 1 };
// Defines the length of the first and second parts of the array.
Int32 [] lengths = {12, 4 };
// Create an array
Decimal [,] Quarter = (decimal [,]) array. createinstance (typeof (decimal), lengths, lowerbounds );

// Access the Array
// Obtain the lower limit of the array
Int32 firstyear = quarter. getlowerbound (0 );
// Obtain the upper limit of the array
Int32 lastyear = quarter. getupperbound (0 );

For (int32 year = firstyear; year <= lastyear; year ++)
{
For (int32 quar = quarter. getlowerbound (1); quar <= quarter. getupperbound (1); quar ++)
{
Console. writeline (Year. tostring () + "" + quar. tostring ());
}
}
//-------------------------------------------------------------
// Some methods in. net2.0 are from msdn
Int32 [] arrtemp = {0, 1, 2, 3, 3, 3, 3, 7, 8, 9 };

// Determine whether the elements in the specified array match the conditions defined by the specified predicate.
Console. writeline (array. exists <int32> (arrtemp, validate ));
// The statement below is the same as above
Console. writeline (array. exists (arrtemp, validate ));

// Search for elements that match the conditions defined by the specified predicate and return the first matching item in the entire array.
Console. writeline (array. Find <int32> (arrtemp, validate ));
// The statement below is the same as above
Console. writeline (array. Find (arrtemp, validate ));

// Retrieve all elements that match the conditions defined by the specified predicate
Int32 [] arrsub = array. findall <int32> (arrtemp, validate );
// The statement below is the same as above
Int32 [] arrsub1 = array. findall (arrtemp, validate );
// And so on. See msdn
//-------------------------------------------------------------
// Re-adjust the array Length

Arrtemp = (int32 []) redim (arrtemp, 11 );

Arrtemp [10] = 100;

// Traverse the array and process each element of the array with action
Array. foreach <int32> (arrtemp, action );
// The statement below is the same as above
Array. foreach (arrtemp, action );

Console. Read ();
}

/// <Summary>
/// Re-adjust the array Length
/// </Summary>
/// <Param name = "oldarray"> target array </param>
/// <Param name = "newsize"> New length </param>
/// <Returns> A new array with a new length </returns>
/// <Remarks> Of course, it is necessary to perform a verification :) description </remarks>
Static array redim (array oldarray, int32 newsize)
{
// Obtain the array element type
Type T = oldarray. GetType (). getelementtype ();
// Create an array with a new length and its type matches the original array
Array newarray = array. createinstance (T, newsize );
// Copy data
Array. Copy (oldarray, 0, newarray, 0, math. Min (oldarray. length, newsize ));
Return newarray;
}

/// <Summary>
/// Generic delegation Method
/// </Summary>
/// <Param name = "I"> </param>
Static void action (int32 I)
{
Console. writeline (I );
}

/// <Summary>
/// Generic delegation Method
/// </Summary>
/// <Param name = "I"> </param>
/// <Returns> </returns>
Static bool validate (int32 I)
{
If (int32.equals (3, I ))
{
Return true;
}

Return false;
}
}
}

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.