无法创建 class 的唯一实例
Trouble creating unique instances of a class
我一直在研究实现不同排序方法的 class。
myTest.bubblesort(sortMe, false)
returns 升序排列的列表。我已经检查过这种行为,它工作正常。 backTest.bubblesort(sortMe, true)
returns 相同列表按降序排列。我已检查此行为是否正确。
我 运行 遇到麻烦的地方是 TestSorting
的 backTest
实例表现得像是对 myTest
实例的引用。当 backTest
对象改变时,它也会修改 myTest
对象。换句话说,它们并不是我预期的唯一实例。有人可以解释为什么吗?
class Program
{
static void Main(string[] args)
{
int[] sortMe = Sorting.GenerateTestArray(10, 100);
TestSorting<int> myTest = new TestSorting<int>();
TestSorting<int> backTest = new TestSorting<int>();
int[] test = myTest.BubbleSort(sortMe, false);
int[] testBack = backTest.BubbleSort(sortMe, true);
}
}
class TestSorting<T> where T : IComparable
{
public T[] BubbleSort(T[] sortMe, bool descending)
{
if (!descending)
return BubbleAscending(sortMe);
else
return BubbleDescending(sortMe);
}
private T[] BubbleAscending(T[] sortMe)
{
bool stopMe = true;
int stopRecurse = sortMe.Length - 1;
int optimizeMe = stopRecurse;
for (int i = 0; i < stopRecurse && stopMe; i++)
{
stopMe = false;
for (int j = 0; j < optimizeMe; j++)
{
if (sortMe[j].CompareTo(sortMe[j + 1]) > 0)
{
Swap(sortMe, j, j + 1);
stopMe = true;
}
}
optimizeMe--;
}
return sortMe;
}
private T[] BubbleDescending(T[] sortMe)
{
bool stopMe = true;
int stopRecurse = sortMe.Length - 1;
int optimizeMe = 0;
for (int i = 0; i < stopRecurse && stopMe; i++)
{
stopMe = false;
for (int j = stopRecurse; j > optimizeMe; j--)
{
if (sortMe[j].CompareTo(sortMe[j - 1]) > 0)
{
Swap(sortMe, j, j - 1);
stopMe = true;
}
}
optimizeMe++;
}
return sortMe;
}
}
但它们都是对 sortMe 的引用
你传给 sortMe 给 BubbleSort
您的 class 不是 return 新数组,它正在修改输入数组并 return 引用它。如果您的输入是一次性的,您可以这样调用您的排序:
int[] test = myTest.BubbleSort(Sorting.GenerateTestArray(10, 100), false);
int[] testBack = backTest.BubbleSort(Sorting.GenerateTestArray(10, 100), true);
如果您从排序 class 中删除 return 会更清楚。这样很明显它是在修改第一个参数,而不是创建一个新数组。例如:
int[] test = Sorting.GenerateTestArray(10, 100);
myTest.BubbleSort(test, false);
int[] testBack = Sorting.GenerateTestArray(10, 100);
backTest.BubbleSort(testBack, true);
我一直在研究实现不同排序方法的 class。
myTest.bubblesort(sortMe, false)
returns 升序排列的列表。我已经检查过这种行为,它工作正常。 backTest.bubblesort(sortMe, true)
returns 相同列表按降序排列。我已检查此行为是否正确。
我 运行 遇到麻烦的地方是 TestSorting
的 backTest
实例表现得像是对 myTest
实例的引用。当 backTest
对象改变时,它也会修改 myTest
对象。换句话说,它们并不是我预期的唯一实例。有人可以解释为什么吗?
class Program
{
static void Main(string[] args)
{
int[] sortMe = Sorting.GenerateTestArray(10, 100);
TestSorting<int> myTest = new TestSorting<int>();
TestSorting<int> backTest = new TestSorting<int>();
int[] test = myTest.BubbleSort(sortMe, false);
int[] testBack = backTest.BubbleSort(sortMe, true);
}
}
class TestSorting<T> where T : IComparable
{
public T[] BubbleSort(T[] sortMe, bool descending)
{
if (!descending)
return BubbleAscending(sortMe);
else
return BubbleDescending(sortMe);
}
private T[] BubbleAscending(T[] sortMe)
{
bool stopMe = true;
int stopRecurse = sortMe.Length - 1;
int optimizeMe = stopRecurse;
for (int i = 0; i < stopRecurse && stopMe; i++)
{
stopMe = false;
for (int j = 0; j < optimizeMe; j++)
{
if (sortMe[j].CompareTo(sortMe[j + 1]) > 0)
{
Swap(sortMe, j, j + 1);
stopMe = true;
}
}
optimizeMe--;
}
return sortMe;
}
private T[] BubbleDescending(T[] sortMe)
{
bool stopMe = true;
int stopRecurse = sortMe.Length - 1;
int optimizeMe = 0;
for (int i = 0; i < stopRecurse && stopMe; i++)
{
stopMe = false;
for (int j = stopRecurse; j > optimizeMe; j--)
{
if (sortMe[j].CompareTo(sortMe[j - 1]) > 0)
{
Swap(sortMe, j, j - 1);
stopMe = true;
}
}
optimizeMe++;
}
return sortMe;
}
}
但它们都是对 sortMe 的引用
你传给 sortMe 给 BubbleSort
您的 class 不是 return 新数组,它正在修改输入数组并 return 引用它。如果您的输入是一次性的,您可以这样调用您的排序:
int[] test = myTest.BubbleSort(Sorting.GenerateTestArray(10, 100), false);
int[] testBack = backTest.BubbleSort(Sorting.GenerateTestArray(10, 100), true);
如果您从排序 class 中删除 return 会更清楚。这样很明显它是在修改第一个参数,而不是创建一个新数组。例如:
int[] test = Sorting.GenerateTestArray(10, 100);
myTest.BubbleSort(test, false);
int[] testBack = Sorting.GenerateTestArray(10, 100);
backTest.BubbleSort(testBack, true);