任何人都可以教我如何获得这个编码的答案出现在另一个类的编码吗?
public class BubbleSort4
{
public static void main(String[] args) {
int intArray[] = new int[]{5,90,35,45,150,3};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
}
bubbleSort(intArray);
System.out.println("");
System.out.println("Array After Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
}
}
public static void bubbleSort(int[] intArray)
{
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(intArray[j-1] > intArray[j])
{
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
}
}
}
编码答案: 冒泡排序后的数组 5 90 35 45 150 3
冒泡排序后的数组 3 5 35 45 90 150
答案 0 :(得分:1)
我认为你必须改变你的班级结构。使intArray成为一个类字段:
public class BubbleSort4 {
static int intArray[] = new int[]{5,90,35,45,150,3};
public static void main(String[] args) {
...
有一个返回已排序数组的方法:
public static int[] getSortedArray() {
bubbleSort(intArray);
return intArray;
}
现在您可以从任何类调用BubbleSort4.getSortedArray()
,并返回已排序的数组。希望这会有所帮助。