.net(数组)

摘要:
for(inti=0;iintArray[i]){Console.WriteLine("次小值为:"+intArray[i+1]);break;}}//--------------------------------------------------------------------------------}}classCat{publicstringname{get;set;}publicstringcolor{get;set;}}}
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;
        }
    }

}

免责声明:文章转载自《.net(数组)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C++学习笔记九顺序容器(二) ForFreeDom 博客园在C#中调用EXE文件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

layui使用layui-excel扩展导出xlsx格式文件

layui-excel扩展导出的文件可用office打开,正常显示;直接用table带的导出功能,导出的文件用office打开显示乱码。--导出表不展示--˃78910layui.config.use(['table','form','laydate','excel'],function(){11varform=layui.form;12vartable=l...

Vue浏览器调试工具VueTools安装以及使用

ue-devtools是一款基于chrome浏览器的插件,用于vue应用的调试,这款vue调试神器可以极大地提高我们的调试效率。vue-devtools使用起来还是比较简单的,上手非常的容易,这里就细讲其使用说明了。安装方法二:这里以chrome浏览器为例:1、打开chrome网上应用店,搜索vue.js注:如果打不开页面需要代理选择第一个,点击添加至chr...

linux系统redhat7.9安装R

1.查看系统信息[root@localhosthome]#cat/etc/redhat-releaseRedHatEnterpriseLinuxServerrelease7.9(Maipo)[root@localhosthome]#lsb_release aLSB版本::core-4.1-amd64:core-4.1-noarch:ccxx-4.1-amd6...

TensorRT在ubuntu18.04的安装

安装TensorRT前需要安装Cuda和cudnn,安装步骤可以参考ubuntu安装cuda和cudnn。...

带EFI支持的GRUB2安装全记录

--引导目录#定义引导目录。默认前缀是/boot/grub2,因此我们可以直接定义/。但是,如果您将其安装在EFI系统上,则可以直接写入EFI的装载点=====2016-02-26===============在新版本的grub2中找不到引导目录参数。特别是,在安装EFI时,需要将其更改为--EFI目录,否则您将找不到EFI目录的错误。grub2-insta...

doxygen使用详解

而doxygen就能把遵守某种格式的注释自动转化为对应的文档。以下以linux下的C++语言为例进行介绍,以下讨论基于doxygen1.3.3。Doxygen的使用步骤非常简单。具体如何写doxygen认识的注释在第3节详细介绍。EXTRACT_ALLNO为NO,只解释有doxygen格式注释的代码;为YES,解析所有代码,即使没有注释。可以有多种方式标识出...