0

I'm going to output the object element, but it not successful to output the value to public. Anyone have some idea? I have tried to implement the following code.

import java.util.Scanner;

public class Qa {

    private static void qb(int left, int right) {

        int pivotIndex = left; //set first element as pivot
        int storeIndex = pivotIndex + 1;

        for (int i = pivotIndex + 1; i <= right; i++)
            if (element[i] < element[pivotIndex]) {
                swap(i, storeIndex);
                storeIndex++;
            }

        swap(pivotIndex, storeIndex - 1);
        printElement();

        //recursion
        if (storeIndex - 2 - left >= 1)
            qsort(left, storeIndex - 2);

        if (right - storeIndex >= 1)
            qsort(storeIndex, right);
    }

    private static void swap(int n1, int n2) {
        int tmp = element[n1];
        element[n1] = element[n2];
        element[n2] = tmp;
    }

    private static void printElement() {
        for (int i = 0; i < element.length; i++)
            System.out.print(element[i] + "  ");
        System.out.println("");
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
erictc
  • 15
  • 5

3 Answers3

1

If the goal is to access the element array as a class member it just needs to be defined as static and change element's initialization to the class member.

    public static int[] element;
    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of the items you want to store: ");
        n = sc.nextInt();
        
        // This line must update element not create a local variable.
        element = new int[10];
        System.out.println("Enter the values: ");

        for(int i=0;i<n;i++) {
            element[i] = sc.nextInt();
        }

        printElement();
        qsort(0, element.length - 1);
    }

element's initialization must be changed from creating a new local variable to using the new class member. i.e. change

int[] element = new int[10];

to

element = new int[10];

If this is not done the other static methods will not have access to the data your scanner took in and will result in NullPointerExceptions.

  • Thanks. it can complete now but It show some error.`Enter the number of the items you want to store: 4 Enter the values: 36 44 23 11 Exception in thread "main" java.lang.NullPointerException at QuickSort.printElement(QuickSort.java:51) at QuickSort.main(QuickSort.java:18)` – erictc Nov 30 '20 at 16:38
  • I've updated my answer to be more explicit about the change needed to `element`'s initialization. – Jimbo McHiggins Nov 30 '20 at 16:44
0

Add a parameter

printElement(element);

Update the method

private static void printElement(int[] element) {

Note: You can print with Arrays.toString as well

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

you can pass your array of values as parameter to your printElement method and then print the elements inside the array:

private static void printElement(int[] elements) {
    for (int i = 0; i < elements.length; i++)
        System.out.print(elements[i] + "  ");
    System.out.println("");
}

and pint:

printElement(element);
qsort(0, element.length - 1);
printElement(element);
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36