Expt No.06
Expt No.06
❖ Aim : To write a Java program on 2D array, and also demonstrate the use of String Buffer and
Vector.
❖ Objectives : To study the differance between array & vector in java alongwith its use in java
program.
❖ Prerequisites : Students should know about array concept in C along witharray declaration,
initialization & its advantages.
❖ Software used : jdk 1.6.0
❖ Theory :
1] Array in Java :
• array is a collection of similar type of elements that have contiguous memory location.
• Java array is an object that contains elements of similar data type.
• It is a data structure where we store similar elements.
• We can store only fixed set of elements in a java array.
• Array in java is index based, first element of the array is stored at 0 index.
an array.
➔ OUTPUT :
10
20
70
40
50
OUTPUT : 3
2] Vector In Java :
– We know that Arrays are very useful when there is a need to use fix number ofvariables
– There is a problem with Array i.e. they use only single data type or The Elements of
array are always Same type For Avoiding this Problem Vectors are used.
– Vectors are Collection of elements those are object data type.
– For Using Vectors we have to import java . util package . These are also Calledas
dynamic Array of object data type .
– For Creating a Vector, Vector Class will be used which is reside in java’sutility
package
– vectors doesn’t support primitives data types like int, float, char etc.
– Three ways to create vector class object:
1. Method 1:
It creates an empty Vector with the default initial capacity of 10. It means the Vector
will be re-sized when the 11th elements needs to be inserted into the Vector.
Note: By default vector doubles its size. i.e. In this case the Vector size would remain
10 till 10 insertions and once we try to insert the 11th element It would become 20
(double of default capacity 10).
2. Method 2:
3. Method 3:
e.g.