As a part of the java.util package, we can resize the ArrayList by adding or removing elements. This is the basic difference between java array and ArrayList.
Declaraing an ArrayList
We need to import java.util to use ArrayList.
import java.util.ArrayList;
Create an ArrayList
ArrayList languages = new ArrayList();
Here is the full code.
package my_proj;
import java.util.Locale;
import java.util.ArrayList;
public class my_first {
public static void main(String[] args) {
ArrayList languages = new ArrayList();
}
}
ArrayList<Integer> marks = new ArrayList<Integer>(3);
marks.add(55);
marks.add(54);
marks.add(60);
// Using for loop and size()
for(int i=0; i < marks.size(); i++)
System.out.println(marks.get(i));
By using Iterator. For using Iterator we must include iterator utility.
import java.util.Iterator;
Looping by using iterator ( marks is our ArrayList as declared above.
Iterator j = marks.iterator();
while (j.hasNext()) {
System.out.println(j.next());
}