一维二维数组的创建与遍历

一、一维数组的创建与遍历
方法一:
类型[ ]变量=new 类型[ ];l例如:string a=new string[ var int]
若使用new关键字,则方括号内必须声明一个int类型的值来表示长度
也可以:string[ ] a=new string[ ]{ }
方法二:
string[ ] a={"a","b","c","d"};
一维数组的遍历:for和foreach
using  System;
namespace  practice3
{
     class   MainClass
    {
         public   static   void  Main( string [] args)
        {
             string [] a =  new   string []{  "huang" "jun" "kai" "h" "j" "k"  };
             for  ( int  i =  0 ; i < a.Length; i++)
            {
                 Console .WriteLine (a[i]);
            }
             foreach  ( string  item  in  a) 
            {
                 Console .WriteLine (item);
            }
        }
    }
}
二、二维数组的创建与遍历
方法一:
string[,] a=new string[5,3]
string[,] a=new string[5,3]{ {"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","i"},{"l","m","n"}};
方法二:
string[, ] a={ {"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","i"},{"l","m","n"}};
二维数组的遍历:for和foreach
知识点: GetUpperBound(int variable>维度数字<);获取指定维度的上限
GetLowerBound( int variable>维度数字<);获取指定维度的下限
using  System;
namespace  practice3
{
     class   MainClass
    {
         public   static   void  Main( string [] args)
        {
             string [,] a =  new   string [,]
            { {  "张三" "男" "18"  }, {  "李四" "女" "19"  }, {  "王五" "男" "18"  } };
             for  ( int  i =  0 ; i < a.GetUpperBound ( 0 )+ 1 ; i++) 
            {
                 for  ( int  j =  0 ; j < a.GetUpperBound ( 1 )+ 1 ; j++) 
                {
                     Console .Write(a[i,j]+ " \t " );
                }
                 Console .WriteLine ();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值