A Vector method clear() removes all of the elements from this Vector. A Vector will be empty once this method completes its execution.
1 package iogyan.example;
2
3 import java.util.Arrays;
4 import java.util.Vector;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10 Vector<Integer> vector = new Vector<>();
11 vector.addAll(Arrays.asList(10, 20, 30, 70, 30));
12 System.out.println("A vector elements : " + vector);
13
14
15 vector.clear();
16 System.out.println("A changed vector elements : " + vector);
17 }
18 }
In the above example, a Vector of type integer created and elements added using addAll() method by specifying a collection. A clear() method called that removes all of the elements of this vector. A vector object prints that does contains elements.
1 A vector elements : [10, 20, 30, 70, 30]
2 A changed vector elements : []
Vector method clear() with String
Vector method clear() with String
1 package iogyan.example;
2
3 import java.util.Arrays;
4 import java.util.Vector;
5
6 public class StringMain {
7
8 public static void main(String[] args) {
9
10 Vector<String> vector = new Vector<>(5);
11
12 vector.addAll(Arrays.asList("Welcome", "to", "IOGyan"));
13 System.out.println("A vector after adding components : " + vector);
14
15
16 vector.clear();
17 System.out.println("A changed vector elements : " + vector);
18 }
19 }
In the above example, a Vector of type String created and elements added using addAll() method by specifying a collection. A clear() method called that removes all of the elements of this vector. A vector object prints that does contains elements.
1 A vector after adding components : [Welcome, to, IOGyan]
2 A changed vector elements : []
Vector method clear() with custom object
Vector method clear() with custom object
1 package iogyan.example;
2
3 import iogyan.model.Employee;
4
5 import java.util.Arrays;
6 import java.util.Vector;
7
8 public class EmployeeMain {
9
10 public static void main(String[] args) {
11
12 Employee emp1 = new Employee(1, "Emp1","Tech", 1000);
13 Employee emp2 = new Employee(2, "Emp2","Admin", 5000);
14 Employee emp3 = new Employee(3, "Emp3","Admin", 12000);
15
16 Vector<Employee> vector = new Vector<>();
17
18 vector.addAll(Arrays.asList(emp1, emp3, emp2));
19 System.out.println("A vector elements : " + vector);
20
21
22 vector.clear();
23 System.out.println("A changed vector elements : " + vector);
24 }
25 }
In the above example, a Vector of type Employee created and elements added using addAll() method by specifying a collection. A clear() method called that removes all of the elements of this vector. A vector object prints that does contains elements.
1 A vector elements : [Emp1 Tech 1000.0, Emp3 Admin 12000.0, Emp2 Admin 5000.0]
2 A changed vector elements : []
Employee class
1 package iogyan.model;
2
3 public class Employee implements Comparable<Employee> {
4 private final int id;
5 private final String name;
6 private final String dept;
7 private final double salary;
8
9 public Employee(int id, String name, String dept, double salary) {
10 this.id = id;
11 this.name = name;
12 this.dept = dept;
13 this.salary = salary;
14 }
15
16 public int getId() {
17 return this.id;
18 }
19
20 public String getName() {
21 return this.name;
22 }
23
24 public String getDept() {
25 return this.dept;
26 }
27
28 public double getSalary() {
29 return this.salary;
30 }
31
32 @Override
33 public int hashCode() {
34 return super.hashCode();
35 }
36
37 @Override
38 public boolean equals(Object obj) {
39 return this.id == ((Employee)obj).id;
40 }
41
42 @Override
43 public String toString() {
44 return this.getName() + " " + this.getDept() + " " + this.getSalary();
45 }
46
47 @Override
48 public int compareTo(Employee o) {
49 return this.getId() - o.getId();
50 }
51 }
Related options for your search