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

Accessing ArrayList Java OOP

An ArrayList in Java is a resizable array found in the java.util package, allowing dynamic size adjustments for storing collections of objects. It supports methods for adding, accessing, modifying, and removing elements, making it ideal for object management in OOP. The document provides examples of declaring, accessing, and manipulating ArrayList elements using loops and methods.

Uploaded by

humaimaqadeer22
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)
3 views

Accessing ArrayList Java OOP

An ArrayList in Java is a resizable array found in the java.util package, allowing dynamic size adjustments for storing collections of objects. It supports methods for adding, accessing, modifying, and removing elements, making it ideal for object management in OOP. The document provides examples of declaring, accessing, and manipulating ArrayList elements using loops and methods.

Uploaded by

humaimaqadeer22
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/ 8

Accessing the ArrayList in OOP with

Java
• Presented by: [Your Name]
• Date: [Insert Date]
What is an ArrayList?
• - An ArrayList is a resizable array in Java.
• - Found in java.util package.
• - Unlike arrays, size can grow or shrink
dynamically.
• - Useful in OOP for storing collections of
objects.
Declaring an ArrayList
• import java.util.ArrayList;

• ArrayList<String> names = new


ArrayList<String>();

• - Use angle brackets <> to define the data


type.
• - Can store objects like String, Integer, or
custom classes.
Adding Elements
• names.add("Ali");
• names.add("Sara");
• names.add("Zara");

• - .add() method is used to insert elements.


Accessing Elements
• System.out.println(names.get(0)); // Output:
Ali

• - Use .get(index) to access elements.


• - Index starts from 0.
Using Loops to Access All Elements
• Using for loop:
• for(int i = 0; i < names.size(); i++) {
• System.out.println(names.get(i));
• }

• Using enhanced for loop:


• for(String name : names) {
• System.out.println(name);
• }
Modifying and Removing Elements
• Modify:
• names.set(1, "Ayesha"); // replaces "Sara"
with "Ayesha"

• Remove:
• names.remove(0); // removes "Ali"
Conclusion
• - ArrayList is a powerful, flexible way to
manage objects in Java.
• - Ideal for dynamic data handling in OOP.
• - Supports easy access, modification, and
removal of elements.

You might also like