ArrayList Lecture - Google Docs (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

‭Lecture Plan: Introduction to ArrayList in Java‬

‭1. Introduction to ArrayList‬

‭●‬ W
‭ hat is an ArrayList?‬
java.util‬
‭An‬‭ArrayList‬‭is a part of Java's‬‭Collection Framework‬‭,‬‭located in the‬‭
‭package.‬
‭It is a‬‭dynamic array‬‭that can grow and shrink as‬‭needed. Unlike arrays, its size is not‬
‭fixed.‬

‭●‬ ‭Why use ArrayList?‬

‭‬ D
○ ‭ ynamic resizing.‬
‭○‬ ‭Easy to add, remove, and manipulate elements.‬
‭○‬ ‭Built-in methods for common tasks.‬

‭Declaration:‬

‭ArrayList<Type> list = new ArrayList<>();‬

‭2. Common Methods of ArrayList (with Examples)‬

add()‬
‭1.‬ ‭Adding Elements:‬‭

‭○‬ ‭Adds an element to the ArrayList.‬

‭Example:‬

‭ArrayList<String> cities = new ArrayList<>();‬


‭ ities.add("New York");‬
c
‭cities.add("London");‬
‭System.out.println(cities); // Output: [New York, London]‬
get()‬
‭2.‬ ‭Accessing Elements:‬‭

‭○‬ ‭Retrieves an element at a specific index.‬

‭Example:‬

‭System.out.println(cities.get(0)); // Output: New York‬

set()‬
‭3.‬ ‭Updating Elements:‬‭

‭○‬ ‭Replaces the element at a specific index.‬

‭Example:‬

c‭ ities.set(1, "Tokyo");‬
‭System.out.println(cities); // Output: [New York, Tokyo]‬

remove()‬
‭4.‬ ‭Removing Elements:‬‭

‭○‬ ‭Removes an element by index or value.‬

‭Example:‬

‭ ities.remove("Tokyo");‬
c
‭System.out.println(cities); // Output: [New York]‬

size()‬
‭5.‬ ‭Checking Size:‬‭

‭○‬ ‭Returns the number of elements in the ArrayList.‬

‭Example:‬

‭System.out.println(cities.size()); // Output: 1‬
isEmpty()‬
‭6.‬ ‭Checking if Empty:‬‭

true‬‭if the ArrayList is empty.‬


‭○‬ ‭Returns‬‭

‭Example:‬

‭System.out.println(cities.isEmpty()); // Output: false‬

clear()‬
‭7.‬ ‭Clearing the List:‬‭

‭○‬ ‭Removes all elements from the ArrayList.‬

‭Example:‬

c‭ ities.clear();‬
‭System.out.println(cities); // Output: []‬

contains()‬
‭8.‬ ‭Checking for an Element:‬‭

‭○‬ ‭Checks if a specific element exists in the ArrayList.‬

‭Example:‬

‭System.out.println(cities.contains("New York")); // Output: false‬


‭9.‬ ‭Looping Through the ArrayList:‬

‭ sing a‬‭
U for‬‭loop:‬
‭for (int i = 0; i < cities.size(); i++) {‬
‭System.out.println(cities.get(i));‬
‭}‬

‭ sing an enhanced‬‭
U for‬‭loop:‬
‭for (String city : cities) {‬
‭System.out.println(city);‬
‭}‬
‭1. Compare Two ArrayLists‬

‭ ou can use the‬‭


Y equals()‬‭method to compare two‬‭
ArrayList‬‭objects. It checks if the two‬
‭lists have the same elements in the same order.‬

‭Example:‬

‭import java.util.ArrayList;‬

‭public class CompareArrayLists {‬


‭public static void main(String[] args) {‬
‭ArrayList<String> list1 = new ArrayList<>();‬
‭list1.add("Apple");‬
‭list1.add("Banana");‬
‭list1.add("Cherry");‬

‭ rrayList<String> list2 = new ArrayList<>();‬


A
‭list2.add("Apple");‬
‭list2.add("Banana");‬
‭list2.add("Cherry");‬

‭System.out.println(list1.equals(list2)); // Output: true‬


‭}‬
‭}‬

‭2. Compare and Sort Elements‬

Collections.sort()‬‭method sorts the elements of‬‭an‬‭


‭ he‬‭
T ArrayList‬ ‭. To compare‬
‭elements for sorting, you can use natural ordering or a custom comparator.‬
‭Example: Natural Ordering‬

i‭mport java.util.ArrayList;‬
‭import java.util.Collections;‬

‭public class SortArrayList {‬


‭public static void main(String[] args) {‬
‭ArrayList<Integer> numbers = new ArrayList<>();‬
‭numbers.add(45);‬
‭numbers.add(12);‬
‭numbers.add(78);‬
‭numbers.add(33);‬

‭ ollections.sort(numbers); // Sort in ascending order‬


C
‭System.out.println(numbers); // Output: [12, 33, 45, 78]‬
‭}‬
‭}‬

‭Example: Custom Comparator‬

i‭mport java.util.ArrayList;‬
‭import java.util.Collections;‬
‭import java.util.Comparator;‬

‭public class CustomSortArrayList {‬


‭public static void main(String[] args) {‬
‭ArrayList<String> names = new ArrayList<>();‬
‭names.add("John");‬
‭names.add("Alice");‬
‭names.add("Bob");‬

/‭/ Sort in descending order‬


‭Collections.sort(names, Comparator.reverseOrder());‬
‭System.out.println(names); // Output: [John, Bob, Alice]‬
‭}‬
‭}‬
‭3. Compare Each Element‬

ArrayLists‬‭to compare their‬‭elements.‬


‭You can loop through two‬‭

‭Example:‬

‭import java.util.ArrayList;‬

‭public class ElementComparison {‬


‭public static void main(String[] args) {‬
‭ArrayList<Integer> list1 = new ArrayList<>();‬
‭list1.add(1);‬
‭list1.add(2);‬
‭list1.add(3);‬

‭ rrayList<Integer> list2 = new ArrayList<>();‬


A
‭list2.add(1);‬
‭list2.add(2);‬
‭list2.add(4);‬

‭for (int i = 0; i < list1.size(); i++) {‬


‭if (list1.get(i).equals(list2.get(i))) {‬
‭System.out.println("Element " + i + " is the same: " + list1.get(i));‬
‭} else {‬
‭System.out.println("Element " + i + " is different: " + list1.get(i) + " vs " + list2.get(i));‬
‭}‬
‭}‬
‭}‬
‭}‬
‭4. Find Maximum and Minimum Using Comparator‬

Collections.max()‬‭and‬‭
‭You can use the‬‭ Collections.min()‬‭methods to find the largest‬
ArrayList‬
‭and smallest elements in an‬‭ ‭.‬

‭Example:‬

i‭mport java.util.ArrayList;‬
‭import java.util.Collections;‬

‭public class MinMaxExample {‬


‭public static void main(String[] args) {‬
‭ArrayList<Integer> numbers = new ArrayList<>();‬
‭numbers.add(10);‬
‭numbers.add(20);‬
‭numbers.add(5);‬
‭numbers.add(15);‬

i‭nt max = Collections.max(numbers);‬


‭int min = Collections.min(numbers);‬

‭ ystem.out.println("Maximum: " + max); // Output: Maximum: 20‬


S
‭System.out.println("Minimum: " + min); // Output: Minimum: 5‬
‭}‬
‭}‬
‭5. Compare Subsets of an ArrayList‬

containsAll()‬‭method to check if one‬‭list contains all elements of another‬


‭ ou can use the‬‭
Y
‭list.‬

‭Example:‬

‭import java.util.ArrayList;‬

‭public class SubsetComparison {‬


‭public static void main(String[] args) {‬
‭ArrayList<String> list1 = new ArrayList<>();‬
‭list1.add("Apple");‬
‭list1.add("Banana");‬
‭list1.add("Cherry");‬

‭ rrayList<String> list2 = new ArrayList<>();‬


A
‭list2.add("Apple");‬
‭list2.add("Cherry");‬

‭System.out.println(list1.containsAll(list2)); // Output: true‬


‭}‬
‭}‬
‭Summary:‬

‭Method‬ ‭Usage‬ ‭Description‬


‭add(element)‬ ‭list.add("Apple");‬ ‭Adds an element to the end of the ArrayList.‬
I‭nserts an element at a specific index, shifting‬
‭add(index, element)‬ ‭list.add(1, "Orange");‬ ‭subsequent elements.‬
‭get(index)‬ ‭list.get(0);‬ ‭Retrieves the element at the specified index.‬
‭ eplaces the element at the specified index with a new‬
R
‭set(index, element)‬ ‭list.set(1, "Grapes");‬ ‭value.‬
‭remove(index)‬ ‭list.remove(2);‬ ‭Removes the element at the specified index.‬
‭ emoves the first occurrence of the specified element‬
R
‭remove(object)‬ ‭list.remove("Banana");‬ ‭from the ArrayList.‬
‭size()‬ ‭list.size();‬ ‭Returns the number of elements in the ArrayList.‬
‭isEmpty()‬ ‭list.isEmpty();‬ ‭Checks if the ArrayList is empty (returns true or false).‬
‭ emoves all elements from the ArrayList, leaving it‬
R
‭clear()‬ ‭list.clear();‬ ‭empty.‬
‭ hecks if the specified element exists in the ArrayList‬
C
‭contains(element)‬ ‭list.contains("Apple");‬ ‭(returns true/false).‬
‭ ompares two ArrayLists for equality (order and‬
C
‭equals(object)‬ ‭list1.equals(list2);‬ ‭elements).‬
c‭ ontainsAll(collectio‬ ‭ hecks if the ArrayList contains all elements of another‬
C
‭n)‬ ‭list1.containsAll(list2);‬ ‭collection.‬
‭Collections.sort()‬ ‭Collections.sort(list);‬ ‭Sorts the elements in natural ascending order.‬
‭Collections.sort(list,‬
‭ ollections.sort(list,‬ C
C ‭ omparator.reverseOrd‬ S
‭ orts the ArrayList using a custom comparator (e.g.,‬
‭Comparator)‬ ‭er());‬ ‭descending order).‬
‭Collections.max()‬ ‭Collections.max(list);‬ ‭Finds and returns the largest element in the ArrayList.‬
‭Collections.min()‬ ‭Collections.min(list);‬ ‭Finds and returns the smallest element in the ArrayList.‬
f‭or (int i = 0; i <‬
‭for loop‬ ‭list.size(); i++) {}‬ ‭Iterates through the ArrayList using a traditional for loop.‬
‭Enhanced for loop‬ ‭for (String item : list) {}‬ ‭Iterates through the ArrayList using a simplified loop.‬
‭3. Scenario-Based Example‬

‭Task‬‭:‬‭Student Marks Management‬

‭‬ P
● ‭ roblem: Manage the marks of students in a class using an ArrayList.‬
‭●‬ ‭Operations:‬
‭1.‬ ‭Add marks of students.‬
‭2.‬ ‭Display all marks.‬
‭3.‬ ‭Update a specific student's mark.‬
‭4.‬ ‭Remove a mark based on index.‬
‭5.‬ ‭Calculate the average of all marks.‬
‭Code Example:‬

‭import java.util.ArrayList;‬

‭public class StudentMarks {‬


‭public static void main(String[] args) {‬
‭ArrayList<Integer> marks = new ArrayList<>();‬

/‭/ Adding marks‬


‭marks.add(85);‬
‭marks.add(90);‬
‭marks.add(78);‬
‭marks.add(88);‬

/‭/ Displaying marks‬


‭System.out.println("Marks: " + marks);‬

/‭/ Updating a mark‬


‭marks.set(2, 80);‬
‭System.out.println("Updated Marks: " + marks);‬

/‭/ Removing a mark‬


‭marks.remove(1);‬
‭System.out.println("After Removal: " + marks);‬

/‭/ Calculating the average‬


‭int total = 0;‬
‭for (int mark : marks) {‬
‭total += mark;‬
‭}‬
‭double average = (double) total / marks.size();‬
‭System.out.println("Average Marks: " + average);‬
‭}‬
‭}‬
‭Scenario-Based Problem:‬

‭ roblem Statement:‬‭You are organizing a coding competition‬‭where participants are ranked‬


P
‭based on their scores. Create a program using an‬‭ArrayList‬‭to manage the scores of‬
‭participants. Implement the following features:‬

‭ .‬
1 ‭ dd scores of participants to the list.‬
A
‭2.‬ ‭Sort the scores in descending order.‬
‭3.‬ ‭Find the highest and lowest scores in the competition.‬
‭4.‬ ‭Compare the scores list with another list of predefined scores to check if they match.‬

[85, 92,‬‭
‭Inputs to Provide:‬‭Participants' scores:‬‭ 78, 90, 88]‬
[92, 85, 78, 90,‬‭
‭Predefined scores for comparison:‬‭ 88]‬

‭Expected Output:‬

‭Original Scores: [85, 92, 78, 90, 88]‬

‭Sorted Scores (Descending): [92, 90, 88, 85, 78]‬

‭ ighest Score: 92‬


H
‭Lowest Score: 78‬

‭Do scores match with predefined scores? false‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy