二维数组的简单用法
public class IntArrayDemo
{
public static void Print()
{
for (int i = 0; i<IntArray.Ints.Length; i++)
{
Console.WriteLine(i);
}
}
public static void GetValue()
{
var First = IntArray.GetFirstRow();
foreach (int i in First)
{
Console.WriteLine(i);
}
Console.WriteLine("----------------------");
var Second = IntArray.GetSecondRow();
foreach (int i in Second)
{
Console.WriteLine(i);
}
Console.WriteLine("----------------------");
var Third=IntArray.GetThirdRow();
foreach (int i in Third)
{
Console.WriteLine(i);
}
Console.WriteLine("----------------------");
int[] firstColumn = IntArray.GetFirstColumn();
foreach (int i in firstColumn)
{
Console.WriteLine(i);
}
}
}
public class IntArray
{
//报错MissingMemberException: The lazily-initialized type does not have a public, parameterless constructor.
//可以看出执行顺序
/* private static Lazy<int[]> ints=new Lazy<int[]>();
public static int[] Ints=ints.Value;
static IntArray()
{
Ints = Enumerable.Range(0,9).ToArray();
}
*/
private static Lazy<int[]> ints = new Lazy<int[]>(() => Enumerable.Range(0, 9).ToArray());
public static int[] Ints => ints.Value;
//二维数组
public static int[,] Array2D = new int[3, 4] {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
// 获取第一行数据
public static int[] GetFirstRow()
{
return Enumerable.Range(0, Array2D.GetLength(1))
.Select(col => Array2D[0, col])
.ToArray();
}
// 获取第二行数据
public static int[] GetSecondRow()
{
return Enumerable.Range(0, Array2D.GetLength(1))
.Select(col => Array2D[1, col])
.ToArray();
}
// 获取第三行数据
public static int[] GetThirdRow()
{
return Enumerable.Range(0, Array2D.GetLength(1))
.Select(col => Array2D[2, col])
.ToArray();
}
// 获取第一列数据
public static int[] GetFirstColumn()
{
return Enumerable.Range(0, Array2D.GetLength(0))
.Select(row => Array2D[row, 0])
.ToArray();
}
}
本文作者:孤沉
本文链接:https://www.cnblogs.com/guchen33/p/18473283
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步