Menu

Showing posts with label List in Java. Show all posts
Showing posts with label List in Java. Show all posts

Working with list and arrayList in Java.

In this tutorial we will see a simple and small implementation of List and ArrayList using Java.

ArrayList is resize-able implementation is List type. Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

We are writing a program where we will add some values in the ArrayList and then we will iterate on that List and print the values on console.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package rashid.jorvee;

import java.util.ArrayList;
import java.util.List;

public class ListImpl {

 public static void main(String[] args) {
  List list=new ArrayList();
  List newlist=new ArrayList();
  newlist.add("New Delhi");
  newlist.add("9999999");
  newlist.add("1212121");
  newlist.add("Rashid");
  newlist.add("Jorvee");
  for(int i=0;i<newlist.size();i++) {
   System.out.println("At Index "+i +" List has value "+newlist.get(i));
  }

 }

}
Output:
At Index 0 List has value New Delhi
At Index 1 List has value 9999999
At Index 2 List has value 1212121
At Index 3 List has value Rashid
At Index 4 List has value Jorvee