import java.util.*;
class Ball{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
Object a[]= al.toArray();
for (int i=0; i<a.length; i++)
{
System.out.println("Contents are :"+ a[i]);
}
}}
So I created an ArrayList and added a couple of elements to it. Then used toArray method and got object array a. However if I use i<a.length()
instead of i<a.length
, I get an error. What I don't understand is that if length()
is a method, what is length
?
Secondly why can't I output ArrayList elements using a for loop?
for (int i=0; i<al.length(); i++)
{
System.out.println("Contents are :"+ al[i]);
}
(over here using al.length() as well as al[i] gives an error)