Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法理解为什么我的选择排序作为java方法的实现不能按预期工作_Java_Arrays_Sorting_Data Structures_Selection Sort - Fatal编程技术网

无法理解为什么我的选择排序作为java方法的实现不能按预期工作

无法理解为什么我的选择排序作为java方法的实现不能按预期工作,java,arrays,sorting,data-structures,selection-sort,Java,Arrays,Sorting,Data Structures,Selection Sort,无法理解为什么我的选择排序作为java方法的实现没有按预期工作 类选择排序{ void SelectionSortMethod(int[]数组){ 最小温度,温度=0; SwapClass swap=新SwapClass(); ArrayPrinting AP=新的ArrayPrinting(); for(int i=0;i

无法理解为什么我的选择排序作为java方法的实现没有按预期工作

类选择排序{
void SelectionSortMethod(int[]数组){
最小温度,温度=0;
SwapClass swap=新SwapClass();
ArrayPrinting AP=新的ArrayPrinting();
for(int i=0;i
我希望数组按acsending顺序排序,但out是原始数组。

实际输出是传入main方法的原始数组。但是预期的输出是数组应该按升序排序。

内部for循环中的数组[min]需要与数组[j]而不是数组[i]进行比较


在SwapMethod中,数组元素没有被交换,而是变量X和y的值被交换。

您的
SwapMethod
是错误的(因为它交换局部变量,所以不会影响数组)。除此之外,没有理由使用
SwapClass
@Eran,谢谢您的帮助。但是我需要在不同的类中保留不同的方法,并使用它们的类对象调用它们。我调试了SwapMethod,发现了从数组传递的值和交换的值。如果必须保留该SwapClass,则可以保留它,但应该将数组和要交换的元素的索引传递给
SwapMethod
。当您传递要交换的值时,您无法修改数组。但是我已经在SortMethod()中传递数组,并在outer for循环中调用SortMethod()中的SwapMethod(),并期望在遍历过程中交换数组元素(使用outer循环)同样,当所有代码都用main方法编写时,冒泡排序或选择排序的代码也可以工作。Java是一种传递值语言。因此,
swap.SwapMethod(array[i]、array[min]、temp)
调用不会更改任何传递参数的值。因此,
array[i]
array[min]
保持不变。