.net(数组)

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

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            // 数组定义的三个方式
            int[] array1 = { 1, 2, 3, 4, 5 };
            int[] array2 = new int[5];
            int[] array3 = new int[] { 1, 2, 3, 4, 5 };

            // 定义一个类的数组
            Cat[] cats = new Cat[3];

            // 用for循环遍历
            for (int i = 0; i < array1.Length; i++)
            {
                Console.WriteLine(array1[i]);
            }

            // 用foreach循环遍历
            foreach (int i in array3)
            {
                Console.WriteLine(i);
            }

            // 定义一个二维数组, 并进行初始化
            int[,] binArray = { { 1, 2 }, { 3, 4 }, { 6, 7 }, { 9, 0 } };

            // 对二维数组进行遍历操作
            for (int i = 0; i < binArray.GetLength(0); i++)    //binArray.GetLength(0):得到有几组
            {
                for (int j = 0; j < binArray.GetLength(1); j++)//binArray.GetLength(0):得到列值
                {
                    Console.Write(binArray[i, j] + " ");
                }
                Console.WriteLine();
            }
            //--------------------------------------------------------------------------------
            //    下面实现一个简单的功能的小程序:
            //       功能:对给定的一个整数数组找到它的次小元素
            //       注:次小值并不是该数组排好序的第二个值
             
             
             

            int[] intArray = { 12, 23, 21, 12, 12, 23, 123, 64, 12 };

            // 1,原始数值排序:
            Console.WriteLine("原始数值排序:---");
            for (int i = 0; i < intArray.Length; i++)
            {
                Console.Write(intArray[i] + " ");
            }

            // 2,将原数组从小到大排序
            for (int i = 0; i < intArray.Length; i++)
            {
                for (int j = 0; j < intArray.Length - 1 - i; j++)
                {
                    if (intArray[j] > intArray[j + 1])
                    {
                        int temp;
                        temp = intArray[j];
                        intArray[j] = intArray[j + 1];
                        intArray[j + 1] = temp;
                    }
                }
            }
            Console.WriteLine();

            // 3,冒泡排序后数值排序
            Console.WriteLine("冒泡排序后数值排序:---");
            for (int i = 0; i < intArray.Length; i++)
            {
                Console.Write(intArray[i] + " ");
            }
            Console.WriteLine();

            // 4,次小值为第二个元素 //不一定为
            Console.WriteLine();

            // 5,解决 4 的问题。
            for (int i = 0; i < intArray.Length; i++)
            {
                if (intArray[i + 1] > intArray[i])
                {
                    Console.WriteLine("次小值为: " + intArray[i + 1]);
                    break;
                }
            }

            //--------------------------------------------------------------------------------
        }
    }

    class Cat
    {
        public string name
        {
            get;
            set;
        }
        public string color
        {
            get;
            set;
        }
    }

}

原文地址:https://www.cnblogs.com/wjchang/p/3671594.html