Array, String and Vector
Array, String and Vector
Array, String and Vector
Example:
int number[ ];
float average[ ];
int[ ] counter;
float[ ] marks;
Java allows to create arrays using new
operator only , as shown below:
arrayname = new type[size];
Examples:
number = new int[5];
average = new float[10];
In this step values are put into the array created.
This process is known as initialization.
arrayname[subscript] = value;
Example:
number[0]=35;
number[1]=40;
.............
number[n]=19;
Java creates array starting with a subscript of
0(zero) and ends with a value less than the
size specified.
Trying to access an array beyond its
boundaries will generate an error message.
Array can also be initialized as the other
ordinary variables when they are declared.
Array initializer is a list of values separated by
commas and surrounded by curly braces.
All arrays store the allocated size in a variable
named length.
To access the length of the array a using
a.length.
Each dimension of the array is indexed from
zero to its maximum size minus one.
Example:
Example:
String firstName;
firstName = new String(“Anil”);
Array can be created and used that contain
strings.
Two useful methods for String objects are equals( ) and substring( ).
The equals( ) method is used for testing whether two Strings contain the same value.
The substring( ) method is used to obtain a selected portion of a String.
➢ StringBuffer( int size)It accepts an integer argument that explicitly sets the
size of the buffer.
StringBuffer s=new StringBuffer(20);
Method 2:
Syntax: Vector object= new Vector(int initialCapacity)
Example: Vector vec = new Vector(3);
It will create a Vector of an initial capacity of 3.
Method 3:
Syntax: Vector object= new vector(int initialcapacity, capacityIncrement)
Example: Vector vec= new Vector(4, 6)
Here we have provided two arguments. The initial capacity is 4 and capacityIncrement
is 6. It means upon insertion of the 5th element the size would be 10 (4+6) and on the
11th insertion it would be 16(10+6).
1 add() It is used to append the specified element in the given vector.
It is used to append all of the elements in the specified collection to the end
2 addAll()
of this Vector.
It is used to append the specified component to the end of this vector. It
3 addElement()
increases the vector size by one.
4 capacity() It is used to get the current capacity of this vector.
5 clear () It is used to delete all of the elements from this vector.
6 contains() It returns true if the vector contains the specified element.
7 elementAt() It is used to get the component at the specified index.
It is used to increase the capacity of the vector which is in use, if necessary.
8 ensur eCapacity() It ensures that the vector can hold at least the number of components
specified by the minimum capacity argument.
9 fir stElement() It is used to get the first component of the vector.
10 lastElement() It is used to get the last component of the vector.
It is used to insert the specified object as a component in the given vector at
11 inser tElementAt()
the specified index.
It is used to remove the specified element from the vector. If the vector does
12 r emove()
not contain the element, it is unchanged.
It is used to delete all the elements from the vector that are present in the
13 r emoveAll()
specified collection.
r emoveAllElement It is used to remove all elements from the vector and set the size of the
14
s() vector to zero.
15 setSize() It is used to set the size of the given vector.
16 size() It is used to get the number of components in the given vector.
PROGRAM:
import java.util.*;
public class VectorExample1
{
public static void main(String args[])
{
Vector<String> vec = new Vector<String>(4);
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity is: "+vec.capacity());
System.out.println("Vector element is: "+vec);
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after addition is: "+vec.capacity());
System.out.println("Elements are: "+vec);
System.out.println("The first animal of the vector is = "+vec.firstElement());
System.out.println("The last animal of the vector is = "+vec.lastElement());
System.out.println("The Animal Tiger is found at position : "+vec.indexOf("Tiger"));
vec.removeElement("Lion");
vec.removeElement("Deer");
System.out.println("After removing 2 Animals from Vector ");
System.out.println("Vector Size :"+vec.size());
System.out.println("Elements are: "+vec);
if(vec.contains("Tiger"))
{
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}
}
}
Size is: 4
Default capacity is: 4
Vector element is: [Tiger, Lion, Dog, Elephant]
Size after addition: 7
Capacity after addition is: 10
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]
The first animal of the vector is = Tiger
The last animal of the vector is = Deer
The Animal Tiger is found at position : 0
After removing 2 Animals from Vector
Vector Size :5
Elements are: [Tiger, Dog, Elephant, Rat, Cat]
Tiger is present at the index 0
A vector can be declared without specifying any
size explicitly.
A vector can accommodate an unknown number
of items . Even, when a size is specified, this can
be overlooked and a different number of items
may be put into the vector.
DISADVANTAGES OF VECTOR OVER
ARRAYS
Since, vectors cannot handle primitive data
types like int, float, long ,char, and double.
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
Let us see the different scenarios, where we need to use the wrapper classes.
Change the value in Method: Java supports only call by value. So, if we pass a
primitive value, it will not change the original value. But, if we convert the
primitive value in an object, it will change the original value.
java.util package: The java.util package provides the utility classes to deal with
objects.
Collection Framework: Java collection framework works with objects only. All
classes of the collection framework (ArrayList, LinkedList, Vector, HashSet,
LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.