Java Program to Replace an Item Into Vector Collection at the Specified Index

You are currently viewing Java Program to Replace an Item Into Vector Collection at the Specified Index

Java program to replace an item into Vector collection at the specified index

// Java program to replace an item into Vector collection 
// at the specified index

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Vector vec = new Vector();

    vec.add(10);
    vec.add(20.5);
    vec.add(true);

    System.out.println("Vector Elements:");
    for (Object obj: vec) {
      System.out.println("  " + obj);
    }

    vec.set(1, "Hello World");

    System.out.println("Updated Vector Elements:");
    for (Object obj: vec) {
      System.out.println("  " + obj);
    }
  }
}

Output

Vector Elements:
  10
  20.5
  true
Updated Vector Elements:
  10
  Hello World
  true

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