金生随缘 發表於 2025-10-14 03:26:00

C#数组

<h1 id="一维数组">一维数组</h1>
<pre><code class="language-cs">using System;

namespace HelloWorld
{
    class Program
    {
      static void Func(int[] param_ints)
      {
            for (int i = 0; i &lt; param_ints.Length; i++)
            {
                param_ints = 9;
            }
      }

      static void Main(string[] args)
      {
            /**
            * 1. 数组常见的初始化方式
            */
            int[] ints1 = new int;//new数组后元素会默认初始化为0
            int[] 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 &lt; ints1.Length; i++)
            {
                /**
                * 输出:
                * 0 0 0 0 0 0 0 0 0 0 0
                */
                Console.Write("{0} ", ints1);
            }
            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 = 100;

            /**
            * 输出:
            * temp_ints: 100, ints1: 100
            */
            Console.WriteLine("temp_ints: {0}, ints1: {1}", temp_ints, ints1);

            /**
             * 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();

            
      }
    }
}
</code></pre>
<p> </p>
<h1 id="多维数组">多维数组</h1>
<pre><code class="language-cs">using System;

namespace HelloWorld
{
    class Program
    {
      static void Main(string[] args)
      {
            /*
            * 1. 多维数组的形式
            */

            /*二维数组*/
            int[,] ints1 = new int
            {
                { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 }
            };

            /*三维数组*/
            int[,,] ints2 = new int
            {
                {
                  { 1, 2 },
                  { 3, 4 }
                },
                {
                  { 5, 6 },
                  { 7, 8 }
                }
            };

            /**
             * 多维数组的初始化、赋值、遍历等操作与一维数组类似,在此不多赘述
             */
      }
    }
}
</code></pre>
<p> </p>
<h1 id="交错数组">交错数组</h1>
<pre><code class="language-cs">using System;

namespace HelloWorld
{
    class Program
    {
      static void Main(string[] args)
      {
            /*1. 交错数组形式*/
            int[][] ints1 = new int[]
            {
                new int[] { 1, 2, 3},
                new int[] { 4, 5, 6},
                new int[] { 7, 8, 9}
            };

            /*2. 交错数组的遍历*/
            for (int i = 0; i &lt; ints1.Length; i++)
            {
                for (int j = 0; j &lt; ints1.Length; j++)
                {
                  /**
                  * 输出:
                  * 1 2 3
                  * 4 5 6
                  * 7 8 9
                  */
                  Console.Write(ints1 + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();

            for (int i = 0; i &lt; ints1.Length; i++)
            {
                foreach (int item in ints1)
                {
                  /**
                  * 输出:
                  * 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[]
            {
                new int[] { 1, 2, 3},
                new int[] { 4, 5},
                new int[] { 6, 7, 8, 9, 10}
            };
      }
    }
}
</code></pre>
<p> </p>
<h1 id="参数数组">参数数组</h1>
<p>参考:https://www.cnblogs.com/SeekHit/p/4892564.html</p>


</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:Reimual,转载请注明原文链接:https://www.cnblogs.com/Reimual/p/19139857</p><br><br>
来源:https://www.cnblogs.com/Reimual/p/19139857
頁: [1]
查看完整版本: C#数组