0% found this document useful (0 votes)
130 views11 pages

Array List

Here are the steps to create a menu driven program to store and display student records using ArrayList: 1. Create a Student class with the given attributes. 2. Create an ArrayList to store Student objects. 3. Write menu options to add, display, search student records. 4. Get user input for option and process accordingly: - Add: Take input for a student and add to list - Display: Iterate list and print all students - Search: Take rollNo as input, search list and print if found 5. Run menu in loop until user chooses to exit This allows storing and retrieving any number of student records dynamically using the ArrayList collection.

Uploaded by

adarsh raj
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)
130 views11 pages

Array List

Here are the steps to create a menu driven program to store and display student records using ArrayList: 1. Create a Student class with the given attributes. 2. Create an ArrayList to store Student objects. 3. Write menu options to add, display, search student records. 4. Get user input for option and process accordingly: - Add: Take input for a student and add to list - Display: Iterate list and print all students - Search: Take rollNo as input, search list and print if found 5. Run menu in loop until user chooses to exit This allows storing and retrieving any number of student records dynamically using the ArrayList collection.

Uploaded by

adarsh raj
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/ 11

Programming in Java

Lecture 11: ArrayList

By
Arvind Kumar
Asst. Professor, LPU
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
• 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
• The 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

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


is out of range.
boolean add(Object o)
• Appends the specified element to the end of this list.

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.

Object clone()
• Returns a shallow copy of this 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.
Let’s Do It
• Create a class Student having attributes name, fatherName,
rollNo, section, college and address. Write a menu driven
program to store and display the records of any number of
Students.

You might also like