当前位置: 首页 > news >正文

C#数组

一维数组

using System;namespace HelloWorld
{class Program{static void Func(int[] param_ints){for (int i = 0; i < param_ints.Length; i++){param_ints[i] = 9;}}static void Main(string[] args){/*** 1. 数组常见的初始化方式*/int[] ints1 = new int[10];  //new数组后元素会默认初始化为0int[] ints2 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };int[] ints3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };/*** 2. 遍历元素*/for (int i = 0; i < ints1.Length; i++){/*** 输出:* 0 0 0 0 0 0 0 0 0 0 0 */Console.Write("{0} ", ints1[i]);}Console.WriteLine();foreach (int i in ints2){/** 输出:* 1 2 3 4 5 6 7 8 9 10 */Console.Write("{0} ", i);}Console.WriteLine();/*** 3. 数组赋值* * temp_ints是对ints的引用,因此ints1中元素被修改,* temp_ints中元素也会被修改*/int[] temp_ints = ints1;ints1[9] = 100;/*** 输出:* temp_ints[9]: 100, ints1[9]: 100*/Console.WriteLine("temp_ints[9]: {0}, ints1[9]: {1}", temp_ints[9], ints1[9]);/*** 4. 数组作为参数传递给函数* * 本质与"3.数组赋值"相同*/Func(ints3);foreach (int i in ints3){/*** 输出:* 9 9 9 9 9 9 9 9 9 9 */Console.Write("{0} ", i);}Console.WriteLine();}}
}

多维数组

using System;namespace HelloWorld
{class Program{static void Main(string[] args){/** 1. 多维数组的形式*//*二维数组*/int[,] ints1 = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };/*三维数组*/int[,,] ints2 = new int[2,2,2] { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };/*** 多维数组的初始化、赋值、遍历等操作与一维数组类似,在此不多赘述*/}}
}

交错数组

using System;namespace HelloWorld
{class Program{static void Main(string[] args){/*1. 交错数组形式*/int[][] ints1 = new int[3][]{new int[] { 1, 2, 3},new int[] { 4, 5, 6},new int[] { 7, 8, 9}};/*2. 交错数组的遍历*/for (int i = 0; i < ints1.Length; i++){for (int j = 0; j < ints1[i].Length; j++){/*** 输出:* 1 2 3* 4 5 6* 7 8 9*/Console.Write(ints1[i][j] + " ");}Console.WriteLine();}Console.WriteLine();for (int i = 0; i < ints1.Length; i++){foreach (int item in ints1[i]){/*** 输出:* 1 2 3* 4 5 6* 7 8 9*/Console.Write(item + " ");}Console.WriteLine();}/*** 交错数组的赋值与作为函数参数传递等相关操作与一维、多维数组一样,在此不多赘述*//*** 3.交错数组和多维数组的区别* * Ⅰ.交错数组中的数组元素长度可以不一样,而多维数组中每一维的长度必须一样* Ⅱ.交错数组是数组的数组,而多维数组是一个整体*/int[,] ints2 = new int[,]{{ 1, 2, 3},{ 4, 5},    //报错,多维数组中每一维的长度必须一样{ 7, 8, 9}};int[][] ints3 = new int[3][]{new int[] { 1, 2, 3},new int[] { 4, 5},new int[] { 6, 7, 8, 9, 10}};}}
}

参数数组

参考:https://www.cnblogs.com/SeekHit/p/4892564.html

http://www.hskmm.com/?act=detail&tid=30507

相关文章:

  • 251013
  • 简谈误差与不确定度
  • 可怕!我的Nodejs系统因为日志打印了Error 对象就崩溃了 Node.js System Crashed Because of Logging an Error
  • 前言
  • 实践
  • 数据结构字符串和图
  • 字典dict
  • 结婚证识别技术:融合计算机视觉、深度学习与自然语言处理的综合性AI能力的体现
  • 上下文丢失
  • 数据结构序列
  • 上下文学习(In-context Learning, ICL)
  • 混淆矩阵
  • 提示词工程实践指南:从调参到对话的范式转变
  • Multi-Head Attention机制
  • 泛化能力
  • JVM引入
  • shiro 架构
  • test9 - post
  • 高级语言程序设计第一次作业
  • Python-weakref技术指南
  • 第二次
  • 从众多知识汲取一星半点也能受益匪浅【day11(2025.10.13)】
  • 王爽《汇编语言》第四章 笔记
  • 10.13总结
  • MySql安装中的问题
  • 题解:AT_agc050_b [AGC050B] Three Coins
  • go:generate 指令
  • 光栅化
  • 图形学中的变换
  • Unity URP 体积云