Write A Program In Java To Demonstrate Use Of List. 1) Create Arraylist And Add Weekdays (In String Form) 2) Create Linkedlist And Add Months (In String Form) Display Both List.

0

Write A Program In Java To Demonstrate Use Of List. 

1) Create Arraylist And Add Weekdays (In String Form) 

2) Create Linkedlist And Add Months (In String Form)

 Display Both List.

Write a program in java to demonstrate use of list. 1) create arraylist and add weekdays (in string form) 2) create linkedlist and add months (in string form) display both list.

Before demonstrate the program we will understand the concept of list.


The List interface in Java provides a way to store an ordered collection of objects. It is a child interface of the Collection interface and can store duplicate values as well as null values. The List interface provides index-based methods to insert, update, delete and search elements. Some classes that implement the List interface include LinkedList, ArrayList, Vector and Stack.


Here's an example program in Java that demonstrates the use of a List to create an ArrayList and a LinkedList, and add weekdays and months to them respectively:


Program
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListExample {

    public static void main(String[] args) {

        // Create a new ArrayList of strings for weekdays
        List<String> weekdays = new ArrayList<>();

        // Add weekdays to the ArrayList
        weekdays.add("Monday");
        weekdays.add("Tuesday");
        weekdays.add("Wednesday");
        weekdays.add("Thursday");
        weekdays.add("Friday");
        weekdays.add("Saturday");
        weekdays.add("Sunday");

        // Print the contents of the ArrayList
        System.out.println("Weekdays: " + weekdays);

        // Create a new LinkedList of strings for months
        List<String> months = new LinkedList<>();

        // Add months to the LinkedList
        months.add("January");
        months.add("February");
        months.add("March");
        months.add("April");
        months.add("May");
        months.add("June");
        months.add("July");
        months.add("August");
        months.add("September");
        months.add("October");
        months.add("November");
        months.add("December");

        // Print the contents of the LinkedList
        System.out.println("Months: " + months);
    }
}

Output
Weekdays: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] Months: [January, February, March, April, May, June, July, August, September, October, November, December]

Explanation
In this program, we first import the ArrayList, LinkedList, and List classes from the java.util package.

Then, we create a new ArrayList of strings called weekdays.

Next, we add seven string elements to the weekdays ArrayList using the add() method.

We then print the contents of the weekdays ArrayList to the console using the println() method.

After that, we create a new LinkedList of strings called months.

We add twelve string elements to the months LinkedList using the add() method.

Finally, we print the contents of the months LinkedList to the console using the println() method.

❤️ I Hope This Helps You Understand, Click Here To Do More Exercise In Java Programming.


Tags

Post a Comment

0Comments
Post a Comment (0)