【发布时间】: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