0% found this document useful (0 votes)
4 views

Lecture#17 ArrayList

The document provides an overview of the Java ArrayList class, highlighting its resizable nature and its location within the java.util package. It contrasts ArrayLists with traditional arrays, emphasizing the ability to modify size and manage elements dynamically. Additionally, it covers methods for creating, adding, removing, and sorting elements in both single and 2D ArrayLists.

Uploaded by

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

Lecture#17 ArrayList

The document provides an overview of the Java ArrayList class, highlighting its resizable nature and its location within the java.util package. It contrasts ArrayLists with traditional arrays, emphasizing the ability to modify size and manage elements dynamically. Additionally, it covers methods for creating, adding, removing, and sorting elements in both single and 2D ArrayLists.

Uploaded by

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

OBJECT ORIENTED

PROGRAMMING
Java ArrayList
Java ArrayList

• The ArrayList class is a resizable array.


• It is found in the java.util package.

Difference b/w Array and ArrayList


The size of an array cannot be modified (if you want to add or remove
elements to/from an array, you have to create a new one). While
elements can be added and removed from an ArrayList whenever you
want. The syntax is also slightly different:
Java ArrayList
• The ArrayList class is a resizable array.
• It is found in the java.util package.

Difference b/w Array and ArrayList


The size of an array cannot be modified (if you want to add or
remove elements to/from an array, you have to create a new
one). While elements can be added and removed from an
ArrayList whenever you want. The syntax is also slightly different:
Creating an ArrayList
import java.util.ArrayList; // Import the ArrayList class

ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList


object

Adding item in an ArrayList


The ArrayList class has many useful methods. For example, to add elements to the list, use the add() method:
Adding at a specific position:
Accessing an Elements

Modifying an Elements
Remove an Elements

To remove all the elements in


the ArrayList
ArrayList Size

Loop Through an
ArrayList
Using For each Loop

Other Types of data in


ArrayList
Sort an ArrayList (1/2)
Another useful class in the java.util package is the Collections class, which include the sort()
method for sorting lists alphabetically or numerically:

Check for Z to A
Sort an ArrayList (2/2)
2D ArrayList
n Java, a 2D ArrayList is essentially an ArrayList of ArrayLists, where each "row" is an ArrayList that
can hold a list of elements.
This provides flexibility in terms of dynamic resizing for both rows and columns, unlike a 2D array which
has fixed dimensions.

Create 2D ArrayList
Create 2D ArrayList with
Loop
Conti... With previous program
Access and Modify 2D
ArrayList

Iterating Over a 2D ArrayList


Iterating Over a 2D ArrayList using For Each
Dynamically add and remove
element

You might also like