`
webcode
  • 浏览: 5986752 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

.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;
        }
    }

}

分享到:
评论

相关推荐

    asp.net 数组与序列化

    asp.net 数组与序列化! 很值得下载看看!资源免费,大家分享!!

    浅谈VB.NET数组声明和初始化

    经过长时间学习VB.NET数组声明和VB.NET数组初始化,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西。对数组进行操作的能力在任何编程语言中都很重要。VB.NET与其它语言相似,提供了简单的...

    .net 数组与字符串

    .net中字符串和数组的详细讲解,ppt格式。

    VB.net数组第一节

    数组的概念,声明,使用,初始化,案例学生管理系统的提出

    asp.net 数组中字符串替换的几种方式

    asp.net 数组中字符串替换的几种方式 在 ASP.NET 开发中,数组中字符串替换是非常常见的操作之一。本文将介绍三种不同的字符串替换方式,帮助开发者更好地实现数组中字符串的替换操作。 方法一:使用 ArrayList ...

    删除数组重复元素(VB.NET)

    VB.NET删除数组中的重复元素,包括源码和可执行程序,已在VS2005中测试通过。

    .NET数组使用中的注意事项小结

    本文分析了.NET数组使用中的注意事项。分享给大家供大家参考。具体分析如下: 1.初始值问题 对于int、double、float等一些值类型数组,没有赋值的情况下, 默认值是0; 而对于String 等引用类型,初始值为null。 2....

    ASP.NET数组删除重复值实现代码

    在ASP.NET编程中,要想删除数组的重复值可以使用多种方法代码实现相同的效果。今天,在某个博客中看到某功能代码中的一小段代码很不错,它就是用来移动数组中相同值的方法,分享给大家

    使用数组列表ArrayList填充ListBox

    使用数组列表ArrayList填充ListBox

    【C】数组数组初始化总结

    【C】数组数组初始化总结;参见博客https://blog.csdn.net/u010168781/article/details/80061118

    asp.net 读取序列化后的数组

    asp.net 读取序列化后的数组! 很值得下载看看!资源免费,大家分享!!

    VB.NET二维数组快速排序(更新)

    VB.NET二维数组快速排序(更新) 'OldArrays(),为排序二维数组;NewArrays(),为存放结果数组,SortColumnsOrOrders(),传递排序参数数组,偶数个为排序列号,奇数为升降序,0为升序,1为降序;FieldRow,是否有字段行...

    asp.net 判断数组是否存在某个值的方法

    方法一: 代码如下: string str1 = “0,1,2,3,4,5,6 “; string[] str = str1.Split( ‘, ‘); bool hasFlag=false; foreach (string a in str) { if (a == “7 “) { hasFlag=true; break; } } if(hasFlag) { //...

    vb.net 第五章 数组

    vb.net数组,包括课本上的全部例题原代码及教学课件

    vb 有序数组的合并

    vb.net有序数组合并代码 将两个有序数组合并成一个有序数组

    asp.net c# 数组学习

    数组概述 C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。 声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面...

    数组相减 VB

    数组相减 VB

    asp过滤数组重复数据,函数.asp

    asp过滤数组重复数据,函数.asp asp过滤数组重复数据,函数.asp

    vb.net 控件数组演示

    vb.net 控件数组演示。。。。。。。。。。。。。。

Global site tag (gtag.js) - Google Analytics