将数组添加到排序数组中 [英] add an array into a sorted array

查看:59
本文介绍了将数组添加到排序数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将int数组添加到已排序数组中,以保持排序状态.在这里
是我的代码,但不起作用:

Hi there, I want to add an int array into a sorted array in a way to remain sorted. here
is my code, but it doesn''t work:

namespace Sort_Int_Array
{
    class Sort
    {
        public int[] Firstarray;
        public int[] Secondarray;
        public Sort(int[] firstarray, int [] secondarray)
        {
           Firstarray = firstarray;
            Secondarray= secondarray;
        }
        public Sort(int[] firstarray)
        {
            Firstarray = firstarray;
        }
        public string Convert_to_String()
        {
            string s = "";
            for (int i = 0; i < Firstarray.Length; i++)
            {
                s += "," + Firstarray[i].ToString();
            }
            return s;
        }

      
        public void Array_Set_Value()
        {
            int N=0;
            int [] finalarray = new int [Firstarray.Length + Secondarray.Length];
            for (int i = 0; i < Firstarray.Length; i++)
            {
                for (int j=0; j<Secondarray.Length; j++)
                {
                    if (Firstarray[i]< Secondarray[j])
                    {
                        N=i+1;
                        Firstarray.CopyTo(finalarray, 0);
                        finalarray.SetValue(Secondarray[i], N);
                    }
                }
            }
        }
        }
    }

推荐答案

您可以使用list< int>为了这.我认为您正在尝试从2个排序的数组中进行排序.

You can use list<int> for this. I think you are trying to sort from 2 sorted array.

namespace Sort_Int_Array
{
    class Sort
    {
        public List<int> Firstarray = new List<int>();
        public List<int> Secondarray = new List<int>();
        public List<int> finalarray = new List<int>();
        public Sort(List<int> firstarray, List<int> secondarray)
        {
            Firstarray = firstarray;
            Secondarray = secondarray;
        }
        public Sort(List<int> firstarray)
        {
            Firstarray = firstarray;
        }
        public string Convert_to_String()
        {
            string s = "";
            for (int i = 0; i < finalarray.Count; i++)
            {
                s += finalarray[i].ToString()+",";
            }
            return s;
        }


        public void Array_Set_Value()
        {
            while (Firstarray.Count!=0 || Secondarray.Count!=0)
            {
                if (Firstarray.Count != 0 && Secondarray.Count != 0)
                {
                    if (Firstarray[0] <= Secondarray[0])
                    {
                        finalarray.Add(Firstarray[0]);
                        Firstarray.RemoveAt(0);
                    }
                    else
                    {
                        finalarray.Add(Secondarray[0]);
                        Secondarray.RemoveAt(0);
                    }
                }
                else if (Firstarray.Count!=0)
                {
                    finalarray.Add(Firstarray[0]);
                    Firstarray.RemoveAt(0);
                }
                else
                {
                    finalarray.Add(Secondarray[0]);
                    Secondarray.RemoveAt(0);
                }
            }
        }
    }
}

List<int> a = new List<int>();
List<int> b = new List<int>();
a.Add(3);
a.Add(10);
b.Add(1);
b.Add(20);
Sort_Int_Array.Sort so = new Sort_Int_Array.Sort(a,b);
so.Array_Set_Value();
Console.WriteLine(so.Convert_to_String());</int></int></int></int></int></int></int></int></int></int></int></int></int>


首先,您的Sort方法根本不执行排序操作.

其次,您的Array_Set_Value方法执行了它所说的操作.它创建了第三个数组,将第一个数组复制到该数组,然后执行一些非常奇怪的操作.这比完成追加工作所需的代码更多.

为什么不创建一个足以容纳两个数组的数组,将第一个数组复制到目标数组的元素0,然后将Array.第二个数组复制到目标数组元素(长度第一个数组的+ 1).

现在,如果您希望对它进行排序,则只需在其上调用Array.Sort方法即可.
First, your Sort method doesn''t do anything like sorting at all.

Second, your Array_Set_Value method does do what it says. It creates a 3rd array, copies the first array to it, then does something very odd. This is WAY more code than what''s needed to do an Append job.

Why don''t you just create an array that''s big enough to hold both of them, Array.Copy the first array to element 0 of the target array, then Array.Copy the second array to the target array element (Length of first array + 1).

Now, if you want it sorted, all you have to do is call the Array.Sort method on it.


如果您要对finalarray进行排序,请将您的Firstarray和Secondarray复制到finalarray和然后简单ur
finalarray.sort方法.

如果您要创建逗号分隔的字符串,请在末尾添加逗号

If u want to sort a finalarray,copy ur Firstarray and Secondarray into finalarray and then simple ur
finalarray.sort method.

if u are creating comma separated string then add comma at the end

public string Convert_to_String()
       {
           string s = "";
           for (int i = 0; i< Firstarray.Length; i++)
           {
               s +=   Firstarray[i].ToString()+ ",";
           }
           return s;
       }


这篇关于将数组添加到排序数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