-2

I have the following program where I have to return an array after performing certain operation on 2 arrays (display elements of Array a that are not in Array b),

Here's the code

class Main {
    static int[] result(int a[], int b[]) {
        int count, x, m = 0;
        int d[] = new int[100];
        for (int i = 0; i < a.length; i++) {
            count = 0;
            x = a[i];
            for (int j = 0; j < b.length; j++) {
                if (b[j] == x) {
                    count++;
                }
            }
            if (count == 0) {
                //System.out.print(a[i]+" ");
                d[m] = a[i];
                m++;
            }
        }
        return d;
    }
}

public class HelloWorld {
    public static void main(String args[]) {
        int a[] = new int[] { 2, 3, 5, 6, 8, 9 };
        int b[] = new int[] { 0, 2, 6, 8 };
        int c[] = result(a, b);
        for (int k = 0; k < c.length; k++) {
            System.out.print(c[k] + " ");
        }
    }
}

The following error occurs:

HelloWorld.java:34: error: cannot find symbol int c[]=result(a,b); ^ symbol: method result(int[],int[]) location: class HelloWorld 1 error

Vimukthi
  • 846
  • 6
  • 19
sree
  • 31
  • 5

1 Answers1

1

To call a static method of another class, you prefix the method name with the name of the class:

int c[] = Main.result(a, b);
// -------^^^^^
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • When I'm returning the array, I have declared the size of array 'd' to be 100. Is there a way to allocate size dynamically? – sree Jul 05 '19 at 08:19
  • @sree - Just use any expression instead of `100` in `int d[] = new int[100];` But that still requires that you know the size you want in advance. You might use a `List` if you don't know the size in advance (which I think you don't). Or you could make `d` the same size as `a` (`int d[] = new int[a.length];`), then if you didn't need all of it you could copy to a smaller array before returning. – T.J. Crowder Jul 05 '19 at 08:23