C#数组的定义及使用(一)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
//c#数组的使用
class Program
{
static void Main(string[] args)
{
//一位数组
int[] a = new int[30];//定义一个30个元素的数组
int l = a.Length;//获取数组长度
Console.WriteLine(l);
string[] s = { "a", "b", "c" };
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i]);
}
//二维数组
int[,] b = new int[2, 3];//定义两行三列的二维数组
int ans = 0;
Console.WriteLine("二维数组输出");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
b[i, j] = ans;
ans++;
Console.Write("{0} ", b[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("获取数组长度: {0}",b.Length);
Console.WriteLine("获取第0维度的长度: {0}",b.GetLength(0));//获取第0维度的长度 2
Console.WriteLine("获取第1维度的长度: {0}",b.GetLength(1));//获取第1维度的长度 3
//交叉数组 (个人觉得就是动态数组,)
int[][] c = { new int[] { 2, 3 }, new int[] { 4, 5, 6 } };
int[][] d = new int[2][];
d[0] = new int[2];//长度为2的一维数组
d[1] = new int[3];//长度为3的一维数组
//交叉数组的遍历
Console.WriteLine("交叉数组输出");
for (int i = 0; i < c.Length; i++)
{
for (int j = 0; j < c[i].Length; j++)
{
Console.Write("{0} ", c[i][j]);
}
Console.WriteLine();
}
}
}
}
运行结果