JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 01 Collections Framework 02 Hierarchy of ArrayList Class 03 ArrayList in Java 04 Internal Working of ArrayList 05 Constructors of ArrayList 06 Methods of ArrayList 07 Benefits of ArrayList vs Arrays Topics For Today’s Discussion
Collections Framework
Collection SortedSet SetList Priority Queue ArrayDeque TreeSet LinkedHashSet HashSet Iterable Queue ArrayList LinkedList Vector Stack Deque
ArrayList Collection List Abstract List Hierarchy of Array List Class Iterable extends implements extends extends
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training ArrayList in Java ArrayList is a part of collection framework present in java.util package. It provides dynamic arrays in Java.
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Internal Working of ArrayList myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] Array List Initialization Adding elements to Array List Capacity of Array List is Full Creating a new array & moving the elements to new array Size is increased to double
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(int Capacity) ArrayList(Collection c) ArrayList( )
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(int Capacity) ArrayList(Collection c) ArrayList( ) This constructor builds an empty array list. Syntax: ArrayList<E> myArray = new ArrayList<E>();
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(int Capacity) ArrayList( ) ArrayList(Collection c) This constructor builds an array list that is initialized with the elements of the collection c. public boolean addAll(Collection c) Syntax:
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(Collection c) ArrayList( ) ArrayList(int Capacity) It is used to build an array list that has the specified initial capacity. ArrayList myArray = new ArrayList(int initialCapacity); Syntax:
Methods of Arraylist
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() This method is used to insert a specific element at a specific position index in a list. ArrayList<String> al=new ArrayList<String>(); al.add(“Jino"); al.add(“Piyush"); al.add(“Pramod"); Example:
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() This method is used to remove all the elements from any list. ArrayList<String> al=new ArrayList<String>(); al.add("abc"); al.add("xyz"); System.out.println("ArrayList before clear: "+al); al.clear(); System.out.println("ArrayList after clear: "+al); Example:
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() ArrayList<Integer> al = new ArrayList<Integer>(9); al.add(2); al.add(4); al.add(5); al.trimToSize(); System.out.println("The List elements are:"); The trimToSize() method in Java trims the capacity of an ArrayList instance to be the list’s current size. This method is used to trim an ArrayList instance to the number of elements it contains. Example:
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() ArrayList<Integer> al = new ArrayList<Integer>(9); al.add(2); al.add(4); al.add(5); int pos = al. indexOf(3); This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Example:
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() ArrayList<String> al=new ArrayList<String>(); Object cloneList; //Added 2 elements al.add("abc"); al.add("xyz"); System.out.println("Elements are: "); cloneList = al.clone(); System.out.println("Elements in cloneList are:"); Returns a shallow copy of this ArrayList. Example:
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() arrayList.add("element_1"); arrayList.add("element_2"); arrayList.add("element_3"); arrayList.add("element_4"); Object[] objArray = arrayList.toArray(); Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. Example:
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() arrayList.add(“J"); arrayList.add(“I"); arrayList.add(“N"); arrayList.add(“O"); arraylist.remove(“N”); Example: java.util.ArrayList.remove(Object) method removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged.
void add(int index, Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() Declaration: public int size() arrayList.add(“J"); arrayList.add(“I"); arrayList.add(“N"); arrayList.add(“O"); int asize = arraylist.size(); It returns the number of elements in this list i.e the size of the list. Example:
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Advantages of ArrayList Over Array Allows to add duplicate elements Can traverse in both directions Insert & Remove elements at particular position ArrayList is Variable length Size can be modified dynamically Can add any type of data in arraylist
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka

Java ArrayList Tutorial | Edureka

  • 1.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training
  • 2.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training 01 Collections Framework 02 Hierarchy of ArrayList Class 03 ArrayList in Java 04 Internal Working of ArrayList 05 Constructors of ArrayList 06 Methods of ArrayList 07 Benefits of ArrayList vs Arrays Topics For Today’s Discussion
  • 3.
  • 4.
  • 5.
    ArrayList Collection List Abstract List Hierarchy ofArray List Class Iterable extends implements extends extends
  • 6.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training ArrayList in Java ArrayList is a part of collection framework present in java.util package. It provides dynamic arrays in Java.
  • 7.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training Internal Working of ArrayList myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] Array List Initialization Adding elements to Array List Capacity of Array List is Full Creating a new array & moving the elements to new array Size is increased to double
  • 8.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(int Capacity) ArrayList(Collection c) ArrayList( )
  • 9.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(int Capacity) ArrayList(Collection c) ArrayList( ) This constructor builds an empty array list. Syntax: ArrayList<E> myArray = new ArrayList<E>();
  • 10.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(int Capacity) ArrayList( ) ArrayList(Collection c) This constructor builds an array list that is initialized with the elements of the collection c. public boolean addAll(Collection c) Syntax:
  • 11.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training Constructors of ArrayList ArrayList(Collection c) ArrayList( ) ArrayList(int Capacity) It is used to build an array list that has the specified initial capacity. ArrayList myArray = new ArrayList(int initialCapacity); Syntax:
  • 12.
  • 13.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() This method is used to insert a specific element at a specific position index in a list. ArrayList<String> al=new ArrayList<String>(); al.add(“Jino"); al.add(“Piyush"); al.add(“Pramod"); Example:
  • 14.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() This method is used to remove all the elements from any list. ArrayList<String> al=new ArrayList<String>(); al.add("abc"); al.add("xyz"); System.out.println("ArrayList before clear: "+al); al.clear(); System.out.println("ArrayList after clear: "+al); Example:
  • 15.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() ArrayList<Integer> al = new ArrayList<Integer>(9); al.add(2); al.add(4); al.add(5); al.trimToSize(); System.out.println("The List elements are:"); The trimToSize() method in Java trims the capacity of an ArrayList instance to be the list’s current size. This method is used to trim an ArrayList instance to the number of elements it contains. Example:
  • 16.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() ArrayList<Integer> al = new ArrayList<Integer>(9); al.add(2); al.add(4); al.add(5); int pos = al. indexOf(3); This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Example:
  • 17.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() ArrayList<String> al=new ArrayList<String>(); Object cloneList; //Added 2 elements al.add("abc"); al.add("xyz"); System.out.println("Elements are: "); cloneList = al.clone(); System.out.println("Elements in cloneList are:"); Returns a shallow copy of this ArrayList. Example:
  • 18.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() arrayList.add("element_1"); arrayList.add("element_2"); arrayList.add("element_3"); arrayList.add("element_4"); Object[] objArray = arrayList.toArray(); Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. Example:
  • 19.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() arrayList.add(“J"); arrayList.add(“I"); arrayList.add(“N"); arrayList.add(“O"); arraylist.remove(“N”); Example: java.util.ArrayList.remove(Object) method removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged.
  • 20.
    void add(int index,Object element) void clear( ) void trimToSize() Int indexOf(Object O) Object clone() Object[] to Array() Object remove(int index) Int size() Declaration: public int size() arrayList.add(“J"); arrayList.add(“I"); arrayList.add(“N"); arrayList.add(“O"); int asize = arraylist.size(); It returns the number of elements in this list i.e the size of the list. Example:
  • 21.
    JAVA CERTIFICATION TRAININGwww.edureka.co/java-j2ee-soa-training Advantages of ArrayList Over Array Allows to add duplicate elements Can traverse in both directions Insert & Remove elements at particular position ArrayList is Variable length Size can be modified dynamically Can add any type of data in arraylist