Found 9326 Articles for Object Oriented Programming

Loop through the Vector elements using an Iterator in Java

Jai Janardhan
Updated on 30-Jun-2020 08:22:40

1K+ Views

An Iterator can be used to loop through the Vector elements. The method hasNext( ) returns true if there are more elements in the Vector and false otherwise. The method next( ) returns the next element in the Vector and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Iterator; import java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector();       vec.add(4);       vec.add(1);       vec.add(3);       vec.add(9);     ... Read More

Enumerate through the Vector elements in Java

Rishi Raj
Updated on 30-Jun-2020 08:25:07

560 Views

The Vector elements can be traversed using the Enumeration interface. The method hasMoreElements( ) returns true if there are more elements to be enumerated and false if there are no more elements to be enumerated. The method nextElement( ) returns the next object in the enumeration.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Enumeration; import java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector();       vec.add(7);       vec.add(3);       vec.add(5);       vec.add(9);       vec.add(2);     ... Read More

Get first and last elements from Vector in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

493 Views

The first element in the Vector can be obtained using the java.util.Vector.firstElement() method. The last element in the Vector can be obtained using the java.util.Vector.lastElement() method. Neither of these methods requires any parameters.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(); vec.add(4); vec.add(7); vec.add(2); vec.add(8); ... Read More

Set the size of a Vector in Java

Arushi
Updated on 30-Jul-2019 22:30:24

349 Views

The java.util.Vector.setSize() method can be used to set the size of a Vector in Java. If the new size that is set is greater than the old size of the Vector, then null values are added to the positions created. If the new size that is set is lesser than the old size of the Vector, then the elements at positions more than the new size are discarded.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = ... Read More

Replace all the elements of a Vector with Collections.fill() in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

450 Views

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 Live Demoimport 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); ... Read More

Search an element of Vector in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

492 Views

An element of a Vector can be searched using the method java.util.ArrayList.indexOf(). This method returns the index of the first occurrence of the element that is specified. If the element is not available in the Vector, then this method returns -1.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(4); vec.add(1); vec.add(7); ... Read More

Replace an element at a specified index of the Vector in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

512 Views

An element in a Vector can be replaced at a specified index using the java.util.Vector.set() method. This method has two parameters i.e the index at which the Vector element is to be replaced and the element that it should be replaced with. Vector.set() method returns the element that was at the position specified at the index previously.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(7); ... Read More

Removing Single Elements in a Vector in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

84 Views

A single element in the Vector can be removed using the java.util.Vector.remove() method. This method removes the element at the specified index in the Vector. It returns the element that was removed. Also after the element is removed from the specified index, the rest of the Vector elements are shifted to the left.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(4); ... Read More

Clear out all of the Vector elements in Java

Jai Janardhan
Updated on 30-Jun-2020 08:03:52

281 Views

A Vector can be cleared in Java using the method java.util.Vector.clear(). This method removes all the elements in the Vector. There are no parameters required by the Vector.clear() method and it returns no value.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector(5);       vec.add(4);       vec.add(1);       vec.add(3);       vec.add(9);       vec.add(6);       vec.add(2);       vec.add(8);       System.out.println("The Vector elements are: " ... Read More

Print a Vector in a comma-delimited list, in index order and surrounded by square brackets ([]) in Java

Arushi
Updated on 30-Jun-2020 08:04:49

332 Views

A Vector can be printed in a comma-delimited list, in index order and surrounded by square brackets ([]) by simply using System.out.println() along with the Vector object.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector(5);       vec.add(4);       vec.add(1);       vec.add(3);       vec.add(9);       vec.add(6);       System.out.println(vec);    } }The output of the above program is as follows −[4, 1, 3, 9, 6]Now let us understand ... Read More

Advertisements