-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVectorExample.java
More file actions
86 lines (73 loc) · 3.18 KB
/
VectorExample.java
File metadata and controls
86 lines (73 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package DataStructures;
import java.util.*;
import java.util.Vector;
/*
This program is a simple example of how to use a vector in java
@author Anette Larios
@since 13.06.2023
@version 1.8.0
*/
public class VectorExample {
public static void main (String []args){
//Declarating a vector object called vector
Vector vector = new Vector(3,2);
//Printing the initial size of the vector(amount of elements)
System.out.println("Initial size: " + vector.size());
//Printing the initial capacity of the vector
System.out.println("Initial capacity: " + vector.capacity());
//Adding elements to the vector
vector.addElement(new Integer(1));
vector.addElement(new Integer(2));
vector.addElement(new Integer(3));
vector.addElement(new Integer(4));
//Printing capacity of the vector
System.out.println("Capacity after four additions: " + vector.capacity());
//Adding element to the vector
vector.addElement(new Double(5.45));
//Printing capacity of the vector
System.out.println("Current capacity: " + vector.capacity());
//Adding elements to the vector
vector.addElement(new Double(6.08));
vector.addElement(new Integer(7));
//Printing capacity of the vector
System.out.println("Current capacity: " + vector.capacity());
//Adding elements to the vector
vector.addElement(new Float(9.4));
vector.addElement(new Integer(10));
//Printing capacity of the vector
System.out.println("Current capacity: " + vector.capacity());
//Adding elements to the vector
vector.addElement(new Integer(11));
vector.addElement(new Integer(12));
//Printing first and last elements of the vector
System.out.println("First element: " + (Integer)vector.firstElement());
System.out.println("Last element: " + (Integer)vector.lastElement());
//Adding elements to the vector
vector.addElement(new Integer(11));
vector.addElement(new Integer(12));
//Printing first and last elements of the vector
System.out.println("First element: " + (Integer)vector.firstElement());
System.out.println("Last elenment: " + (Integer)vector.lastElement());
//Verifying if the vector contains number 3
if(vector.contains(new Integer(3)))
System.out.println("Vector contains 3.");
//Creating a enumeration object called 'vectorEnum' and assigning vector values.
Enumeration vectorEnum = vector.elements();
System.out.println("Elements in vector:");
//while vector still has elements, prints the next element.
while(vectorEnum.hasMoreElements())
System.out.print(vectorEnum.nextElement() + " ");
System.out.println();
}
public static class MapExample1 {
public static void main (String [] args){
Map m1 = new HashMap();
m1.put("Zara", "8");
m1.put("Alex", "31");
m1.put("Ayan", "12");
m1.put("Daisy", "14");
System.out.println("Map elements");
System.out.println("\t" + m1);
}
}
}