Reputation: 11
I am a beginner in Java and I have to implement vector in java by using these methods addElement(), elementAt(), removeElement(), size()
and I don't know how I tried to write the code of addElement
first and I am getting this error :
Please I really need help with this
Here is the code :
import java.util.Vector;
class Vectimplement {
public static void main(String[] args) {
Vector v = new Vector();
v.add("element_1");
v.add("element_2");
v.add("element_3");
v.addElement("New Element");
System.out.println("Elements in Vector :");
for(int i=0; i < v.size(); i++){
System.out.println(v.get(i));
}
}
}
Upvotes: 0
Views: 3718
Reputation: 11
You can add values by accepting it from user:
public class Vectorexapp {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a,b;
String s;
double c;
System.out.println("Enter 2 integer values, 1 string and 1 float value:");
a=sc.nextInt();
b=sc.nextInt();
s=sc.next();
c=sc.nextDouble();
Vector v=new Vector();
v.add(0,a);
v.add(1,b);
v.add(2,s);
v.add(3,c);
System.out.println("vector values:"+v);
System.out.println("vector size:"+v.size());
System.out.println("Vector capacity:"+v.capacity());
}
}
output:
Enter 2 integer values, 1 string and 1 float value:
60
40
abcd
50.6
vector values:[60, 40, abcd, 50.6]
vector size:4
Vector capacity:10
Upvotes: 0
Reputation: 11
Vector to add element at specified position:
public class Vectorexapp {
public static void main(String[] args) {
Vector v=new Vector();
v.add(0,10);
v.add(1,20);
v.add(2,"HELLO");
v.add(3,40.5);
System.out.println("Value at index 2:" +v.get(1));
System.out.println("Values of vector:"+v);
v.clear();
System.out.println("After clear vector values:"+v);
}
}
output:
Value at index 2:20
Values of vector:[10, 20, HELLO, 40.5]
After clear vector values:[]
Upvotes: 0
Reputation: 11
Vector is a pre define class you can use it's method my creating it's object as follow,
To add values in vector use object.add() methods,
following are some methos:
public class Vectorexapp {
public static void main(String[] args) {
Vector v=new Vector();
v.add(10);
v.add(20);
v.add("HELLO");
v.add(40.5);
System.out.println("first element:"+v.firstElement());
System.out.println("last element:"+v.lastElement());
System.out.println("Values of vector:"+v);
}
}
Upvotes: 0
Reputation: 2515
After supressing warnings I was able to run program successfully :
package test;
import java.util.Vector;
class Vectimplement {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
@SuppressWarnings("rawtypes")
Vector v = new Vector();
v.add("element_1");
v.add("element_2");
v.add("element_3");
v.addElement("New Element");
System.out.println("Elements in Vector :");
for (int i = 0; i < v.size(); i++) {
System.out.println(v.get(i));
}
}
}
Console Output :
element_1
element_2
element_3
New Element
These warnings are generated because after Java6(or maybe 7) it was required to explicitly provide the "type" of Objects to be stored in containers. Hence, the best practice is to use :
Vector<String> v = new Vector<>();
Upvotes: 1
Reputation: 1282
Use generics to specify the type of objects you're going to put into the collection.
Vector<String> v = new Vector<>();
Upvotes: 3