class Program
{
static void Main(string[] args)
{
//第一种声明方式;
int[,] arr = new int[2, 3];//初始化声明2行3列二位数组的长度;
//第二种声明方式;
int[,] arr2 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };//声明数组长度并初始化它具体的值;
//第三种声明方式;
int[,] arr3 = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
//声明二位数组并初始化直接赋值;不用声明具体长度以及实例化,行数可以变;列数不变;列数变,每列都要变;
Console.WriteLine(arr3.Length);//长度是值数组的每一个元素之和=行*列;
Console.WriteLine(arr3[2, 1]);//二位数相应下标从0号位开始;相关取法和一维数组一样;注意写法arr[0,2];
arr3[0, 2] = 888;//可以重新赋值;
Console.WriteLine(arr3[0, 2]);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr3[i, j] + " ");
}
Console.Write("\n");
}
//练
const int Line = 4;//行数
const int List = 5;//列数
int[,] arr = new int[Line, List] { { 1, 2, 3, 0, 8 }, { 4, 5, 6, 0, 8 }, { 7, 8, 9, 0, 8 }, { 1, 2, 3, 0, 8 }, };
for (int i = 0; i < Line; i++)
{
for (int j = 0; j < List; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
//练
int[,] arr1 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr1[i, j] + " ");
}
Console.WriteLine();
}
//练 遍历数组 简易方法//arr.getlength();
int[,] arr = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
for(int i=0;i<arr.GetLength(0);i++){
for (int j = 0; j < arr.GetLength(1);j++ )
{
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();
}
Console.WriteLine();
int js = 0;
foreach(int arr1 in arr){
Console.Write(arr1+" ");
js++;
if(js%3==0){
Console.WriteLine();
}
//}
//练
int[,] arr= new int[3,4]{{1,2,3,4},{5,6,7,8},{9,10,11,12}};
for(int i=0;i<arr.GetLength(0);i++){
for (int j = 0; j < arr.GetLength(1);j++ )
{
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();
}
//arr[2, 3] = 88;
//练习
//1.1 1.2:第一题
//3行3列的数
int[,] arr1=new int[3,3];
int js = 0;
int sum = 0;
for(int i=0;i<arr1.GetLength(0);i++){
for (int j = 0; j < arr1.GetLength(1);j++ )
{
js++;
arr1[i, j] = js;
if(i+j==2||i==j){
sum += arr1[i, j];
}
}
}
foreach(int arra in arr){
Console.Write(arra+" ");
js++;
if(js%3==0){
Console.WriteLine();
}
}
Console.WriteLine("对角线之和为:{0}",sum);
Console.WriteLine();
//第二题
//2.1:找出这个二维数组里最大和最小的数组
int[,] Arr = {
{ 26, 23, 156, 25 },
{ 11, 833, 52, 117 },
{ 114, 33, 542, 17 }
};
int arrMax=Arr[0,0];
int arrMin=Arr[0,0];
for (int i = 0; i < Arr.GetLength(0);i++ )
{
for(int j=0;j<Arr.GetLength(1);j++){
if(arrMax<Arr[i,j]){//判断最大值
arrMax=Arr[i,j];
}
if (arrMin > Arr[i,j])//判断最小值
{
arrMin = Arr[i,j];
}
}
}
Console.WriteLine("最大值为{0}\n最小值为{1}\n", arrMax, arrMin);
//2.2:对这个二维数组进行从小到到的排序显示,如这样{{1,2,3},{4,5,6},{7,8,9}};
//------------二维数组直接排序------------------
for (int i = 0; i < Arr.GetLength(0); i++)
{
for (int j = 0; j < Arr.GetLength(1); j++)
{
for (int k = 0; k < Arr.GetLength(0); k++)
{
for (int q = 0; q < Arr.GetLength(1); q++)
{
if (Arr[i, j] < Arr[k, q])
{
int temp = Arr[i, j];
Arr[i, j] = Arr[k, q];
Arr[k, q] = temp;
}
}
}
}
}
//------------二维数组变成一维数组;再排序转换成二维数组------------------
int[] arrPaixu=new int[Arr.Length];
int js_1 = 0;
for (int i = 0; i < Arr.GetLength(0); i++)
{
for (int j = 0; j < Arr.GetLength(1); j++)
{
arrPaixu[js_1] = Arr[i, j];
js_1++;
}
}
for (int i = 0; i < arrPaixu.Length; i++)
{
for (int j = 0; j < arrPaixu.Length; j++)
{
if (arrPaixu[i] < arrPaixu[j])
{
int temp = arrPaixu[i];
arrPaixu[i] = arrPaixu[j];
arrPaixu[j] = temp;
}
}
}
js_1 = 0;
for (int i = 0; i < Arr.GetLength(0); i++)
{
for (int j = 0; j < Arr.GetLength(1); j++)
{
Arr[i, j]=arrPaixu[js_1];
js_1++;
}
}
int js1 = 0;
foreach (int arr22 in Arr)
{
Console.Write(arr22 + " ");
js1++;
if (js1 % 4 == 0)
{
Console.WriteLine();
}
}
Console.ReadKey();
}
}
unity游戏开发-C#语言基础篇(二维数组)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...