我在Java不是新手,但我在JUnit。我对一个简单的for循环有问题。我用气泡排序来排序数组元素,但我不知道为什么最后两个元素在循环过程中消失。我知道这是一件很小的事,但我找不到错误。你能帮帮我吗?
这是我的课:
package exercise5;
public class Ejercicio5 {
public static int[] sort(int[] arrayNums) {
// array that I have tried: {6,5,8,3,7,1}; [6]
System.out.println("size: " + arrayNums.length);
for (int j = 0; j < arrayNums.length; j++) {
System.out.println("j:" + j);
if (arrayNums[j] > arrayNums[j + 1]) {
System.out.println("entra");
int numGuardado = arrayNums[j + 1];
arrayNums[j + 1] = arrayNums[j];
arrayNums[j] = numGuardado;
}
print(arrayNums);
}
return arrayNums;
}
public static void print(int[] arrayParaImprimir) {
System.out.println("Array:");
for (int j = 0; j < arrayParaImprimir.length; j++) {
if (j != arrayParaImprimir.length - 1) {
System.out.print(arrayParaImprimir[j] + ", ");
} else {
System.out.print(arrayParaImprimir[j] + "\n");
}
}
}
}我的TestClass和JUnit5:
package exercise5;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import junit.framework.TestCase;
public class Ejercicio5Test extends TestCase{
@Test
public void resultadoCorrecto(){
int[] correct = {1,3,5,6,7,8};
int[] array = {6,5,8,3,7,1};
int[] result = Ejercicio5.sort(array);
Assert.assertArrayEquals(result, correct);
}
@Test
public void resultadoIncorrecto(){
int[] correct = {1,3,5,6};
int[] array = {3,5,6,1};
int[] result = Ejercicio5.sort(array);
Assert.assertArrayEquals(result, correct);
}
}当j等于4时,排序是: 5,6,3,7,1,8,但当j传递到5时,两个元素消失。此外,在我的Test类中只有两个方法,但是当我运行它时,它又识别了一个方法,并给出了一个错误:

这是我尝试过的{1,3,5,6,7,8}数组,这是我期望{5,6,3,7,1,8}数组大小为6的数组,而不是消失的元素。
这是控制台中的输出。不是ArrayIndexOutOfBounds。只会消失2个元素,大小会改变,不会抛出任何异常:
size: 6
j:0
entra
Array:
5, 6, 8, 3, 7, 1
j:1
Array:
5, 6, 8, 3, 7, 1
j:2
entra
Array:
5, 6, 3, 8, 7, 1
j:3
entra
Array:
5, 6, 3, 7, 8, 1
j:4
entra
Array:
5, 6, 3, 7, 1, 8
j:5
size: 4
j:0
Array:
3, 5, 6, 1
j:1
Array:
3, 5, 6, 1
j:2
entra
Array:
3, 5, 1, 6
j:3发布于 2022-11-14 12:08:57
(int j= 0;j< arrayNums.length;j++) {
这对输入中的每个数字都是循环的。然后你..。
if (arrayNumsj > arrayNumsj + 1) {
将其与输入中的下一个数字进行比较。因此,在最后一个循环中,您将最后一个数字(arrayNums[j])与.之后的号码。因此,ArrayIndexOutOfBoundsEx是不存在的。
你想少循环一个。
发布于 2022-11-14 12:39:20
你漏了一个循环,这是正确的代码-
public class Ejercicio5 {
//you don't need to return as it modifies exiting array
public static void sort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
// swap arr[j+1] and arr[j]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
/* Prints the array */
public static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int[] array = {6,5,8,3,7,1};
Ejercicio5.sort(array);
System.out.println("Sorted array");
Ejercicio5.printArray(array);
}}
输出- Sorted array 1 3 5 6 7 8
现在,您的测试案例应该可以进行一些小的更改。
发布于 2022-11-14 13:20:34
如果运行JUnit测试,它将运行@Test注释的所有方法。
您的输出准确地反映了排序函数的两个调用,首先是从resultadoCorrecto()到行"j:5“的调用,第二个是来自resultadoIncorrecto()的调用,然后是输出行(从”大小: 4")。
所以什么都没有消失,。
resultadoCorrecto()调用数组大小为6的排序函数。
resultadoIncorrecto()调用数组大小为4的排序函数。
你的泡沫分类问题:
for循环必须一直到索引< length-1为止。
你必须鼓起所有的泡沫与另一个循环。(见Sagar Kale的回答)
https://stackoverflow.com/questions/74431336
复制相似问题