- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace all the elements of a Vector with Collections.fill() in Java
All the elements of a vector can be replaced by a specific element using java.util.Collections.fill() method. This method requires two parameters i.e. the Vector and the element that replaces all the elements in the Vector. No value is returned by the Collections.fill() method.
A program that demonstrates this is given as follows:
Example
import java.util.Collections; import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(7); vec.add(4); vec.add(1); vec.add(2); vec.add(9); System.out.println("The Vector elements are: " + vec); Collections.fill(vec, 3); System.out.println("The Vector elements are: " + vec); } }
The output of the above program is as follows:
The Vector elements are: [7, 4, 1, 2, 9] The Vector elements are: [3, 3, 3, 3, 3]
Now let us understand the above program.
The Vector is created. Then Vector.add() is used to add the elements to the Vector. Collections.fill() method is used to replace all the elements of the Vector with element 3.
Then the Vector is displayed. A code snippet which demonstrates this is as follows:
Vector vec = new Vector(5); vec.add(7); vec.add(4); vec.add(1); vec.add(2); vec.add(9); System.out.println("The Vector elements are: " + vec); Collections.fill(vec, 3); System.out.println("The Vector elements are: " + vec);
- Related Articles
- Replace All Elements Of ArrayList with with Java Collections
- Replace all occurrences of specified element of ArrayList with Java Collections
- Reverse order of all elements of ArrayList with Java Collections
- How to replace one vector elements with another vector elements in R?
- Clear out all of the Vector elements in Java
- Shuffle elements of ArrayList with Java Collections
- Swap elements of ArrayList with Java collections
- Append all elements of another Collection to a Vector in Java
- Python Pandas - Replace all NaN elements in a DataFrame with 0s
- Replace an element at a specified index of the Vector in Java
- How to replace values in a vector with values in the same vector in R?
- Add elements at the middle of a Vector in Java
- Add elements at the end of a Vector in Java
- Copy Elements of One ArrayList to Another ArrayList with Java Collections Class
- Count frequencies of all elements in array in Python using collections module

Advertisements