SlideShare a Scribd company logo
Programming in Java
Topic: ArrayList
Introduction
• Standard Java arrays are of a fixed length.
• After arrays are created, they cannot grow or shrink, which
means we can not resize Arrays.
• Arrays are useful when we know in advance how many
elements the array is to hold.
• ArrayList is a collection which provides the implementation of
resizable array.
ArrayList
• The ArrayList class extends AbstractList and implements the List
interface.
• Defined in java.util package.
• ArrayList supports dynamic arrays that can grow as needed.
• Array lists are created with an initial size.
• When this size is exceeded, the collection is automatically enlarged.
• When objects are removed, the array may be shrunk.
ArrayList Constructors
• T he ArrayList class supports three constructors.
 ArrayList() : creates an empty array list with an initial capacity
sufficient to hold 10 elements.
 ArrayList(int capacity) : creates an array list that has the
specified initial capacity.
 ArrayList(Collection c) : creates an array list that is initialized
with the elements of the collection c.
Methods of ArrayList
boolean add(Object o)
• Appends the specified element to the end of this list.
void add(int index, Object element)
• Inserts the specified element at the specified position index in
this list.
• Throws IndexOutOfBoundsException if the specified index is
out of range.
boolean addAll(Collection c )
• Appends all of the elements in the specified collection to the end of
this list.
• Throws NullPointerException if the specified collection is null.
boolean addAll(int index, Collection c )
• Inserts all of the elements of the specified collection into this list,
starting at the specified position.
• Throws NullPointerException if the specified collection is null.
void clear()
• Removes all of the elements from this list.
Object remove(int index)
• Removes the element at the specified position in this list.
boolean remove(Object o)
• Removes the first specified object present in the arraylist.
int size()
• Returns the number of elements in the list.
boolean contains(Object o)
• Returns true if this list contains the specified element.
Object get(int index)
• Returns the element at the specified position in this list.
int indexOf(Object o)
• Returns the index in this list of the first occurrence of the
specified element, or -1 if the List does not contain the element.
int lastIndexOf(Object o)
• Returns the index in this list of the last occurrence of the
specified element, or -1.
void ensureCapacity(int minCapacity)
• Increases the capacity of this ArrayList instance, if necessary,
to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
Object set(int index, Object element)
• Replaces the element at the specified position in this list with
the specified element.
• Throws IndexOutOfBoundsException if the specified index is
out of range (index < 0 || index >= size()).
boolean isEmpty()
• used to check whether the Arraylist is empty or not?
void trimToSize()
• reduces the size of an arraylist to the number of elements
present in the arraylist.
• import java.util.*;
• class TestCollection2
• {
• public static void main(String args[])
• {
• ArrayList<String> al=new ArrayList<String>();
• al.add("Ravi");
• al.add("Vijay");
• al.add("Ravi");
• al.add("Ajay");
• for(String obj:al)
• System.out.println(obj);
• }
• }
A2003822018_21789_17_2018_09. ArrayList.ppt

More Related Content

PPT
L11 array list
PPTX
ArrayList class and useful methods.pptx
PPT
PPTX
Collection Framework-1.pptx
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPT
Collection Framework.power point presentation.......
PPTX
Collections lecture 35 40
PPTX
Array list(1)
L11 array list
ArrayList class and useful methods.pptx
Collection Framework-1.pptx
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Collection Framework.power point presentation.......
Collections lecture 35 40
Array list(1)

Similar to A2003822018_21789_17_2018_09. ArrayList.ppt (20)

