能否编写遍历一个不知道维数数组的通用类?

解决方案 »

  1.   


            static void Main(string[] args)
            {
                int[,] arr = { { 1, 2, 3 }, { 2, 3, 4 } };
                EnumerateArray(arr);
            }        static void EnumerateArray(Array array)
            {
                IEnumerator ie = array.GetEnumerator();
                while (ie.MoveNext())
                    Console.WriteLine(ie.Current);
            }
      

  2.   


    string[,]   arr   =   new   string[5,6];
    for   (int   i   =   0   ;   i   <   arr.GetLength(0)   ;   i   ++)
    {
    for   (int   j   =   0   ;   j   <   arr.GetLength(1)   ;   j   ++)
    {
    string   s   =   arr[i,j];
    }
    }
      

  3.   

    多维数组也好,动态数组(List)也好。都是顺序存储的,内部还是一维数组。1楼方法很好。
      

  4.   

    我写了一个 用户名与密码的array,验证指定的用户名与密码.
    代码实现:/// <summary>
    /// 验证用户
    /// </summary>
    /// <param name="_usersPassword">提供验证的用户与密码二维数组</param>
    /// <param name="_userName">要验证的用户名</param>
    /// <param name="_userPassword">要验证的用户密码</param>
    /// <returns>
    /// 0:无法找到该用户!
    /// 1:密码错误!
    /// 2:成功验证!
    /// </returns>
    private int ValidateArray(Array _usersPassword, string _userName, string _userPassword) {
    System.Collections.IEnumerator myIEnumerator = _usersPassword.GetEnumerator();
    int numRank = _usersPassword.Rank;
    // 数组的下标
    int count = 1;
    while (myIEnumerator.MoveNext() & count == 1) {
    // 判断用户名,因为是用户名跟密码的一组对比,所以要控制下下标,
    if (string.Compare(myIEnumerator.Current.ToString(), _userName, true) == 0 & count == 1) {
    count++;
    // 判断用户密码
    // 因为是用户名跟密码的一组对比,所以要控制下下标,
    if (myIEnumerator.MoveNext() & count == numRank) {
    if (string.Compare(myIEnumerator.Current.ToString(), _userPassword, true) == 0) return 2;
    else return 1;
    }
    }
    }
    return 0;
    }现在问题是假如我要验证一个多维数组中,当前维数的下标相等.于是,我每增加一个下标,都要在代码上增加一个if语句,假设我有很多个下标要验证,该如何实现? 或许我的思维有点问题.
    请各位赐教! 谢谢
      

  5.   

    你应该换种思路,改用hashtable存取数据,key里面存usename,value里面存password;
    参照下面的代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                string username="a";
                string password="b";
                System.Collections.Hashtable ht = new System.Collections.Hashtable();
                ht.Add(username, password);
                ValidateArray(ht, "a", "b");
               
            }        /// <summary>
            /// 验证用户
            /// </summary>
            /// <param name="_usersPassword">提供验证的用户与密码二维数组</param>
            /// <param name="_userName">要验证的用户名</param>
            /// <param name="Password">要验证的用户密码</param>
            /// <returns>
            /// 0:无法找到该用户!
            /// 1:密码错误!
            /// 2:成功验证!
            /// </returns>
            private int ValidateArray(System.Collections.Hashtable _usersPassword, string _userName, string Password)
            {
                if (_usersPassword.ContainsKey(_userName) == false)
                {
                    return 0;
                }            if ((string)_usersPassword[_userName] == Password)
                {
                    return 2;
                }
                    else
                {
                    return 1;
                }        }    }
    }
      

  6.   

    存储一组用户名与密码,根本就不应该用多维数组来做,应该抽象出一个名为User的类,具有UserName和Password属性,然后用IList<User>作为集合
      

  7.   

    static void Main(string[] args)
    {
    int[,] arr = { { 1, 2, 3 }, { 2, 3, 4 } };
    EnumerateArray(arr);
    }static void EnumerateArray(Array array)
    {
    foreach(object v in array)
    {
    Console.WriteLine(v);
    }            
    }借一楼的例子,其实和一楼是一回事,但看起来舒服点