Java BlueJ Bubblesort

Java BlueJ Bubblesort,java,bluej,Java,Bluej,我有一个关于Java项目的问题。在学校里,我们开始编写一个bubblesort方法,该方法应该对整数列表进行排序,然后对它们进行图形化排序。然而,我的问题是,我们必须在家里完成它,而我似乎被一件小事卡住了 这是我的Bubblesort类的全部代码: import java.util.Random; /** * Write a description of class Bubblesort here. * * @author Aaron Zwickenpflug * @version 3

我有一个关于Java项目的问题。在学校里,我们开始编写一个bubblesort方法,该方法应该对整数列表进行排序,然后对它们进行图形化排序。然而,我的问题是,我们必须在家里完成它,而我似乎被一件小事卡住了

这是我的
Bubblesort
类的全部代码:

import java.util.Random;
/**
 * Write a description of class Bubblesort here.
 * 
 * @author Aaron Zwickenpflug
 * @version 30.01.2017
 */
public class Bubblesort {
    private Random random;
    private int maxSquare;
    private int maxRandom;
    private int[] x;
    private Square[] square;

    public Bubblesort() {
        this.random = new Random();

        this.maxSquare = 100;
        this.maxRandom = 200;

        this.x = new int[this.maxSquare];
        this.square = new Square[this.maxSquare];

        for (int i = 0; i < this.maxSquare; i++) {
            this.square[i] = new Square(i, x[i]);
            this.x[i] = random.nextInt(maxRandom);
            this.square[i].changeY(x[i]);
            // System.out.println(this.x[i]);
        }
    }

    public void redraw() {
        for (int i = 0; i < this.maxSquare - 1; i++) {
            this.square[i + 1].changeY(this.x[i]);
        }
    }

    public void sort_bubble() {
        int m;
        for (int i = 1; i < 100; i++) {
            for (int y = 0; y < this.maxSquare - i; y++) {
                if (this.x[y] > this.x[y + 1]) {
                    m = this.x[y];
                    this.x[y] = this.x[y + 1]; 
                    this.x[y + 1] = m;
                    this.square[y + 1].changeY(this.x[i + 1]);
                }
            }
        }
    }

}
现在,问题出在最后一行:

this.square[y + 1].changeY(this.x[i + 1]);
它通常(当我运行程序时)会更改2个正方形之间的y坐标,并对其进行“排序”。虽然它只是弄乱了图形,方块开始变为一些奇怪的值。我确实检查了需要排序的列表,效果很好

这就是图形的外观:

我希望有人能帮我解决问题。 提前感谢,,
A

我花了一些时间查看了您的代码,我应该早点发现问题,但是您使用的
Canvas
类把我甩了。因此,我使用Java Swing组件而不是AWT组件重写了您的程序,在您的
sort\u bubble()
方法中更正了一行代码并添加了一行代码后,我得到了以下结果:

问题在于交换
sort_bubble()
方法中的元素,或者更确切地说,交换基础
int
数组中的元素,但是没有正确更新
Square
数组:

m = this.x[y];
this.x[y] = this.x[y + 1];
this.x[y + 1] = m;
this.square[y + 1].changeY(this.x[i + 1]);
上面您正在用
x[y+1]
交换
x[y+1]
,但出于某种原因,您正在用
x[i+1]
值更新
square[y+1]
。您对
Square
数组的更新应反映您在基础
int
数组上的掉期。因此,
sort\u bubble()
中的交换和更新代码应该类似于:

m = this.x[y];
this.x[y] = this.x[y + 1];
this.x[y + 1] = m;

// reflect the above swaps when updating 'this.square'
this.square[y].changeY(this.x[y]);
this.square[y + 1].changeY(this.x[y + 1]);


希望这个答案能有所帮助,但很抱歉,需要一段时间才能做出回应。如果您对我在回答中提到的内容有任何进一步的问题,请告诉我。

我花了一些时间查看了您的代码,我本应该早点发现问题,但您使用的
Canvas
类将我拒之门外。因此,我使用Java Swing组件而不是AWT组件重写了您的程序,在您的
sort\u bubble()
方法中更正了一行代码并添加了一行代码后,我得到了以下结果:

问题在于交换
sort_bubble()
方法中的元素,或者更确切地说,交换基础
int
数组中的元素,但是没有正确更新
Square
数组:

m = this.x[y];
this.x[y] = this.x[y + 1];
this.x[y + 1] = m;
this.square[y + 1].changeY(this.x[i + 1]);
上面您正在用
x[y+1]
交换
x[y+1]
,但出于某种原因,您正在用
x[i+1]
值更新
square[y+1]
。您对
Square
数组的更新应反映您在基础
int
数组上的掉期。因此,
sort\u bubble()
中的交换和更新代码应该类似于:

m = this.x[y];
this.x[y] = this.x[y + 1];
this.x[y + 1] = m;

// reflect the above swaps when updating 'this.square'
this.square[y].changeY(this.x[y]);
this.square[y + 1].changeY(this.x[y + 1]);


希望这个答案能有所帮助,但很抱歉,需要一段时间才能做出回应。如果您对我在回答中提到的内容有任何进一步的问题,请告诉我。

请将您的问题包括(基本上添加
Square
class'代码),并添加指向图形图像的链接。在重绘for循环中,为什么要在maxSquare-1上迭代?您应该始终根据正在迭代的集合的大小进行迭代。inew square(i,x[i])中使用
x[i]
之前,您应该设置
x[i]
(除非这是有意的)。我相信
int
数组将每个索引初始化为
0
所以你基本上是在做
new Square(I,0)
@RAZ_Muh_Taz我同意你的评论,尽管我相信OP这样做是为了避免因为
this.Square[I+1]
this.Square[I+1]中遇到
arrayindexoutofbounds异常(此.x[i])
。更不用说
i
i
相同,因为
this.square.length==this.x.length
。好的,所以我添加了square类,以及图形的图像。顺便说一句,这不是我写的任何代码。这正是我们的代码老师提供给我们。请您的问题包括一个(基本上添加
Square
class'代码)还可以添加指向图形图像的链接。在重绘for循环中,为什么要在maxSquare-1上迭代?您应该始终根据迭代的集合大小进行迭代。ix[i]之前应该设置
x[i]
new Square(i,x[i])
中(除非这是有意的)。我相信
int
数组将每个索引初始化为
0
,所以基本上就是在做
new Square(i,0)
@RAZ_Muh_Taz我同意你的评论,尽管我相信OP这样做是为了避免因为
this.square[I+1]
而遇到
arrayindexoutofbounds异常
。更不用说
i
i
相同,因为
this.square.length==this.x.length
。好的,所以我添加了square类,以及图形的图像。顺便说一句,这不是我写的任何代码。这正是我们的代码老师提供给我们。