PPTX
Collections in object oriented programming
DOCX
C# Collection classes
PDF
javacollections.pdf
PPTX
collection framework.pptx
DOC
Advanced core java
PDF
Array list (java platform se 8 )
PDF
java I am trying to run my code but it is not letting me .pdf
DOCX
Collections framework
PPT
Java10 Collections and Information
PDF
Java collections
PPTX
Collections - Lists & sets
PDF
I need to implement the following ArrayList methods in javapublic.pdf
PPTX
Arrays and Strings engineering education
PPT
12_-_Collections_Framework
PDF
Collections and generics
DOCX
ArrayList.docx
PPT
description of Collections, seaching & Sorting
PDF
List classes
PDF
5 collection framework
DOC
Array properties
Collections in object oriented programming
C# Collection classes
javacollections.pdf
collection framework.pptx
Advanced core java
Array list (java platform se 8 )
java I am trying to run my code but it is not letting me .pdf
Collections framework
Java10 Collections and Information
Java collections
Collections - Lists & sets
I need to implement the following ArrayList methods in javapublic.pdf
Arrays and Strings engineering education
12_-_Collections_Framework
Collections and generics
ArrayList.docx
description of Collections, seaching & Sorting
List classes
5 collection framework
Array properties
Ad

More from RithwikRanjan (13)

PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
PPTX
INTERNSHIP PRESENTATION.pptx
PPTX
internship.pptx
PPTX
AM.pptx
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
PPTX
sam_technical_seminar.pptx
PPTX
sam_report.pptx
PPTX
TQM-Module 5.pptx
PPTX
CIM MODULE 2.pptx
PPT
0_A1590026209_21789_20_2018_0 Lecture.ppt
PPT
A457405934_21789_26_2018_Inheritance.ppt
PPT
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
PPT
A1869984431_21789_28_2018_Abstract Class.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
INTERNSHIP PRESENTATION.pptx
internship.pptx
AM.pptx
4_A1208223655_21789_2_2018_04. Operators.ppt
sam_technical_seminar.pptx
sam_report.pptx
TQM-Module 5.pptx
CIM MODULE 2.pptx
0_A1590026209_21789_20_2018_0 Lecture.ppt
A457405934_21789_26_2018_Inheritance.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
A1869984431_21789_28_2018_Abstract Class.ppt
Ad

Recently uploaded (20)

PPTX
PPT ARIEZ'S TOUR FINAL Pulogebang on.pptx
PDF
Uniqaya Night Cream For All Skin Type With Natural Ingredients
PPTX
Expert Custom Tailoring Services for All Needs.pptx
PPTX
Ethical_Clothing_Presentation for everyone
PDF
Jake Seal Top 5 Career Milestones in the Film Industry.pdf
PDF
Maslow's Hierarchy Isn't a Ladder — It's a Loop (by Meenakshi Khakat)
PDF
5 Popular Designs of Kashmir Silk Handmade Carpets.pdf
PPTX
Too Lucky to Be a Victim., an essay on social media
PDF
Modern Furniture Trends & Home Interior Decoration
PDF
John Baptist Scalabrini Novena - Fr Luciano Final eddition.pdf
PDF
Renovating a Midwest Ranch Rustic Modern Charm with Carved Doors
PDF
PrayerPetals- Where Faith and Womanhood Flourish Together.pdf
PPTX
Hyperlipidemia current medication with lifestyle.
PDF
4 Mindset Shifts to Turn Frustration into Focus
PPTX
GEE2-BEED-II: Ibaloi Indigenous People .pptx
PDF
Special Needs Dogs – How to Care for Them with Love .pdf
PPTX
The-World-of-Fashion-Trends-and-Innovation-2025.pptx
DOCX
Free Pomodoro Tecnique Effect Guide -25mint - pomodorotimer.com.au
PPTX
hhhsyysvwvsydxuguduehshsvdhvdjbuwbjwjdbwubs
DOC
学历学位硕士ACAP毕业证,澳大利亚凯斯林大学毕业证留学未毕业
PPT ARIEZ'S TOUR FINAL Pulogebang on.pptx
Uniqaya Night Cream For All Skin Type With Natural Ingredients
Expert Custom Tailoring Services for All Needs.pptx
Ethical_Clothing_Presentation for everyone
Jake Seal Top 5 Career Milestones in the Film Industry.pdf
Maslow's Hierarchy Isn't a Ladder — It's a Loop (by Meenakshi Khakat)
5 Popular Designs of Kashmir Silk Handmade Carpets.pdf
Too Lucky to Be a Victim., an essay on social media
Modern Furniture Trends & Home Interior Decoration
John Baptist Scalabrini Novena - Fr Luciano Final eddition.pdf
Renovating a Midwest Ranch Rustic Modern Charm with Carved Doors
PrayerPetals- Where Faith and Womanhood Flourish Together.pdf
Hyperlipidemia current medication with lifestyle.
4 Mindset Shifts to Turn Frustration into Focus
GEE2-BEED-II: Ibaloi Indigenous People .pptx
Special Needs Dogs – How to Care for Them with Love .pdf
The-World-of-Fashion-Trends-and-Innovation-2025.pptx
Free Pomodoro Tecnique Effect Guide -25mint - pomodorotimer.com.au
hhhsyysvwvsydxuguduehshsvdhvdjbuwbjwjdbwubs
学历学位硕士ACAP毕业证,澳大利亚凯斯林大学毕业证留学未毕业

