C#基础知识5:数组及应用

Author:hiyo585

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//数组的应用
namespace ArrayDemo2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 3, 1, 44, 20, 5, 6, 0, 90, 1, 4, 92 };
            for(int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            //Console.WriteLine(arr);
            Console.WriteLine();
            //
            //
            //数组的排序Array.Sort(arr)
            Array.Sort(arr);//对数组排序
            for(int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " " );
            }
            Console.WriteLine();

            //
            //
            //数组的反转Array.Reverse(arr)
            Array.Reverse(arr);
            for( int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            //数组的合并
            //将两个一维数组合并成为一个一维数组或者一个二维数组
            int[] brr = new int[] { 2, 3, 4, 9, 10, 33, 21, 8 };
            int newLong = arr.Length+brr.Length;
            int[] newArray = new int[newLong];
            //将两个一维数组合并成为一个一维数组
            for(int j = 0; j < newLong; j++)
            {
                if (j < arr.Length)
                {
                    newArray[j] = arr[j];
                } else if (j >= arr.Length)
                {
                    newArray[j] = brr[j-arr.Length];
                } 
            }
            Console.WriteLine(); 
            for(int m=0;m<brr.Length; m++)
            {
                Console.Write(brr[m] + " ");
            }
            Console.WriteLine();
            foreach(int k in newArray)
            {
                Console.Write(k + " ");
            }
            int maxLength = 0;
            if (arr.Length > brr.Length)
            {
                maxLength = arr.Length;
            }
            else
                maxLength = brr.Length;
            int[,] newArrayII = new int[2,maxLength];
            for(int n1=0; n1 < 2; n1++)
            {
                for (int n2 = 0; n2 < maxLength; n2++)
                {
                    if (n1 == 0 && n2 < arr.Length)
                    {
                        newArrayII[n1, n2] = arr[n2];
                    }
                    //else if (n1 == 0 && n2 >= arr.Length)
                    //{
                    //    newArrayII[n1, n2] = 0;
                    //}
                    else if (n1 == 1 && n2 < brr.Length)
                    {
                        newArrayII[n1,n2] = brr[n2];
                    } //else if(n1 ==1 && n2 >= brr.Length)
                    //{
                    //    newArrayII[n1, n2] = 0;
                    //}
                }
            }
            Console.WriteLine();
            for(int k1 =0; k1 < 2; k1++)
            {
                for(int k2=0; k2 < maxLength; k2++)
                {
                    Console.Write("{0:3}", newArrayII[k1,k2] + " ");
                }
                Console.WriteLine();
            }

            //注意二位数组的定义方法

            //合并两个一维数组成为一个二维数组的方法,
            //另外一个方法
            int[] arr1 = new int[] { 3, 3, 3, 3, 3, 33, 3 };
            int[] arr2 = new int[] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
            //定义一个新的二位数组
            int long1 = arr1.Length; 
            int long2 = arr2.Length;
            int long3 = 0;
            if (long1 > long2)
                long3 = long1;
            else
                long3 = long2;
            int[,] arr3 = new int[2, long3];
            for(int m1 = 0; m1 < arr3.Rank; m1++)
            {
                switch (m1)
                {
                    case 0:
                        {
                            for(int m2 = 0; m2 < arr1.Length; m2++)
                            {
                                arr3[m1,m2] = arr1[m2];
                            }
                            break; 
                        }
                    case 1:
                        {
                            for(int m3 = 0; m3 < arr2.Length; m3++)
                            {
                                arr3[m1,m3] = arr2[m3];
                            }
                            break;
                        }
                }
            }
            Console.WriteLine();
            Console.WriteLine("合并后的二维数组:");
            for(int q1 = 0; q1 < arr3.Rank; q1++)
            //arr3.Rank 意思:获取Array的秩(维数)。 例如,一维数组返回 1,二维数组返回 2,依次类推
            //Array.GetUpperBound(Int32) 方法
            //获取数组中指定维度最后一个元素的索引
            //public int GetUpperBound (int dimension);
            //参数:数组的从零开始的维度,其上限需要确定。GetUpperBound(array.Rank-1)最高维(从0开始)
            //返回值:数组中指定维度最后一个元素的索引,或 -1(如果指定维度为空)。
            {
                for (int q2=0;q2<arr3.GetUpperBound(arr3.Rank-1)+1; q2++)
                   
                {
                    Console.Write(arr3[q1,q2]+ " ");
                }
                Console.WriteLine();
            }
            Console.ReadLine(); 

        }
    }
}
/*
 * 
3 1 44 20 5 6 0 90 1 4 92
0 1 1 3 4 5 6 20 44 90 92
92 90 44 20 6 5 4 3 1 1 0
2 3 4 9 10 33 21 8
92 90 44 20 6 5 4 3 1 1 0 2 3 4 9 10 33 21 8
92 90 44 20 6 5 4 3 1 1 0
2 3 4 9 10 33 21 8 0 0 0

合并后的二维数组:
3 3 3 3 3 33 3 0 0 0 0 0 0 0 0 0
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值