zoukankan
html css js c++ java
C# 数组 Array
using
System;
namespace
testArrayApp
{
/**/
///
<summary>
///
Class1 的摘要说明。
///
</summary>
class
Class1
{
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
static
void
Main(
string
[] args)
{
//
//
TODO: 在此处添加代码以启动应用程序
//
//
声明一维数组,没有初始化,等于null
int
[] intArray1;
//
初始化已声明的一维数组
intArray1
=
new
int
[
3
];
intArray1
=
new
int
[
3
]
{
1
,
2
,
3
}
;
intArray1
=
new
int
[]
{
1
,
2
,
3
}
;
//
声明一维数组,同时初始化
int
[] intArray2
=
new
int
[
3
]
{
1
,
2
,
3
}
;
int
[] intArray3
=
new
int
[]
{
4
,
3
,
2
,
1
}
;
int
[] intArray4
=
{
1
,
2
,
3
,
4
}
;
string
[] strArray1
=
new
string
[]
{
"
One
"
,
"
Two
"
,
"
Three
"
}
;
string
[] strArray2
=
{
"
This
"
,
"
is
"
,
"
an
"
,
"
string
"
,
"
Array
"
}
;
//
通过数组索引(下标),对元素访问
if
(intArray1[
2
]
>
intArray2[
0
])
{
//
把一维数组作为方法中的参数
Write_1DArray(intArray1);
Write_1DArray(strArray2);
}
//
直接创建一维数组,作为方法参数
Write_1DArray(
new
int
[]
{
2
,
3
,
4
,
5
}
);
Write_1DArray(
new
String[]
{
"
Hello
"
,
"
My
"
,
"
Friends
"
}
);
//
声明二维数组,没有初始化
short
[,] sArray1;
//
初始化已声明的二维数组
sArray1
=
new
short
[
2
,
2
];
sArray1
=
new
short
[
2
,
2
]
{
{
1
,
1
}
,
{
2
,
2
}
}
;
sArray1
=
new
short
[,]
{
{
1
,
2
,
3
}
,
{
4
,
5
,
6
}
}
;
//
声明二维数组,同时初始化
short
[,] sArray2
=
new
short
[
1
,
1
]
{
{
100
}
}
;
short
[,] sArray3
=
new
short
[,]
{
{
1
,
2
}
,
{
3
,
4
}
,
{
5
,
6
}
}
;
short
[,] sArray4
=
{
{
1
,
1
,
1
}
,
{
2
,
2
,
2
}
}
;
//
声明三维数组,同时初始化
byte
[,,] bArray1
=
{
{
{
1
,
2
}
,
{
3
,
4
}
}
,
{
{
5
,
6
}
,
{
7
,
8
}
}
}
;
//
把二维数组作为方法的参数
Write_2DArray(sArray1);
//
直接创建二维数组,作为方法参数
Write_2DArray(
new
short
[,]
{
{
1
,
1
,
1
}
,
{
2
,
2
,
2
}
}
);
//
声明交错数组,没有初始化
int
[][] JagIntArray1;
//
初始化已声明的交错数组
JagIntArray1
=
new
int
[
2
][]
{
new
int
[]
{
1
,
2
}
,
new
int
[]
{
3
,
4
,
5
,
6
}
}
;
JagIntArray1
=
new
int
[][]
{
new
int
[]
{
1
,
2
}
,
new
int
[]
{
3
,
4
,
5
}
,
intArray2
//
使用int[]数组变量
}
;
//
声明交错数组,同时初始化
int
[][] JagIntArray2
=
{
new
int
[]
{
1
,
1
,
1
}
,
new
int
[]
{
2
,
2
}
,
intArray1
}
;
//
把交错数组作为方法参数
Write_JagArray(JagIntArray1);
}
private
static
void
Write_1DArray(
int
[] ArrayName)
{
//
一维数组的Length属性就是元素个数
for
(
int
i
=
0
;i
<
ArrayName.Length ;i
++
)
{
//
通过数组名[索引]访问数组元素
Console.Write(ArrayName[i]
+
"
"
);
}
Console.WriteLine ();
}
private
static
void
Write_1DArray(
string
[] ArrayName)
{
//
一维数组的Length属性就是元素个数
for
(
int
i
=
0
;i
<
ArrayName.Length;i
++
)
{
//
通过"数组名[索引]"访问数据元素
Console.Write(ArrayName[i]
+
"
"
);
}
Console.WriteLine ();
}
private
static
void
Write_2DArray(
short
[,] ArrayName)
{
//
多维数组使用GetLength方法得到每一维的长度
for
(
int
i
=
0
; i
<
ArrayName.GetLength ;i
++
)
{
Console.Write(
"
二维数组第{0}行:
"
,i
+
1
);
for
(
int
j
=
0
;j
<
ArrayName.GetLength(
1
);j
++
)
{
//
多维数组通过"数组名[索引,索引..]"访问数据元素
Console.Write (ArrayName[i,j]
+
"
"
);
}
Console.WriteLine ();
}
}
private
static
void
Write_JagArray(
int
[][] ArrayName)
{
//
交错数组的Length属性是包含子数组的个数
for
(
int
i
=
0
;i
<
ArrayName.Length;i
++
)
{
Console.Write(
"
交错数组第{0}个子数组:
"
,i
+
1
);
//
二维交错数组的子数组是一维数组,使用Length属性得到元素数
for
(
int
j
=
0
;j
<
ArrayName[i].Length ;j
++
)
{
//
交错数组通过"数组名[索引][索引]
"访问数据元素
Console.Write (ArrayName[i][j]
+
"
"
);
}
Console.WriteLine();
}
}
}
}
查看全文
相关阅读:
Euraka适合初学者的简单小demo
springboot中常用的依赖
SpringBoot的入门程序
spring-data-solr查询
SpringBoot整合Redis
SpringBoot整合MyBatis
使用swagger2生成文档
SpringBoot整合Spring Data JPA
SpringBoot构建RESTful API
SpringBoot属性配置
原文地址:https://www.cnblogs.com/msn/p/534461.html
最新文章
vs代码模板制作
团队计划
团队博客介绍
js DOM常见操作
伪类与伪元素的区别
DOM和BOM的区别
css flex布局
css position属性学习
css grid布局
ubuntu16.04在python3 下创建Django项目并运行
热门文章
开始使用博客园,自我介绍
SpringBoot整合Redis
SpringBoot整合MyBatis
Springboot入门程序
SpringBoot基本配置
Spring的SSM标准配置
springDataRedis的小demo
fastDFS的入门程序
实现负载均衡的小demo
作为消费者访问提供者提供的功能(eureka的铺垫案例)
Copyright © 2011-2022 走看看