Home > OS >  Java: Array elements dissapearing during a for loop
Java: Array elements dissapearing during a for loop

Time:11-14

I'm not new at Java, but I'm in JUnit. I'm having a problem with a simple for loop. I'm ordering array elements with bubble sorting, but I don't know why the two last elements disappear during the loop. I know it will be a little tiny thing, but I can't find the mistake. Could you help me, please? This is my class:

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");
            }

        }
    }
}

My TestClass with 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);
    }
}

When j is equal to 4, the ordering is doing: 5, 6, 3, 7, 1, 8 but when j passed to 5, two elements dissapear. In addition, in my Test class there are only two methods, but, when I run it, it recognise one more and give me an error:

enter image description here

This is the array that I have tried {1,3,5,6,7,8} and this is that I expected {5,6,3,7,1,8} with 6 of array's size, not element dissapearing.

CodePudding user response:

for (int j = 0; j < arrayNums.length; j  ) {

This loops for every number in the input. Then you..

if (arrayNums[j] > arrayNums[j   1]) {

Compare this to the next number in the input. On the last loop, you are therefore comparing the last number (arrayNums[j]) with the.. number after that. Which doesn't exist, hence, ArrayIndexOutOfBoundsEx.

You want to loop one fewer.

CodePudding user response:

You missed one loop here is the correct code -

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);
}

}

output - Sorted array 1 3 5 6 7 8

and your testcases should work now with minor changes

  • Related