A2003822018_21789_17_2018_09. ArrayList.ppt

  • 2. Introduction • Standard Java arrays are of a fixed length. • After arrays are created, they cannot grow or shrink, which means we can not resize Arrays. • Arrays are useful when we know in advance how many elements the array is to hold. • ArrayList is a collection which provides the implementation of resizable array.
  • 3. ArrayList • The ArrayList class extends AbstractList and implements the List interface. • Defined in java.util package. • ArrayList supports dynamic arrays that can grow as needed. • Array lists are created with an initial size. • When this size is exceeded, the collection is automatically enlarged. • When objects are removed, the array may be shrunk.
  • 4. ArrayList Constructors • T he ArrayList class supports three constructors.  ArrayList() : creates an empty array list with an initial capacity sufficient to hold 10 elements.  ArrayList(int capacity) : creates an array list that has the specified initial capacity.  ArrayList(Collection c) : creates an array list that is initialized with the elements of the collection c.
  • 5. Methods of ArrayList boolean add(Object o) • Appends the specified element to the end of this list. void add(int index, Object element) • Inserts the specified element at the specified position index in this list. • Throws IndexOutOfBoundsException if the specified index is out of range.
  • 6. boolean addAll(Collection c ) • Appends all of the elements in the specified collection to the end of this list. • Throws NullPointerException if the specified collection is null. boolean addAll(int index, Collection c ) • Inserts all of the elements of the specified collection into this list, starting at the specified position. • Throws NullPointerException if the specified collection is null.
  • 7. void clear() • Removes all of the elements from this list. Object remove(int index) • Removes the element at the specified position in this list. boolean remove(Object o) • Removes the first specified object present in the arraylist. int size() • Returns the number of elements in the list.
  • 8. boolean contains(Object o) • Returns true if this list contains the specified element. Object get(int index) • Returns the element at the specified position in this list. int indexOf(Object o) • Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain the element. int lastIndexOf(Object o) • Returns the index in this list of the last occurrence of the specified element, or -1.
  • 9. void ensureCapacity(int minCapacity) • Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Object set(int index, Object element) • Replaces the element at the specified position in this list with the specified element. • Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).
  • 10. boolean isEmpty() • used to check whether the Arraylist is empty or not? void trimToSize() • reduces the size of an arraylist to the number of elements present in the arraylist.
  • 11. • import java.util.*; • class TestCollection2 • { • public static void main(String args[]) • { • ArrayList<String> al=new ArrayList<String>(); • al.add("Ravi"); • al.add("Vijay"); • al.add("Ravi"); • al.add("Ajay"); • for(String obj:al) • System.out.println(obj); • } • }

Editor's Notes

  • #12: TestArrayList.java, TestCollection1.java