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

ARRAYLIST

This Java program demonstrates the use of ArrayList for managing a list of names, including operations like adding, modifying, and removing elements. It emphasizes the importance of ArrayList as a dynamic alternative to standard arrays, showcasing structured and reusable code practices. The program does not require user input, focusing instead on hardcoded names for efficient data manipulation.

Uploaded by

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

ARRAYLIST

This Java program demonstrates the use of ArrayList for managing a list of names, including operations like adding, modifying, and removing elements. It emphasizes the importance of ArrayList as a dynamic alternative to standard arrays, showcasing structured and reusable code practices. The program does not require user input, focusing instead on hardcoded names for efficient data manipulation.

Uploaded by

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

import java.util.

ArrayList; • Importance: Provides a visual separator for this section in the


program output.
• Imports the ArrayList class from Java’s built-in java.util package.

• Importance: Allows us to use ArrayList, a resizable array alternative


to standard arrays. names.add("Jenny");

names.add("Lyne");

public class Name { names.add("Kerstine");

• Declares a public class named Name. names.add("Eld");

• Importance: Java requires all code to be inside a class for execution. names.add("Precious");

• Adds five names to the ArrayList.

public static void main(String[] args) { • Importance: Stores the initial names in a dynamic list for further
operations.
• Defines the main() method, the entry point of the Java program.

• Importance: Java programs start execution from this method.


printList(names);

• Calls printList(names), a method to display the list contents.


ArrayList<String> names = new ArrayList<>();
• Importance: Prints the names added, ensuring they are stored
• Creates an ArrayList named names that stores String values.
correctly.
• Importance: Allows dynamic storage and manipulation of a list of
names.
// Changing an Element

• Comment indicating that an element will be modified.


// Adding Elements
• Importance: Improves readability by showing the purpose of the
• Comment indicating that the next lines add elements to the list.
next block of code.
• Importance: Helps in code readability and organization.

System.out.println("Changing an Element:");
System.out.println("Adding Elements:");
• Prints "Changing an Element:" to the console.
• Prints "Adding Elements:" to the console.
• Importance: Notifies the user about the modification operation.
• Importance: Demonstrates how to remove an element from an
ArrayList.
names.set(1, "Lendrix");

• Replaces the name at index 1 (Lyne) with "Lendrix".


printList(names);
• Importance: Demonstrates how to modify elements in an ArrayList.
• Displays the list after removing "Jenny".

• Importance: Confirms that the element was successfully removed.


printList(names);

• Displays the updated list after modification.


// Displays the updated list after changes
• Importance: Confirms the change visually.
• Comment indicating that the final list will be displayed.

• Importance: Improves code organization and readability.


// Removing an Element

• Comment indicating that an element will be removed.


System.out.println("Displays the updated list after changes:");
• Importance: Clearly separates the next operation.
• Prints "Displays the updated list after changes:" to the console.

• Importance: Clearly marks the final output section.


System.out.println("Removing an Element:");

• Prints "Removing an Element:" to the console.


printList(names);
• Importance: Notifies the user about the deletion operation.
• Displays the final modified list.

• Importance: Confirms that all operations were successfully


names.set(1, "Lyne");
executed.
• Reverts "Lendrix" back to "Lyne".

• Importance: Ensures "Lyne" appears correctly in the final output.


private static void printList(ArrayList<String> list) {

• Defines a private method printList() that takes an ArrayList<String>


names.remove(0); as input.

• Removes the first element ("Jenny") from the list. • Importance: Avoids repetitive code by centralizing list printing in
one method.
list.forEach(name -> System.out.print(name + " ")); • Why it’s used: We need a list to store and manipulate names
efficiently.
• Uses Java’s forEach method with a lambda expression to print all
names in one line. 2. Scanner is for User Input (Not Needed Here)

• Importance: Provides a concise and modern way to iterate over the • Scanner is used when you need to read user input from the
list. keyboard.

• Example:

System.out.println("\n"); Scanner sc = new Scanner(System.in);

• Prints a newline (\n) to separate list output sections. System.out.print("Enter a name: ");

• Importance: Improves readability of the program output. String name = sc.nextLine();

This Java program demonstrates four key ArrayList operations: • Why it’s NOT used: Your program doesn’t ask users for input; names
are hardcoded into the ArrayList.
1. Adding elements (add())
3. ArrayList is More Flexible than Scanner
2. Modifying an element (set())
Dynamic Resizing: ArrayList can grow or shrink as needed, unlike arrays.
3. Removing an element (remove())
Easy Element Modification: You can replace, remove, and reorder
4. Displaying the list (using forEach inside printList()) elements easily.
Why is this important? Efficient for Storing and Processing Data: It is not tied to user input like
Scanner.
• ArrayList is a dynamic alternative to arrays in Java.

• The code is structured, reusable, and efficient.

• It follows best practices like method reuse (printList()), clear


sectioning (comments & println), and modern Java syntax (forEach).

Why use ArrayList instead of Scanner?

ArrayList and Scanner serve different purposes in Java. Here’s why ArrayList
is used in your code instead of Scanner: Conclusion

1. ArrayList is for Storing and Managing Data We use ArrayList here because the program is focused on managing a list of
names, not reading user input. If the program required users to type names,
• ArrayList<String> names = new ArrayList<>(); we would use both Scanner (for input) and ArrayList (for storage).
• Purpose: Stores multiple names dynamically and allows easy
modifications (add, remove, update).

You might also like