2

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

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45

7 Answers7

5

The ArrayList has a method size that returns the size of the list, and a get method to get the element stored at a particular index.

So your for loop could look like:

for(int k=0; k < al.size(); k++) {
    System.out.println("elements are" + al.get(k));
}

Or if you want you could loop through each element without an index:

for(String text : al) {
    System.out.println("elements are" + text);
}

If you want to use streams with java8, you could also do:

al.stream().forEach(System.out::println);

If you are interested, there's another answer that also talks about looping through lists in java.

Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
3

Since ArrayList is a collection not an array, you may use al.size() instead.

If you want to use index try this

al.get(k)
Purushoth
  • 2,673
  • 3
  • 22
  • 36
kevin
  • 31
  • 4
0

Try this one You can't treat arrylist as array

for(int k=0;k<al.size();k++){
    System.out.println("elements are"+ al.get(k));
}
Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
0

You can try this way :

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.size();k++)
       {
         System.out.println("elements are"+ al.get(k) );
       }
   }
}
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
0
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.size();k++)
  {
      System.out.println("elements are"+al.get(k));
  }

You need to use size() in order to find out the length of arraylist When you use Eclipse like Editors, hope you will overcome all these methods easily and also syntax errors itself help to learn properly.

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
priya raj
  • 362
  • 2
  • 8
0

Since you are new to Collection You confusing Array with Arraylist . Arraylist doesnot have length . It have size() method .

Since already good option given So go with java 8 Stream

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");

        al.stream().forEach(System.out::println);
soorapadman
  • 4,451
  • 7
  • 35
  • 47
-1
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.size();k++)
      {
       System.out.println("elements are"+al.get(k));
      }
   }
}
Ishtiaque05
  • 431
  • 3
  • 10