[go: up one dir, main page]

0% found this document useful (0 votes)
17 views10 pages

ArrayList

The document provides an overview of ArrayList in Java, highlighting its ability to create resizable arrays compared to standard fixed-length arrays. It details the constructors available for ArrayList, various methods for adding, removing, and accessing elements, and includes a practical exercise to implement an Employee storage system using ArrayList. Key functionalities such as dynamic resizing and element management are emphasized throughout the text.

Uploaded by

Eswar Adithya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

ArrayList

The document provides an overview of ArrayList in Java, highlighting its ability to create resizable arrays compared to standard fixed-length arrays. It details the constructors available for ArrayList, various methods for adding, removing, and accessing elements, and includes a practical exercise to implement an Employee storage system using ArrayList. Key functionalities such as dynamic resizing and element management are emphasized throughout the text.

Uploaded by

Eswar Adithya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

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.
Let’s Do Something…

Write a program to store the Employee objects in an arraylist


such that each employee is having name, emp_id and salary
attributes.

Implement a method getEmployee(String name) and display


the emp_id and salary of that Employee.

You might also like