Java Array to ArrayList

We can also convert arrays into array lists. For that, we can use the asList() method of the Arrays class. To use asList(), we must import the java.util.Arrays package first. For example,


\begin{lstlisting}
import java.util.ArrayList;
import java.util.Arrays;
\par
cla...
...t(arr));
System.out.println(''\nArrayList: '' + animals);
}
}
\end{lstlisting}

Output


\begin{lstlisting}
Array: Dog, Cat, Horse
ArrayList: [Dog, Cat, Horse]
\par
\end{lstlisting}

In the above program, we first created an array arr of the String type. We then converted the array into an array list using the asList() method.