Question

Consider the follow ArrayLis in Javat: ArrayList centennials = new ArrayList<>(); centennials.add(LocalDate.of(1776, Month.JULY, 4)); centennials.add(LocalDate.of(1876, Month.JULY,...

Consider the follow ArrayLis in Javat:

ArrayList centennials = new ArrayList<>();

centennials.add(LocalDate.of(1776, Month.JULY, 4));

centennials.add(LocalDate.of(1876, Month.JULY, 4));

centennials.add(LocalDate.of(1900, Month.JULY, 4));

centennials.add(LocalDate.of(1976, Month.JULY, 4));

centennials.add(LocalDate.of(2076, Month.JULY, 4));

As you can observe, a java program author has mistakenly entered a 1900 item into the ArrayList. Without removing the associated centennials.add(...) source line, write the code to remove the errant entry. Print out the resulting ArrayList and size as follows:

Before removal:

07/04/1776

07/04/1876

07/04/1900

07/04/1976

07/04/2076

size = 5

After removal:

07/04/1776

07/04/1876

07/04/1976

07/04/2076

size = 4

Hint: you should use the DateTimeFormatter class for formatting

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.io.*;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;
public class arraylist {
public static void main(String[] args)
{
ArrayList centennials=new ArrayList<>();

//Adding data to Array list
centennials.add(LocalDate.of(1776, Month.JULY, 4));
centennials.add(LocalDate.of(1876, Month.JULY, 4));
centennials.add(LocalDate.of(1900, Month.JULY, 4));
centennials.add(LocalDate.of(1976, Month.JULY, 4));
centennials.add(LocalDate.of(2076, Month.JULY, 4));

System.out.println("Before removel:\n");
System.out.println(centennials); //To print the dates before removal of 1990
System.out.println("Size="+centennials.size()); //Find the size of the array list before removel of 1990
centennials.remove(2); //Remove the element at index '2'
System.out.println("After removal:\n");
System.out.println(centennials); //To print the dates after removal of 1990
System.out.println("Size="+centennials.size()); //Find the size of the array list after removal of 1990
}

}

Output:

<terminated> arraylist [Java Application] C:\Program Files Vava\jdk1.8.0_73\bin\javaw.exe (19-Nov-2019, 11:44:07 PM) Before r

Add a comment
Know the answer?
Add Answer to:
Consider the follow ArrayLis in Javat: ArrayList centennials = new ArrayList<>(); centennials.add(LocalDate.of(1776, Month.JULY, 4)); centennials.add(LocalDate.of(1876, Month.JULY,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT