Introduction to common use of arrays in asp.net C #

Source: Internet
Author: User
Tags arrays

Example

The code is as follows: Copy code

Using System;
Class ArrayApp
{
Public static void Main ()
 {
// One-dimensional array usage: calculates the number of odd and even numbers in the array
Console. WriteLine ("one-dimensional array demonstration: number of parity in one-dimensional array ");
Int [] arr1 = new int [] {8, 13, 36, 30, 9, 23, 47, 81 };
Int odd = 0;
Int even = 0;
Foreach (int I in arr1)
  {
If (I % 2 = 0)
Even ++;
Else
Odd ++;
  }
Console. WriteLine ("There are {0} even numbers and {1} odd numbers. ", Even, odd );
// Two-dimensional array usage: matrix of n columns in m rows
Console. WriteLine ("two-dimensional array demonstration: 3 rows and 4 columns matrix ");
Int [,] arr2 = new int }};
For (int I = 0; I <3; I ++)
  {
For (int j = 0; j <4; j ++)
   {
Console. Write (arr2 [I, j] + "\ t ");
   }
Console. WriteLine ();
  }
// Usage of a jagged array: two arrays with different numbers of elements
Console. WriteLine ("demo of a jagged array: two arrays with different lengths ");
Int [] [] arr3 = new int [2] [];
Arr3 [0] = new int [5] {1, 3, 5, 7, 9 };
Arr3 [1] = new int [4] {2, 4, 6, 8 };
// Char [] [] arr3 = new char [] [] {H, e, l, l, o}, {C, s, h, a, r, p }};
For (int I = 0; I <arr3.Length; I ++)
  {
Console. Write ("The {0} array is \ t", I + 1 );
For (int j = 0; j <arr3 [I]. Length; j ++)
   {
Console. Write (arr3 [I] [j] + "\ t ");
   }
Console. WriteLine ();
  }
 }
}

Usage of pass-out values

The array is int [] testArray = {4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9}; the maximum value is 9, the storage locations are 9 and 11.

The code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Diagnostics;

Namespace Ch07Ex01
{
Class Program
    {
Static void Main (string [] args)
        {
Int [] testArray = {4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
Int [] maxValIndices;
Int maxVal = Maxima (testArray, out maxValIndices );
Console. WriteLine ("Maximum value {0} found at element indices :",
MaxVal );
Foreach (int index in maxValIndices)
            {
Console. WriteLine (index );
            }
Console. ReadKey ();
        }

Static int Maxima (int [] integers, out int [] indices)
        {
Debug. WriteLine ("Maximum value search started .");
Indices = new int [1];
Int maxVal = integers [0];
Indices [0] = 0;
Int count = 1;
Debug. WriteLine (string. Format (
"Maximum value initialized to {0}, at element index 0.", maxVal ));
For (int I = 1; I <integers. Length; I ++)
            {
Debug. WriteLine (string. Format ("Now looking at element at index {0 }.",
I ));
If (integers [I]> maxVal)
                {
MaxVal = integers [I];
Count = 1;
Indices = new int [1];
Indices [0] = I;
Debug. WriteLine (string. Format (
"New maximum found. New value is {0}, at element index {1 }.",
MaxVal, I ));
                }
Else
                {
If (integers [I] = maxVal)
                    {
Count ++;
Int [] oldIndices = indices;
Indices = new int [count];
OldIndices. CopyTo (indices, 0 );
Indices [count-1] = I;
Debug. WriteLine (string. Format (
"Duplicate maximum found at element index {0}.", I ));
                    }
                }
            }
Trace. WriteLine (string. Format (
"Maximum value {0} found, with {1} occurrences.", maxVal, count ));
Debug. WriteLine ("Maximum value search completed .");
Return maxVal;
        }
    }
}

Debug. writeline is used to display the debugging result in the output window of the interrupt mode.

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.