c# 一维数组和二维数组的定义几种方式

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             //一维数组定义与初始化  
  13.             int[] one1 = new int[] {3,2,1 };//第一种方式  
  14.             int[] one2 = { 3, 2, 1 };      //第二种方式  
  15.             int[] one3;                    //第三种方式  
  16.             one3=new int[]{3,2,1};  
  17.   
  18.             
  19.             //二维数组定义与初始化  
  20.   
  21.             //不规则二维数组  
  22.             int[][] array = new int[2][];  
  23.             array[0] = new int[3];  
  24.             array[0][1] = 11;  
  25.             array[0][2] = 12;  
  26.             array[1] = new int[] { 1, 2, 3, 4,5 };  
  27.   
  28.             //或int[][] array = new int[2][] { new int[] {0,11,12 }, new int[] {1,2,3,4,5 }};  
  29.             
  30.             Console.WriteLine( "不规则二维数组:  ");  
  31.             for (int i = 0; i < array.Length; i++)  
  32.             {  
  33.                 foreach (int j in array[i])  
  34.                 {  
  35.                     Console.Write(j+"   ");  
  36.                 }  
  37.                 Console.WriteLine();  
  38.             }  
  39.   
  40.             //固定长度的矩阵数组  
  41.             int[,] arrray1=new int[2,5]{{9,9,9,9,0},{0,0,9,0,0}};  
  42.             Console.WriteLine("规则二维数组输出方法一:  ");  
  43.             for (int ii = 0; ii < 2; ii++)                        //输出方法一  
  44.             {  
  45.                 for (int j = 0; j < 5; j++)  
  46.                 {  
  47.                     Console.Write(arrray1[ii,j] + "   ");  
  48.                 }  
  49.                 Console.WriteLine();  
  50.             }  
  51.             Console.WriteLine("规则二维数组输出方法二:  ");  
  52.             foreach (int j in arrray1)//arrray.length=10;      //输出方法二  
  53.             {  
  54.                 Console.Write(j + "   ");  
  55.             }  
  56.             Console.WriteLine();  
  57.             Console.ReadLine();  
  58.         }  
  59.     }  
  60. }  
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值