I am tring to print N to 1 using recursion. But while doing so I am getting the following errors what should I change in the code to convert the ArrayList to array successfully
Here is the Code-->
import java.util.ArrayList;
class HelloWorld {
static ArrayList<Integer> ar = new ArrayList<>();
public static int[] printNos(int x) {
// Write Your Code Here
if(x>0){
ar.add(x);
printNos(x-1);
}
int [] arr = new int[ar.size()];
arr = ar.toArray(arr);
return arr;
}
public static void main(String[] args) {
int [] ark = printNos(5);
for(Interger e: ark){
System.out.print(e);
}
}
}
and the following error is occuring-->
ERROR!
javac /tmp/kEUqQIfOZV/HelloWorld.java
/tmp/kEUqQIfOZV/HelloWorld.java:11: error: no suitable method found for toArray(int[])
arr = ar.toArray(arr);
^
method Collection.<T#1>toArray(IntFunction<T#1[]>) is not applicable
(cannot infer type-variable(s) T#1
(argument mismatch; int[] cannot be converted to IntFunction<T#1[]>))
method ArrayList.<T#2>toArray(T#2[]) is not applicable
(inference variable T#2 has incompatible bounds
equality constraints: int
lower bounds: Object)
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>toArray(IntFunction<T#1[]>)
T#2 extends Object declared in method <T#2>toArray(T#2[])
/tmp/kEUqQIfOZV/HelloWorld.java:18: error: cannot find symbol
for(Interger e: ark){
^
symbol: class Interger
location: class HelloWorld
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors
Here I know that the second Error is occuring due to the first and I can also solve this problem using various other methods but what I want to learn is why the ArrayList in this code is not converted in array and throws the Error and what changes i have to do to avoid this error ?