【问题标题】:C#, Creating an Array in an Object Class and executing the method, custom sorting methodC#,在对象类中创建一个数组并执行该方法
【发布时间】:2022-01-21 02:28:26
【问题描述】:

C#,我正在尝试在类中创建一个数组作为对象。我希望这个程序在我进行冒泡排序时运行该方法。我需要了解如何将已从文本创建的十进制数组中的值传递给该对象?我在某个地方出了点问题。当我在表单前端的另一侧打印出数组时,我得到的只是表单的名称。

使用类调用的主要形式:Sort sort = new Sort(rawArray);

using System;


namespace BbblSrtProj
{
    
    public class Sort
    {

        private decimal[] theArray;
        public Sort() { }
        public Sort (decimal[] sort)
        {
            this.theArray = sort;
            
        }
        public decimal[] TheArray
        {
            get
            {
                return theArray;
            }
            set 
            {
                theArray = value;
            }
        }

        //Sort Method: Bubble Sort
        public Array SortingMethod()
        {
            for (int i = 0; i <= TheArray.Length - 1; i++)
            {
                // Temp int variable to hold value in
                decimal temp;

                // Swap out adjacent value by order,
                // till completed.
                for (int j = 0; j < TheArray.Length - 1; j++)
                {
                    if (TheArray[j] > TheArray[j + 1])
                    {
                        temp = TheArray[j + 1];
                        TheArray[j + 1] = TheArray[j];
                        TheArray[j] = temp;
                    }
                }
            }

            return TheArray;
        
        }
    }
}

【问题讨论】:

  • 您告诉我们您使用什么来创建 Sort 实例 (Sort sort = new Sort(rawArray);),但调用代码中的 print 语句是什么?

标签: c# arrays object bubble-sort


【解决方案1】:
// Sort.cs 
//This Got it working

using System;
using System.Collections.Generic;
using System.Text;

    namespace SortItProj
    {
    
        public class Sort
        {
            private decimal[] theArray;
    
            public Sort(decimal[] sort)
            {
                this.theArray = sort;
            }
    
            public decimal[] TheArray
            {
                get
                {
                    return theArray;
                }
                set
                {
                    theArray = value;
                }
        }

        public Array SortingMethod()
        {
            for (int i = 0; i <= TheArray.Length - 1; i++)
            {
                // Temp int variable to hold value in
                decimal temp;

                // Swap out adjacent value by order,
                // till completed.
                for (int j = 0; j < TheArray.Length - 1; j++)
                {
                    if (TheArray[j] > TheArray[j + 1])
                    {
                        temp = TheArray[j + 1];
                        TheArray[j + 1] = TheArray[j];
                        TheArray[j] = temp;
                    }
                }
            }    
            return TheArray;    
        }
    }

}

// SortItProj.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SortItProj
{
    internal class Program
    {
        static void Main()
        {
            Random random = new Random();
            decimal[] theArray = new decimal[55];

            int theArrayLength = theArray.Length;   
            decimal[] theArray2 = new decimal[theArrayLength];
            
            int i = 0;

            do
            {
                theArray[i] = (random.Next(1, 1000));
                i++;

            } while (i < theArray.Length);

            Array.Copy(theArray, theArray2, theArrayLength);

            Sort sort = new Sort(theArray);

            sort.SortingMethod();

            int j = 0;
            foreach (decimal number in theArray)
            {
                Console.Write(@$"{theArray2[j]} < RAW | SORTED > ");
                Console.WriteLine(number + "\n");
                j++;
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    如我所见,您正在寻找一个就地冒泡排序;如果是您的情况,我建议采用 不同的(通用)设计:

    public class ArraySort {
      // Generic array (not necessary decimal one)
      // Generic way of sorting (we can specify our own comparer)
      public static T[] BubbleSort<T>(this T[] array, IComparer<T> comparer = null) {
        // Input paramers validation
        if (null == array)
          throw new ArgumentNullException(nameof(array));
    
        // If no comparer provided, we try using default one,
        // if no default comparer has been found, we complain 
        if (null == (comparer ??= Comparer<T>.Default))
          throw new ArgumentNullException(nameof(comparer), 
            $"No default comparer for {typeof(T).Name} class");
            
        // Your algorithm a bit refined        
        for (int i = 0; i < array.Length; i++) 
          for (int j = 0; j < array.Length - 1; j++) 
            if (comparer.Compare(array[j], array[j + 1]) > 0) 
              (array[j], array[j + 1]) = (array[j + 1], array[j]); // <- swap
    
        return array;
      }
    }
    

    现在你可以放任何一个

    ArraySort.BubbleSort(array);
    

    或者

    array = ArraySort.BubbleSort(array);
    

    甚至

    array.BubbleSort();
    

    演示:

    decimal[] array = ArraySort.BubbleSort(new decimal[] { 3, 5, 2, 1, 4});
    
    Console.Write(string.Join(", ", array));
    

    结果:

    1, 2, 3, 4, 5
    

    【讨论】:

    • Woooo-hoooo,完成了,代码看起来很紧凑。
    • 他就是我把它绑在前面的方式。
    • 我在应用程序的前端将它绑定到类:decimal[] array = Sort.BubbleSort(rawArray);
    • @gu3:请注意,decimal[] array = Sort.BubbleSort(rawArray); arrayrawArray 将是 相同的(并且都已排序)
    【解决方案3】:

    您需要一个具有十进制数组作为属性的类和一个为您的作业排序数组的方法?这是我对你写的最好的猜测......如果是这样的话:

     public class SortingClass
    {
        public decimal[] TheArray { get; set; }
    
        public void Sort()
        {
            // Do the sort
            
        }
    }
    

    然后你设置数组并调用方法:

    var sortingClass = new SortingClass();
    sortingClass.TheArray = YourArray;
    sortingClass.Sort();
    

    但是你确定这是作业要你做的吗?因为我没有看到类中数组属性的任何原因。无论如何,如果你希望人们能够帮助你,你应该分享更多关于你出错的细节。

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 2019-07-29
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 2012-08-08
      • 1970-01-01
      相关资源
      最近更新 更多