0

单击按钮后,我想在 textBox 中显示我的排序数组。我有两个类和一个接口。第一类生成随机数并显示在 textbox1 中,第二类进行冒泡排序,我无法在 texbox 上显示排序数组。

一级:

public class RandomArray     
{
    static readonly Random generator = new Random();
    /// <summary>
    /// here are the random numbers stored
    /// </summary>
    int[] array;
    /// <summary>
    /// store the min, max used to generate the data
    /// </summary>
    readonly int min, max;
    /// <summary>
    /// Constructor only needs how the value limits
    /// </summary>
    /// <param name="min">The minimum value (typical 0)</param>
    /// <param name="max">The maximum value (example 100)</param>
    public RandomArray(int min, int max)
    {
        this.min=min;
        this.max=max;
        this.array=new int[0];
    }
    /// <summary>
    /// Fills the array with random numbers
    /// </summary>
    /// <param name="count">The number of data to generate</param>
    public void Fill(int count)
    {
        this.array=new int[count];
        // fill array with random integers
        for (int i=0; i<array.Length; i++)
        {
            array[i]=generator.Next(min, max);
        }
    }        
    /// <summary>
    /// Copy constructor if needed (optional)
    /// </summary>
    /// <param name="other">A RandomArray to copy the data from</param> 
    public RandomArray(RandomArray other)
    {
        this.min=other.min;
        this.max=other.max;
        this.array=(int[])other.array.Clone();
    }
    /// <summary>
    /// Provide the data
    /// </summary>

    public int[] Array { get { return array; } }
    /// <summary>
    /// Provide the limits used
    /// </summary>

    public int Min { get { return min; } }
    public int Max { get { return max; } }
    /// <summary>
    /// Creates a comma separated list of numbers like <c>[45,32,64,..]</c>
    /// </summary>
    public string ToStringList()
    {
        string[] parts=new string[array.Length];
        for (int i=0; i<parts.Length; i++)
        {
            parts[i]=array[i].ToString();
        }
        return "["+string.Join(",", parts)+"]";
    }
    /// <summary>
    /// Shows only the limits used
    /// </summary>
    public override string ToString()
    {
        return string.Format("RandomArray({0},{1})", min, max);
    }
<!-- language: none-->

这是冒泡排序

class BubbleSort :  ISortAlgorithm<int>
{
   public void Sort (int[] array)     
    {
        int temp;
        for (int j = 1; j <= array.Length - 2; j++)
        {
            for (int i = 0; i <= array.Length - 2; i++)
            {
                if (array[i] > array[i + 1])
                {
                    temp = array[i + 1];
                    array[i + 1] = array[i];
                    array[i] = temp;
                }
            }
        }
    }

排序算法:

interface ISortAlgorithm<T>
{
    void Sort(T[] array);
}

最后一件事是对文本框的请求:

private void button2_Click(object sender, EventArgs e)
{            
    BubbleSort p = new BubbleSort();
    textBox1.Text = p.ToString();
}

如果我点击按钮程序显示MyProgram.BubbleSort

我要覆盖冒泡排序方法吗?或者是什么 ?我几乎尝试了一切。感谢和问候。

4

2 回答 2

4

我想你在那里有点困惑。当然,您想在对数组进行排序后创建一个RandomArray对象并调用其方法。ToString()

RandomArray r = new RandomArray(1, 100);
r.Fill(50);
BubbleSort b = new BubbleSort();
b.Sort(r.Array);
textBox1.Text = r.ToStringList(); //depending on the requirements
于 2013-11-11T13:33:40.513 回答
-1

你需要重写类的ToString方法BubbleSort

还有几件事,

1)您需要将数组保留在BubbleSort类中以按照您的方式使用它

class BubbleSort :  ISortAlgorithm<int>
{
   private int[] myArray;
   public void Sort (int[] array)     
    {
        myArray = array;
        int temp;
        for (int j = 1; j <= array.Length - 2; j++)
        {
            for (int i = 0; i <= array.Length - 2; i++)
            {
                if (array[i] > array[i + 1])
                {
                    temp = array[i + 1];
                    array[i + 1] = array[i];
                    array[i] = temp;
                }
            }

        }

    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        foreach(int i in myArray)
        {
            sb.Append(Convert.ToString(i));
        }
        return sb.ToString();
    }
于 2013-11-11T13:31:31.833 回答