Home > java > ArrayList in java example

ArrayList in java example

ArrayList in java example

ArrayList in Java is also concrete sub-class of collection framework classes whose object allows us to organize the data either in the same type or in a different type.

ArrayList extends and implements below classes and interfaces.

ArrayList extends AbstractSequentialList extends AbstractList extends AbstractCollection implements List

Creating ArrayList is nothing but creating an object of the ArrayList class.

ArrayList al = new ArrayList();

ArrayList API

1) ArrayList ();

2) ArrayList (int size);

Advantages of ArrayList over LinkedList

1) No additional memory space is required for data of ArrayList. 

2) Retrieval time is quite faster. Performance is high. Since there is no memory space is required for maintaining the address of data of ArrayList.

3) ArrayList object takes less memory space.

4) Retrieving the data from ArrayList is fast that’s mean it take less execution time.

5) The overall performance of ArrayList is more as compared to LinkedList.

In ArrayList data is organizing in the form of a cell. Cell values and storing in heap memory and sell addresses are stored in associated memory.

ArrayList values can we retrieve based on the position of Cells.

Note: In Real world whenever we come across LinkedList class it is always recommended to the Java programmer to substitute with ArrayList for the obtaining internal advantage of ArrayList.

Basic Program of ArrayList in Java


import java.util.ArrayList;
public class Main
{
public static void main(String arg[])
{
ArrayList<String> a=new ArrayList<String>();
a.add("John");
a.add("Miller");
a.add("Yuvraj");
a.add("Maxwell");

for(String str:a)
{
System.out.println(str);
}
}
}

Output:

John
Miller
Yuvraj
Maxwell

How to Retrive data from ArrayList using Iterator


import java.util.*;
public class Arraylist {

   public static void main(String args[]) {
      // create an array list
      ArrayList al = new ArrayList();
      System.out.println("Initial size of al: " + al.size());

      // add elements to the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");
      al.add(1, "A2");
      System.out.println("Size of al after additions: " + al.size());

      // display the array list
      System.out.println("Contents of al: " + al);

      // Remove elements from the array list
      al.remove("F");
      al.remove(2);
      System.out.println("Size of al after deletions: " + al.size());
      System.out.println("Contents of al: " + al);
   }
}

Output

Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]

ArrayList example : Storing User-defined class objects


Let’s see an example where we are storing Student class object in array list.

import java.util.*;
class Std
{
  int sid,marks;
  String sname;

  Std(int sid,int marks,String sname)
  {
    this.sid=sid;
    this.marks=marks;
    this.sname=sname;
  }
}

public class Main
{
  public static void main(String args[])
  {
    //Creating user-defined class objects 
    Std s1=new Std(1,236,"Ravi");
    Std s2=new Std(2,395,"Rahul");
    Std s3=new Std(3,254,"Raj");

   //creating arraylist  
   ArrayList<Std> al=new <Std>ArrayList();

   //Adding object to ArrayList
   al.add(s1); 
   al.add(s2);
   al.add(s3);

   //Getting Iterator Object to retrieve data from ArraList 
   Iterator itr=al.iterator();

   //traversing elements of ArrayList object 
   while(itr.hasNext())
   {
     Std st=(Std)itr.next();
     System.out.println(st.sid+" "+st.sname+" "+st.marks);
   }
  }
}

OUTPUT

1 Ravi 236
2 Rahul 395
3 Raj 254
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *