You are on page 1of 22

Vectors

 Vector implements a DYNAMIC ARRAY.


 Vectors can hold objects of any type and any number.
 Vector class is contained in java.util package
 Vector is different from ARRAY in two ways:-
1. Vector is synchronized.
2. It contains many legacy methods that are not part of the collection
1. Enumeration.
framework.
2. Iterator
Declaring VECTORS
Creates a default vector, which has
Vector list = new Vector();
an initial size 10.

Creates a vector, whose initial


Vector list = new Vector(int size); capacity is specified by size.

Vector list = new Vector(int size, int incr);

Creates a vector, whose initial capacity is specified


by size and whose increment is specified by incr.
Vectors (Contd….)
 A vector can be declared without specifying any size explicitly.
 A vector without size can accommodate an unknown number of

items.
 Even when size is specified, this can be overlooked and a different
number of items may be put into the vector

In contrast, An ARRAY must have its size


specified.
Sr.no Method Discription

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) 

8 Object elementAt(int index)  Returns the component at the specified index.

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

 Most of the objects collection store objects and not


primitive types.
 Primitive types can be used as object when required.
 As they are objects, they can be stored in any of the
collection and pass this collection as parameters to
the methods.
Wrapper Class
 Wrapper classes are classes that allow primitive types to
be accessed as objects.

 Wrapper class is wrapper around a primitive data type


because they "wrap" the primitive data type into an object
of that class.
What is Wrapper Class?
 Each of Java's eight primitive data types has a class dedicated to it.
 They are one per primitive type.
 Wrapper classes make the primitive type data to act as objects.
Simple Type Wrapper class
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
short Short
byte Byte
Difference b/w Primitive Data Type and
Object of a Wrapper Class
 The following two statements illustrate the difference between a
primitive data type and an object of a wrapper class:
int x = 25;
Integer y = new Integer(33);
Clearly x and y differ by more than their values:
• x is a variable that holds a value;
• y is an object variable that holds a reference to an object.
So, the following statement using x and y as declared above is not allowed:
int z = x + y; // wrong!
 The data field in an Integer object is only accessible using
the methods of the Integer class.
 One such method is intValue() method which returns an int

equal to the value of the object, effectively "unwrapping" the


 Integer object:

int z = x + y.intValue(); // OK!


Boxing and Unboxing
 The wrapping is done by the compiler.
 if we use a primitive where an object is expected, the compiler boxes the primitive in its
wrapper class.
 Similarly, if we use a number object when a primitive is expected, the compiler un-boxes
the object.

 Example of boxing and unboxing:


Integer x, y; x = 12; y = 15; System.out.println(x+y);
 When x and y are assigned integer values, the compiler boxes the integers
because x and y are integer objects.
 In the println() statement, x and y are unboxed so that they can be added as
integers.
Integer Class
 Constructors:
 Integer(i) : constructs an Integer object equivalent to the integer i
 Integer(s) : constructs an Integer object equivalent to the string s

 Integer Class Methods:


 parseInt(s) : returns a signed decimal integer value equivalent to
string s
 toString(i) : returns a new String object representing the integer i
parseInt(s) returns a signed decimal integer value equivalent to string s
toString(i) returns a new String object representing the integer i
byteValue() returns the value of this Integer as a byte
doubleValue() returns the value of this Integer as an double
floatValue() returns the value of this Integer as a float
intValue() returns the value of this Integer as an int
shortValue() returns the value of this Integer as a short
longValue() returns the value of this Integer as a long
int compareTo(int i) Compares the numerical value of the invoking object with that of i. Returns 0 if the
values are equal. Returns a negative value if the invoking object has a lower value.
Returns a positive value if the invoking object has a greater value.

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

Character object being created.


 To obtain the char value contained in a Character object,

callncharValue( ), shown here:


char charValue( );
 It returns the encapsulated character.
Converting primitive numbers to Object numbers

using constructor methods 

Constructor calling Conversion Action


Integer IntVal = new Integer(i); Primitive integer to Integer object

Float FloatVal = new Float(f); Primitive float to Float object

Double DoubleVal = new Double(d); Primitive double to Double object

Long LongVal = new Long(l); Primitive long to Long object


Converting Object numbers to Primitive numbers

using typeValue() method 

Method calling Conversion Action


int i = IntVal.intValue(); Object to primitive integer
float f = FloatVal.floatValue(); Object to primitive float
double d = DoubleVal.doubleValue(); Object to primitive double
long l = LongVal.longValue(); Object to primitive long
Converting Numbers to Strings
using toString() method

Method calling Conversion Action


str = Integer.toString(i); Primitive integer i to String str
str = Float.toString(f); Primitive float f  to String str
str = Double.toString(d); Primitive double d to String str
str = Long.toString(l); Primitive long l to String str
Converting String Object in to Numeric Object

using static method ValueOf()

Method calling Conversion Action


IntVal = Integer.ValueOf(str); Convert String into Integer object
FloatVal = Float.ValueOf(str); Convert String into Float object
DoubleVal = Double.ValueOf(str); Convert String into Double object
LongVal = Long.ValueOf(str); Convert String into Long object
Converting Numeric Strings to Primitive numbers

using Parsing method

Method calling Conversion Action


int i = Integer.parseInt(str); Converts String str into primitive integer i
long l = Long.parseLong(str); Converts String str into primitive long l
public class WrapperDemo {  
    public static void main (String args[]){  
        Integer intObj1 = new Integer (25);  
        Integer intObj2 = new Integer ("25");  
        Integer intObj3= new Integer (35);  
        //compareTo demo  
        System.out.println("Comparing using compareTo Obj1 and Obj2: " + intObj1.compareTo(intObj2));  
        System.out.println("Comparing using compareTo Obj1 and Obj3: " + intObj1.compareTo(intObj3));  
          
        //Equals demo  
        System.out.println("Comparing using equals Obj1 and Obj2: " + intObj1.equals(intObj2));  
        System.out.println("Comparing using equals Obj1 and Obj3: " + intObj1.equals(intObj3));  
                  
        Float f1 = new Float("2.25f");  
        Float f2 = new Float("20.43f");  
        Float f3 = new Float(2.25f);  
        System.out.println("Comparing using compare f1 and f2: " +Float.compare(f1,f2));  
        System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3));  
          
        //Addition of Integer with Float  
        Float f = intObj1.floatValue() + f1;  
        System.out.println("Addition of intObj1 and f1: "+ intObj1 +"+" +f1+"=" +f );  
    }  }    

You might also like