Write a Java program that demonstrates the usage of ArrayList methods and operations. Your program should include the following:
Create an ArrayList called numbers
and add the following integers to it: 5, 10, 15, 20, 25.
Print the content of the ArrayList using the toString()
method.
Use the size()
method to determine and print the number of elements in the ArrayList.
Use the get(index)
method to print the element at index 2.
Use the contains(element)
method to check if the ArrayList contains the element 20. Print "Element 20 is present" if it is, and "Element 20 is not present" if it is not.
Use the indexOf(element)
method to find the index of the element 15. Print the index if it is found and -1 if it is not.
Create a new ArrayList called moreNumbers
and add the following integers to it: 30, 35, 40.
Use the addAll(collection)
method to add all elements from moreNumbers
to numbers
. Print the content of numbers
after the addition.
Use the remove(index)
method to remove the element at index 3 from numbers
. Print the content of numbers
after the removal.
Write the complete Java code for the program.
import java.util.ArrayList;
public class ArrayListMethodsDemo {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(10);
numbers.add(15);
numbers.add(20);
numbers.add(25);
System.out.println("ArrayList content: " + numbers.toString());
System.out.println("Number of elements: " + numbers.size());
System.out.println("Element at index 2: " + numbers.get(2));
if (numbers.contains(20)) {
System.out.println("Element 20 is present");
} else {
System.out.println("Element 20 is not present");
}
int index = numbers.indexOf(15);
if (index != -1) {
System.out.println("Index of element 15: " + index);
} else {
System.out.println("Element 15 is not present");
}
ArrayList<Integer> moreNumbers = new ArrayList<>();
moreNumbers.add(30);
moreNumbers.add(35);
moreNumbers.add(40);
numbers.addAll(moreNumbers);
System.out.println("ArrayList content after adding moreNumbers: " + numbers.toString());
numbers.remove(3);
System.out.println("ArrayList content after removing element at index 3: " + numbers.toString());
}
}
This program demonstrates various ArrayList methods and operations.
ArrayList<Integer>
called numbers
and add the given integers to it.toString()
method to print the content of numbers
.size()
method to determine and print the number of elements in numbers
.get(index)
method to print the element at index 2.contains(element)
method to check if numbers
contains the element 20, and print the appropriate message.indexOf(element)
method to find the index of the element 15, and print the index if found.ArrayList<Integer>
called moreNumbers
and add the given integers to it.addAll(collection)
method to add all elements from moreNumbers
to numbers
, and print the content of numbers
after the addition.remove(index)
method to remove the element at index 3 from numbers
, and print the content of numbers
after the removal.