我现在有两个数组A{2,1,5,4,3}和B{a,b,c,d,e},两个数组元素是一一对应的,当数组A排序后变为{1,2,3,4,5},数组B也变为{b,a,e,d,c}.也就是说,当一个数组改变顺序时,另一个对应的数组也跟着变化!
请问如何实现类似的关联???

解决方案 »

  1.   

    在排序A的时候根据A的元素的索引来设置B中的元素就行了啊
    也就是A的排序和B的排序同步进行
      

  2.   

    加入一个中间变量hash表
    hashtable hash = new HashTable();
    hash.Add("2", "a");
    ..........
    //排序
    arr2[i] = hash[arr1[i]]
      

  3.   

    楼上的方案有个严重的问题,hashtable的key是不可重复的
      

  4.   

    3楼的可以试试,如果不行可以试一下用enum
      

  5.   

    这不是普通冒泡排序稍微改下就可以了么namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            int[] A = new int[] { 2, 1, 5, 4, 3 };
                Char[] B = new Char[] { 'a', 'b', 'c', 'd', 'e' };            for (int i = 0; i < A.Length - 1; i++)
                    for (int j = i + 1; j < B.Length; j++)
                        if (A[i] > A[j])
                        {
                            int TA = A[i];
                            A[i] = A[j];
                            A[j] = TA;
                            Char TB = B[i];
                            B[i] = B[j];
                            B[j] = TB;
                        }
                String S = "{";
                foreach (int a in A)
                    S += a.ToString() + " ";
                S += "} {";
                foreach (Char b in B)
                    S += b.ToString() + " ";
                S += "}";
                MessageBox.Show(S);
            }
        }
    }
      

  6.   

    输出结果:{1 2 3 4 5 } {b a e d c }
      

  7.   

    tryint[] A = new int[]{2, 1, 5, 4, 3};
    string[] B = new string[] {"a","b","c","d","e"};
    Array.Sort(A, B);
    for (int i = 0; i < A.Length; i++)
    {
        Console.WriteLine("{0} --> {1}", A[i], B[i]);
    }
    Console.ReadLine();
      

  8.   

    输出:
    1 --> b
    2 --> a
    3 --> e
    4 --> d
    5 --> c下标是否越界等细节问题楼主自己考虑
      

  9.   

    int a=2;
    b=1,c=5,d=4,e=3;
     然后排序
     就搞定.
     
      

  10.   


                int[] a = { 2, 1, 5, 4, 3 };
                string[] b = { "a", "b", "c", "d", "e" };
                Dictionary<string, int> dic = new Dictionary<string, int>();
                for (int i = 0; i < a.Length; i++)
                    dic.Add(b[i], a[i]);//将a与b的关系存入dic
                Array.Sort(a);//将a排序
                Array.Sort(b, new Comparison<string>(delegate(string first, string second)//对b排序
                    {
                        int indexa = Array.IndexOf(a, dic[first]);
                        int indexb = Array.IndexOf(a, dic[second]);
                        return indexa - indexb;
                    }));
                foreach (int i in a)
                    Console.Write(i + " ");
                Console.WriteLine();
                foreach (string s in b)
                    Console.Write(s + " ");
    /*
    输出结果:
    1 2 3 4 5
    b a e d c
    */
      

  11.   

    对数组B不能做任何操作,只能操作数组A,数组B要根据初始对应关系跟着变@!
      

  12.   

    其实原理很简单,无非就是记住数组a在排序前后的下标吧。
    那就建一个辅助数组,里面记录a在排序后的下标。
    访问b的时候,按照辅助数组里面指定的下标访问就好了。
    最近语无伦次,语言表达能力极差。还是看代码吧。 
    仿照8楼的方法。            int[] a = new int[] { 3, 2, 1, 4, 5 };
                string[] b = new string[] { "c", "b", "a", "d", "e" };            int[] indecies = new int[a.Length];
                for (int i = 0; i != indecies.Length; i++) {
                    indecies[i] = i;
                }
                Array.Sort(a, indecies);