当前位置: 移动技术网 > IT编程>开发语言>c# > C#数组应用分析第1/2页

C#数组应用分析第1/2页

2019年07月18日  | 移动技术网IT编程  | 我要评论
以下内容来自msdn 数组是具有相同数据类型的项的有序集合。要访问数组中的某个项,需要同时使用数组名称及该项与数组起点之间的偏移量。在 c# 中,声明和

在程序循环中初始化 
可以使用此处所示的嵌套循环初始化数组中的所有元素: 

int[,] arr7 = new int[5,4]; 

for(int i=0; i<5; i++) 

for(int j=0; i<4; j++) 

arr7[i,j] = 0; // initialize each element to zero 


system.array 类 
在 .net framework 中,数组是作为 array 类的实例实现的。此类提供了许多有用的方法,如 sort 和 reverse。 

下面的示例演示了使用这些方法是多么的简单。首先,使用 reverse 方法将数组元素反转,然后使用 sort 方法对它们进行排序: 

class arraymethods 

static void main() 

// create a string array of size 5: 
string[] employeenames = new string[5]; 

// read 5 employee names from user: 
system.console.writeline("enter five employee names:"); 
for(int i=0; i<employeenames.length; i++) 

employeenames[i]= system.console.readline(); 


// print the array in original order: 
system.console.writeline("\narray in original order:"); 
foreach(string employeename in employeenames) 

system.console.write("{0} ", employeename); 


// reverse the array: 
system.array.reverse(employeenames); 

// print the array in reverse order: 
system.console.writeline("\n\narray in reverse order:"); 
foreach(string employeename in employeenames) 

system.console.write("{0} ", employeename); 


// sort the array: 
system.array.sort(employeenames); 

// print the array in sorted order: 
system.console.writeline("\n\narray in sorted order:"); 
foreach(string employeename in employeenames) 

system.console.write("{0} ", employeename); 



输出 
enter five employee names: 

luca 

angie 

brian 

kent 

beatriz 


array in original order: 

luca angie brian kent beatriz 


array in reverse order: 

beatriz kent brian angie luca 


array in sorted order: 

angie beatriz brian kent luca
2

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网