-1

I am trying to assign 3 integer arrays to a method that returns one version. But when i try this it says variable bubbleArray and variable insertionArray have not been initialized. Is there another way to do this and still keep the same original values from the method.

    Integer[] bubbleArray,insertionArray,selectionArray = numGenerator();


    bubbleSort(radioValue,bubbleArray);
    selectionSort(radioValue,selectionArray);
    insertionSort(radioValue,insertionArray);

 public Integer[] numGenerator() {
    Random rn = new Random();
    originalOutput.setText("");
    sortedOutput.setText("");
    referenceArray.clear();

    if (number10Button.isSelected()) {
        for (int i = 0; i < 10; i++) {
            int answer = rn.nextInt((10000 - (-10000)) + 1) + (-10000);
            referenceArray.add(answer);
            originalOutput.append(referenceArray.get(i).toString() + "\n");
        }
    } else if (number100Button.isSelected()) {
        for (int i = 0; i < 100; i++) {
            int answer = rn.nextInt((10000 - (-10000)) + 1) + (-10000);
            referenceArray.add(answer);
            originalOutput.append(referenceArray.get(i).toString() + "\n");

        }
    } else if (number1000Button.isSelected()) {
        for (int i = 0; i < 1000; i++) {
            int answer = rn.nextInt((10000 - (-10000)) + 1) + (-10000);
            referenceArray.add(answer);
            originalOutput.append(referenceArray.get(i).toString() + "\n");

        }
    } else if (number5000Button.isSelected()) {
        for (int i = 0; i < 5000; i++) {
            int answer = rn.nextInt((10000 - (-10000)) + 1) + (-10000);
            referenceArray.add(answer);
            originalOutput.append(referenceArray.get(i).toString() + "\n");

        }
    }

    Integer[] bubbleArray = referenceArray.toArray(new Integer[referenceArray.size()]);
    return bubbleArray;
}
Nm Ann
  • 21
  • 1
  • 4
  • You only initialised one of your arrays. [Initializing multiple variables to the same value in Java](https://stackoverflow.com/q/6202818) – Bernhard Barker Feb 07 '18 at 19:38

1 Answers1

1

Your code declares 3 Integer[] variables and assigns the last one to what numGenerator() returns.

Integer[] bubbleArray,insertionArray,selectionArray = numGenerator();

Now since you want three arrays, not just three variables pointing to one array you need to make copies of the array, for example with clone(). If you don't make copies you will have one array which is sorted by bubble sort and the other sorting methods will try to sort an already sorted array, which is not what you want.

Integer[] bubbleArray = numGenerator();
Integer[] insertionArray = bubbleArray.clone();
Integer[] selectionArray = bubbleArray.clone();
Kayaman
  • 72,141
  • 5
  • 83
  • 121