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

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.