C#数组

本文详细介绍了C#中一维、二维及交错数组的声明、初始化和使用方法,并提供了多个示例,包括数组作为方法参数的应用。
None.gifusing System;
None.gif
None.gif
namespace testArrayApp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//**//**//// <summary>
InBlock.gif 
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//**//**//// <summary>
InBlock.gif  
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  [STAThread]
InBlock.gif  
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//
InBlock.gif   
// TODO: 在此处添加代码以启动应用程序
InBlock.gif   
//
InBlock.gif
InBlock.gif   
//声明一维数组,没有初始化,等于null
InBlock.gif
   int[] intArray1;
InBlock.gif   
//初始化已声明的一维数组
InBlock.gif
   intArray1 = new int[3];
ExpandedSubBlockStart.gifContractedSubBlock.gif   intArray1 
= new int[3]dot.gif{1,2,3};
ExpandedSubBlockStart.gifContractedSubBlock.gif   intArray1 
= new int[]dot.gif{1,2,3};
InBlock.gif   
InBlock.gif
InBlock.gif   
//声明一维数组,同时初始化
ExpandedSubBlockStart.gifContractedSubBlock.gif
   int[] intArray2 = new int[3]dot.gif{1,2,3};
ExpandedSubBlockStart.gifContractedSubBlock.gif   
int[] intArray3 = new int[]dot.gif{4,3,2,1};
ExpandedSubBlockStart.gifContractedSubBlock.gif   
int[] intArray4 = dot.gif{1,2,3,4};
ExpandedSubBlockStart.gifContractedSubBlock.gif   
string[] strArray1 = new string[]dot.gif{"One","Two","Three"};
ExpandedSubBlockStart.gifContractedSubBlock.gif   
string[] strArray2 = dot.gif{"This","is","an","string","Array"};
InBlock.gif
InBlock.gif   
InBlock.gif   
//通过数组索引(下标),对元素访问
InBlock.gif
   if (intArray1[2> intArray2[0])
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//把一维数组作为方法中的参数
InBlock.gif
    Write_1DArray(intArray1);
InBlock.gif    Write_1DArray(strArray2);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
//直接创建一维数组,作为方法参数
ExpandedSubBlockStart.gifContractedSubBlock.gif
   Write_1DArray(new int[]dot.gif{2,3,4,5});
ExpandedSubBlockStart.gifContractedSubBlock.gif   Write_1DArray(
new String[]dot.gif{"Hello","My","Friends"});
InBlock.gif   
InBlock.gif   
//声明二维数组,没有初始化
InBlock.gif
   short[,] sArray1;
InBlock.gif   
//初始化已声明的二维数组
InBlock.gif
   sArray1 = new short[2,2];
ExpandedSubBlockStart.gifContractedSubBlock.gif   sArray1 
= new short[2,2]dot.gif{dot.gif{1,1},dot.gif{2,2}};
ExpandedSubBlockStart.gifContractedSubBlock.gif   sArray1 
= new short[,]dot.gif{dot.gif{1,2,3},dot.gif{4,5,6}};
InBlock.gif   
InBlock.gif   
//声明二维数组,同时初始化
ExpandedSubBlockStart.gifContractedSubBlock.gif
   short[,] sArray2 = new short [1,1]dot.gif{dot.gif{100}};
ExpandedSubBlockStart.gifContractedSubBlock.gif   
short[,] sArray3 = new short [,]dot.gif{dot.gif{1,2},dot.gif{3,4},dot.gif{5,6}};
ExpandedSubBlockStart.gifContractedSubBlock.gif   
short[,] sArray4 = dot.gif{dot.gif{1,1,1},dot.gif{2,2,2}};
InBlock.gif   
//声明三维数组,同时初始化
ExpandedSubBlockStart.gifContractedSubBlock.gif
   byte[,,] bArray1 = dot.gif{dot.gif{dot.gif{1,2},dot.gif{3,4}},dot.gif{dot.gif{5,6},dot.gif{7,8}}};
InBlock.gif
InBlock.gif   
//把二维数组作为方法的参数
InBlock.gif
   Write_2DArray(sArray1);
InBlock.gif   
//直接创建二维数组,作为方法参数
ExpandedSubBlockStart.gifContractedSubBlock.gif
   Write_2DArray(new short[,]dot.gif{dot.gif{1,1,1},dot.gif{2,2,2}});
InBlock.gif   
InBlock.gif   
//声明交错数组,没有初始化
InBlock.gif
   int[][] JagIntArray1;
InBlock.gif   
//初始化已声明的交错数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
   JagIntArray1 = new int [2][] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
new int[]dot.gif{1,2},
ExpandedSubBlockStart.gifContractedSubBlock.gif           
new int[]dot.gif{3,4,5,6}
ExpandedSubBlockEnd.gif          }
;
ExpandedSubBlockStart.gifContractedSubBlock.gif   JagIntArray1 
= new int [][]dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new int[]dot.gif{1,2},
ExpandedSubBlockStart.gifContractedSubBlock.gif             
new int []dot.gif{3,4,5},
InBlock.gif             intArray2 
//使用int[]数组变量
ExpandedSubBlockEnd.gif
            }
;
InBlock.gif   
//声明交错数组,同时初始化
ExpandedSubBlockStart.gifContractedSubBlock.gif
   int[][] JagIntArray2 = dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
new int[]dot.gif{1,1,1},
ExpandedSubBlockStart.gifContractedSubBlock.gif            
new int []dot.gif{2,2},
InBlock.gif            intArray1
ExpandedSubBlockEnd.gif                      }
;
InBlock.gif   
//把交错数组作为方法参数
InBlock.gif
   Write_JagArray(JagIntArray1);
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private static void Write_1DArray(int[] ArrayName)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//一维数组的Length属性就是元素个数
InBlock.gif
   for (int i=0;i<ArrayName.Length ;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif//通过数组名[索引]访问数组元素
InBlock.gif
    Console.Write(ArrayName[i]+" ");
ExpandedSubBlockEnd.gif   }

InBlock.gif   Console.WriteLine ();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
private static void Write_1DArray(string[] ArrayName)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif//一维数组的Length属性就是元素个数
InBlock.gif
   for (int i=0;i<ArrayName.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif//通过"数组名[索引]"访问数据元素
InBlock.gif
    Console.Write(ArrayName[i]+" ");
ExpandedSubBlockEnd.gif   }

InBlock.gif   Console.WriteLine ();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
private static void Write_2DArray(short[,] ArrayName)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif//多维数组使用GetLength方法得到每一维的长度
InBlock.gif
   for (int i=0; i<ArrayName.GetLength ;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Console.Write(
"二维数组第{0}行:",i+1);
InBlock.gif    
for (int j=0;j<ArrayName.GetLength(1);j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif//多维数组通过"数组名[索引,索引..]"访问数据元素
InBlock.gif
     Console.Write (ArrayName[i,j]+" ");
ExpandedSubBlockEnd.gif    }

InBlock.gif    Console.WriteLine ();
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
private static void Write_JagArray(int[][] ArrayName)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif//交错数组的Length属性是包含子数组的个数
InBlock.gif
   for (int i=0;i<ArrayName.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Console.Write(
"交错数组第{0}个子数组:",i+1);
InBlock.gif    
//二维交错数组的子数组是一维数组,使用Length属性得到元素数
InBlock.gif
    for (int j=0;j<ArrayName[i].Length ;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif//交错数组通过"数组名[索引][索引]"访问数据元素
InBlock.gif
     Console.Write (ArrayName[i][j]+" ");
ExpandedSubBlockEnd.gif    }

InBlock.gif    Console.WriteLine();
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值