Using get() Method

To randomly access elements of an array list, we use the get() method. For example,


\begin{lstlisting}
import java.util.ArrayList;
\par
class Main {
public static ...
...get(0);
System.out.print(''Element at index 0: '' + str);
}
}
\end{lstlisting}

Output


\begin{lstlisting}
ArrayList: [Dog, Horse, Cat]
Element at index 0: Dog
\end{lstlisting}