好的,这是我的代码。我认为对数组中的这些数字进行排序应该可以正常工作。然而,每次我尝试运行它时,它所做的只是打印“[I@178af9c0”或一些奇怪的变体。我完全不知道该怎么办,非常感谢您能给我的任何帮助。非常感谢!!!
public class BubbleSort {
public void Print(){
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] BubbleSort = {3, 4, 1, 2};
int lengthOfArray = BubbleSort.length;
for (int i = 0; i < lengthOfArray - 1; i++){
for (int n = 1; n < lengthOfArray - i; n++){
if (BubbleSort[n - 1] > BubbleSort[n]){
Swap(i, n , BubbleSort);
}
}
}
System.out.println(BubbleSort.toString());
}
private static void Swap(int index1, int index2, int[] array) {
int temp;
temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
最佳答案
在 Java 中,数组是类。因此该字符串是对数组的引用,而不是数组的内容。如果您想打印内容,最简单的方法可能是迭代所有元素。
for(int i : BubbleSort) System.out.println(""+i);
(在 Java 8 上测试,根据您的 Java 版本进行调整。)
其中存在一些逻辑错误,但由于这看起来像是一个家庭作业问题,我只会提到您正在朝着正确的方向前进,但可能需要用铅笔写出您实际在做什么并绘制流程.
关于java - 为什么我的冒泡排序代码打印出这些奇怪的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33859721/