Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1723396
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: C/C++

2008-04-13 15:56:51

文件:Array.rar
大小:0KB
下载:下载

using System;
public class BasicArray
{
    //数组的创建及基本使用方法

    public static void BasicOp()
    {
        int[] Number = new int[10];
        for (int i = 0; i < Number.Length; ++i)
        {
            Number[i] = i;
            Console.WriteLine(Number[i]);
        }
    }
    //使用foreach遍历数组

    public static void ForeachOp()
    {
        int[] Number = new int[10];
        for (int i = 0; i < Number.Length; ++i)
        {
            Number[i] = i;
        }
        
        foreach (int i in Number)
            Console.WriteLine(i);
    }
    //使用变长数组

    public static void ParamArray()
    {
        int [] Number = new int [10];
        for (int i = 0; i < Number.Length; ++i)
        {
            Number[i] = i;
        }
        PassVarArray(Number);
    }
    //定义变长数组操作函数

    public static void PassVarArray(params int[] intValues)
    {
        foreach (int i in intValues)
            Console.WriteLine(i);
    }
    //多维数组

    public static void RectArrayOp()
    {
        //Length得到整个数组的长度

        const int row = 10;
        const int col = 10;
        int[,] Table = new int[row, col];
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
                Table[i,j] = i * row + j;
        }
        //使用foreach访问数组中的每个值

        foreach (int i in Table)
            Console.WriteLine(i);
    }
    //创建Jagged数组

    public static void JaggedArrayOp()
    {
        const int row = 10;
        const int col = 10;
        int [][] Table = new int[row][];
        for (int i = 0; i < row; i++)
        {
            Table[i] = new int[i];
            for (int j = 0; j < Table[i].Length; j++)
                Table[i][j] = i * row + j;
        }
        //使用foreach访问数组中的每个值

        foreach (int[] ele in Table)
            foreach(int i in ele)
                Console.WriteLine(i*i);
    }

    //使用系统数组定义的方法

    public static void SystemArrayOp()
    {
        const int MAX_LENGTH = 10;
        Random rd = new Random();
        int[] Number = new int[MAX_LENGTH];
        for (int i = 0; i < MAX_LENGTH; i++)
        {
            Number[i] = (rd.Next() % 10);
        }
        foreach (int i in Number)
            Console.Write("{0} ", i);
        Console.WriteLine();
        Array.Sort(Number);
        foreach (int i in Number)
            Console.Write("{0} ", i);
        Console.WriteLine();
        Array.Reverse(Number);
        foreach (int i in Number)
            Console.Write("{0} ", i);
        Console.WriteLine();
    }
}

public class CallBasicArray
{
    public static void test()
    {
        BasicArray.BasicOp();
        BasicArray.ForeachOp();
        BasicArray.ParamArray();
        BasicArray.RectArrayOp();
        BasicArray.JaggedArrayOp();
        Console.Clear();
        BasicArray.SystemArrayOp();
    }
}

阅读(1957) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~