Vectors: Vector Class
Vectors: Vector Class
Vectors: Vector Class
items.
Even when size is specified, this can be overlooked and a different
number of items may be put into the vector
1 void add(int index, Object Inserts the specified element at the specified position in this
element) Vector.
2 boolean add(Object o) Appends the specified element to the end of this Vector.
Appends all of the elements in the specified Collection to the end
3 boolean addAll(Collection c) of this Vector, in the order that they are returned by the specified
Collection's Iterator.
Adds the specified component to the end of this vector, increasing
4 void addElement(Object obj)
its size by one.
5 int capacity() Returns the current capacity of this vector.
6 void clear() Removes all of the elements from this Vector.
7
void copyInto(Object[] Copies the components of this vector into the specified array.
anArray)
9 boolean equals(Object o) Compares the specified Object with this Vector for equality.
10 Object firstElement() Returns the first component (the item at index 0) of this vector.
Example
import java.util.*;
public class VectorDemo {
public static void main(String args[]) {
Vector v = new Vector(3, 2); // initial size is 3, increment is 2
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " +v.capacity());
v.addElement(new Integer(2));
System.out.println("Capacity after four additions: " + v.capacity());
v.addElement(new Double(5.45));
System.out.println("Current capacity: " + v.capacity());
System.out.println("First element: " + v.firstElement());
System.out.println("Last element: " +v.lastElement());
}
}
Wrapper Classes
static int compare(int Compares the values of num1 and num2. Returns 0 if the values are equal. Returns
num1, int num2) a negative value if num1 is less than num2. Returns a positive value if num1 is
greater than num2.
boolean equals(Object Returns true if the invoking Integer object is equivalent to intObj. Otherwise, it
intObj) returns false
valueOf (), toHexString(), toOctalString() and
toBinaryString() Methods:
This is another approach to create wrapper objects.
We can convert from binary or octal or hexadecimal before
assigning value to wrapper object using two argument
constructor.
Below program explains the method in details.
public class ValueOfDemo {
public static void main(String[] args) {
Integer intWrapper = Integer.valueOf("12345");
//Converting from binary to decimal.
Integer intWrapper2 = Integer.valueOf("11011", 2);
//Converting from hexadecimal to decimal.
Integer intWrapper3 = Integer.valueOf("D", 16);
System.out.println("Value of intWrapper Object: "+ intWrapper);
System.out.println("Value of intWrapper2 Object: "+ intWrapper2);
System.out.println("Value of intWrapper3 Object: "+ intWrapper3);
System.out.println("Hex value of intWrapper: " + Integer.toHexString(intWrapper));
System.out.println("Binary Value of intWrapper2: "+
Integer.toBinaryString(intWrapper2));
}}
Character Class
Character is a wrapper around a char.
The constructor for Character is :
Character(char ch)
Here, ch specifies the character that will be wrapped by the