I am new to Collection Framework
.i am running a java program using ArrayList()
. When I am trying loop it to get the elements of it, but it is throwing error like
HelloWorld.java:15: error: cannot find symbol
for(int k=0;k<al.length;k++)
^
symbol: variable length
location: variable al of type ArrayList<String>
HelloWorld.java:17: error: array required, but ArrayList<String> found
System.out.println("elements are"+al[k]);
here is the code i written.
import java.util.ArrayList;
public class HelloWorld{
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("pen");
al.add("pencil");
al.add("ink");
al.add("notebook");
al.add("book");
al.add("books");
al.add("paper");
al.add("white board");
for(int k=0;k<al.length;k++)
{
System.out.println("elements are"+al[k]);
}
}
}
help me to point out my error. thanks in advance