asp.net C#中数组常用使用方法介绍

时间:2021-04-16

  数组是一种特殊的数组类型可以存储我种类型的字段了,下本文章整整了C#数组用法,面包括有一维数组,二维数组及锯齿型数组用法,希望对大家有帮助。

  例子

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3571')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3571>

using System;
class ArrayApp
{
public static void Main ( )
{
//一维数组用法:计算数组中奇偶数的个数
Console.WriteLine("一维数组演示:一维数组中的奇偶数个数");
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("共有 {0} 个偶数, {1} 个奇数。", even, odd);
//二维数组用法:m行n列的矩阵
Console.WriteLine("二维数组演示:3行4列的矩阵");
int[,] arr2 = new int[3,4] { {4,2,1,7}, {1,5,4,9}, {1,3,1,4} };
for ( int i = 0; i < 3; i )
{
for ( int j = 0; j < 4; j )
{
Console.Write(arr2[i,j] "\t");
}
Console.WriteLine( );
}
//锯齿型数组用法:元素个数不同的两个数组
Console.WriteLine("锯齿型数组演示:长度不同的两个数组");
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("第{0}个数组是:\t",i 1);
for ( int j = 0; j < arr3[i].Length; j )
{
Console.Write(arr3[i][j] "\t");
}
Console.WriteLine();
}
}
}
</td> </tr> </table>

  out传递值的用法

  数组为 int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 }; 最大值为9,储存位置为9和11。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9989')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9989>

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;
}
}
}
</td> </tr> </table>

  使用debug.writeline,将调试结果显示在中断模式的output窗口中。
</td> </tr> </table>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章