Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Monday

ArrayList in Java with example Collections framework

ArrayList in Java with example Collections framework

ArrayList which implements the List Interface which belongs to the Collections Framework. ArrayList is very fast and ease of use. So developers prefer ArrayList over Arrays in java.

Advantages:


  • Other than Arrays ArrayList can increase or decrease its size based on the data size.
  • Fast so developers prefer ArrayList
Example:



/**
 * author: JOBBERINDIA
 */
package com.jobberindia;

import java.util.*;
/**
 * @author user
 *
 */
public class HelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
        
        list.add("Hello");
        list.add("World");
System.out.println("Size of list before removing"+list.size());
list.remove("Hello");
System.out.println("Size of list "+list.size());
}

}

Output: - 

Size of list before removing2
Size of list 1

Important Methods belongs to ArrayList:

  1.                 list.add(arg0);
  2. list.add(arg0, arg1);
  3. list.addAll(arg0);
  4. list.addAll(arg0, arg1);
  5. list.clear();
  6. list.contains(arg0);
  7. list.containsAll(arg0);
  8. list.containsAll(arg0);
  9. list.equals(arg0);
  10. list.get(arg0);
  11. list.size();
  12. list.isEmpty();
  13. list.lastIndexOf(arg0);
  14. list.remove(arg0);
  15. list.removeAll(arg0);
  16. list.toArray();
  17. list.toString();

No comments:

Post a Comment