Home > other >  How to declare a global array and variable in Java?
How to declare a global array and variable in Java?

Time:11-24

I'm pretty new to java and am trying to implement prefix sum

package Arrays;

public class prefixSum {

    static void sumArray(int arr[]) {
        int n = arr.length;
        int aux[] = new int[n];
        int curr = arr[0];
        aux[0] = curr;
        for (int i = 1; i < n; i  ) {
            aux[i] = arr[i]   curr;
        }
    }

    static int getSum(int arr[],int start,int end){
        int n=arr.length;
        sumArray(arr);
        if(start==0){
            return aux[end];
        }
        return aux[end]-aux[start-1]
    }

    public static void main(String[] args) {
        int arr[] = { 2, 5, 7, 3, 4, 5, 3 };
        int start = 2;
        int end = 5;
        System.out.print(getSum(arr, start, end));

    }
}


So, I want aux[] to be a global array that can be accessed anywhere. Also, I would like the length of the aux[] array to be the same as the length as arr[]

Thanks in advance !!

CodePudding user response:

you can define the aux[] array in class level and initialize it inside the main method:

public class prefixSum {
    static int[] aux;

...
    public static void main(String[] args) {
        int[] arr = { 2, 5, 7, 3, 4, 5, 3 };
        aux = new int[arr.length];
    ...
    }

And remove the aux array declaration from sumArray method.

P.S.: The Java convention about array declarations is like int[] arr but not int arr[]

Read more about Java code conventions here.

After re-formatting your code It'll be like this (I've not tested the functionality btw) :

public class prefixSum {
    static int aux[];

    static void sumArray(int[] arr) {
        int n = arr.length;
        int curr = arr[0];
        aux[0] = curr;
        for (int i = 1; i < n; i  ) {
            aux[i] = arr[i]   curr;
        }
    }

    static int getSum(int[] arr, int start, int end) {
        int n = arr.length;
        sumArray(arr);
        if (start == 0) {
            return aux[end];
        }
        return aux[end] - aux[start - 1];
    }

    public static void main(String[] args) {
        int[] arr = {2, 5, 7, 3, 4, 5, 3};
        aux = new int[arr.length];

        int start = 2;
        int end = 5;
        System.out.print(getSum(arr, start, end));

    }
}

CodePudding user response:

You can do so by making it a static member of the class as follow:

public class PrefixSum {

    private static int[] aux;

    ...

}

However, keep in mind the following:

  • Your array parameter declaration is wrong, you should declare it as int[] aux rather than int aux[]
  • You should use the global state as less as possible, they're dangers and will cause you issues in the future if the program would run on multiple threads for example. Local state is always better if possible.
  • Last but not least, and mainly FYI - arrays in java are passed by reference and not by value, which means that when you return an array from a function you return the actual array and not a copy of it.

So my suggestion would be something like this:

public class PrefixSum {

    static int[] sumArray(int[] arr) {
        int n = arr.length;
        int[] aux = new int[n];
        int curr = arr[0];
        aux[0] = curr;
        for (int i = 1; i < n; i  ) {
            aux[i] = arr[i]   curr;
        }

        return aux;
    }

    static int getSum(int[] arr, int start, int end) {
        int[] aux = sumArray(arr);
        if (start == 0) {
            return aux[end];
        }
        return aux[end] - aux[start - 1];
    }

    public static void main(String[] args) {
        int[] arr = {2, 5, 7, 3, 4, 5, 3};
        int start = 2;
        int end = 5;
        System.out.print(getSum(arr, start, end));
    }
}
  • Related