Alternative code to find all indices at which an element exists in an array. Can you help find me the error in this code?

public class Main {
public static void main(String args[]) {
int arr[]= {3,3,0,7,3,7};
int p[]= AllIndex(arr, 0, 7) ;
System.out.println§;
//System.out.println(“hello”);
}
public static int[] AllIndex(int[] arr, int n, int data)
{
int arr1[]={};
if(n==arr.length)
return arr1;

    if(arr[n]==data)
    {
        arr1.add(n);
        return arr1;
    }
    int p[]= AllIndex(arr, n, data);
    return p;

}

}

Hi @aparna34,
This method will not work as array does not have a method add and to use that method you have to make a arraylist and that is the same as told in the video.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.