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();
}
}
}
}
查看全文
相关阅读:
思达BI软件Style Intelligence实例教程—股票K线图
思达报表工具Style Report基础教程—参数化查询
思达报表工具Style Report基础教程—公式表
思达报表工具Style Report基础教程—交叉表
思达报表工具Style Report基础教程—分组表
思达报表工具Style Report基础教程—创建一个带条件的查询
思达报表工具Style Report基础教程—查询
思达报表工具Style Report基础教程—参数表单
.net 详解异步编程
使用NPOI导入导出Excel
原文地址:https://www.cnblogs.com/msn/p/534461.html
最新文章
配置jboss为windows服务
repository test has failed 错误
Maven+Spirng+Mybatis+CXF搭建WebService服务
如何将maven的jar项目简单快速的转变成war项目
eclipse开启时报错问题
原生js创建模态框(摘自:东窗凝残月 链接:https://www.cnblogs.com/dcncy/p/9076937.html)
关于自己电脑各项软件规范问题
分页查询语句
java用注解实现redis缓存
windows下安装mysql8.0.12
热门文章
vue环境搭建
windows下安装nodejs
idea使用
win10 远程桌面时报错:出现身份验证错误。要求的函数不受支持
IDEA Error:java: Compilation failed: internal java compiler error
Spring事务
nginx使用记录
设计模式-05.代理模式
思达BI软件Style Intelligence实例教程—柱状数据对比分析图
思达BI软件Style Intelligence实例教程—帕累托图
Copyright © 2011-2022 走看看