Question: public class ArrayListOperations { public static void main ( String [ ] args ) { / / a . Add and display the following elements

public class ArrayListOperations {
public static void main(String[] args){
// a. Add and display the following elements in the ArrayList object: Google, Apple, Amazon, Facebook, Twitter, and Oracle.
ArrayList companies = new ArrayList<>();
companies.add("Google");
companies.add("Apple");
companies.add("Amazon");
companies.add("Facebook");
companies.add("Twitter");
companies.add("Oracle");
System.out.println("Initial list of companies:");
for (String company : companies){
System.out.println(company);
}
// b. Update the 3rd element in the above list by Microsoft.
companies.set(2, "Microsoft"); // Arrays are zero-indexed, so 3rd element is at index 2
System.out.println("
List after updating the 3rd element to 'Microsoft':");
for (String company : companies){
System.out.println(company);
}
// c. Remove the last element from the list of objects.
companies.remove(companies.size()-1);
System.out.println("
List after removing the last element:");
for (String company : companies){
System.out.println(company);
}
// d. Check whether an element is available on the list.
String elementToCheck = "Facebook";
boolean isAvailable = companies.contains(elementToCheck);
System.out.println("
Is '"+ elementToCheck +"' available in the list? "+ isAvailable);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!