Java Vectors
"Vector in Java provides a dynamic array that can hold objects of any type, offering flexibility in size and type. Learn how to declare and utilize Vectors, their key methods, and the differences from arrays. Explore examples and wrapper classes for handling objects in collections."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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. 2. Iterator framework.
Declaring VECTORS Creates a default vector, which has an initial size 10. Vector list = new Vector(); Creates a vector, whose initial capacity is specified by size. Vector list = new Vector(int 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 void add(int index, Object element) boolean add(Object o) Discription Inserts the specified element at the specified position in this Vector. Appends the specified element to the end of this Vector. Appends all of the elements in the specified Collection to the end 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 its size by one. Returns the current capacity of this vector. Removes all of the elements from this Vector. 1 2 boolean addAll(Collection c) 3 void addElement(Object obj) 4 int capacity() void clear() void copyInto(Object[] anArray) 5 6 Copies the components of this vector into the specified array. 7 Returns the component at the specified index. Object elementAt(int index) 8 Compares the specified Object with this Vector for equality. boolean equals(Object o) 9 Returns the first component (the item at index 0) of this vector. Object firstElement() 10
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 boolean char double float int long short byte Wrapper class Boolean Character Double Float Integer Long Short 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
Integer Class Methods: 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 longValue() : returns the value of this Integer as a long shortValue() : returns the value of this Integer as a short
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 Primitive integer to Integer object Integer IntVal = new Integer(i); Primitive float to Float object Float FloatVal = new Float(f); 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 str = Integer.toString(i);Primitive integer i to String str Primitive float f to String str Conversion Action str = Float.toString(f); str = Double.toString(d);Primitive double d to String str Primitive long l to String str str = Long.toString(l);
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 int i = Integer.parseInt(str); Converts String str into primitive integer i long l = Long.parseLong(str); Converts String str into primitive long l Conversion Action
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 ); } }