Java Program to Add an Item Into Linkedlist Collection on the Specified Index

You are currently viewing Java Program to Add an Item Into Linkedlist Collection on the Specified Index

Java program to add an item into LinkedList collection on the specified index

// Java program to add an item into LinkedList collection 
// on the specified index

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList < String > countries = new LinkedList < String > ();

    countries.add("India");
    countries.add("USA");
    countries.add("UK");
    countries.add("CANADA");

    System.out.println("Country names: " + countries);

    countries.add(1, "CHINA");

    System.out.println("Country names: " + countries);
  }
}

Output

Country names: [India, USA, UK, CANADA]
Country names: [India, CHINA, USA, UK, CANADA]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply