• Types of Array in java:
1) Single Dimensional Array
2) Multi dimentional Array
Passing array to method:
Returning array from method:
Creating an array of object:
String:
Length of string:
Join two strings:
Compare two strings:
StringBuffer class:
• StringBuffer is a peer class of String that provides much of the functionality of
strings.
• The string represents fixed-length, immutable character sequences while StringBuffer
represents growable and writable character sequences.
• StringBuffer may have characters and substrings inserted in the middle or appended to
the end.
• It will automatically grow to make room for such additions and often has more
characters preallocated than are actually needed, to allow room for growth.
• StringBuffer class is used to created mutable (modifiable) string. The StringBuffer class
in java is same as String class except it is mutable i.e. it can be changed.
Important Constructors of StringBuffer class
• StringBuffer(): creates an empty string buffer with the initial capacity of 16.
• StringBuffer(String str): creates a string buffer with the specified string.
• StringBuffer(int capacity): creates an empty string buffer with the specified capacity as
length.
1) append() method:
The append() method concatenates the given argument with this string.
class A
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello ");
[Link]("Java"); //now original string is changed
[Link](sb);
}
}
Output:
Hello Java
2) insert() method:
The insert() method inserts the given string with this string at the given position.
class A
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello ");
[Link](2,"Java"); //now original string is changed
[Link](sb);
}
}
Output:
HeJavallo
3) replace() method:
The replace() method replaces the given string from the specified beginIndex and
endIndex-1.
class A
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello");
[Link](1,3,"Java");
[Link](sb);
}
}
Output:
HJavalo
4) delete() method:
The delete() method of StringBuffer class deletes the string from the specified
beginIndex to endIndex-1.
class A
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello");
[Link](1,3);
[Link](sb);
}
}
Output:
Hlo
5) reverse() method:
The reverse() method of StringBuilder class reverses the current string.
class A
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello");
[Link]();
[Link](sb);
}
}
Output:
olleH
6) capacity() method:
• The capacity() method of StringBuffer class returns the current capacity of the buffer.
• The default capacity of the buffer is 16.
• If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2.
• Example:
class A
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer();
[Link]([Link]()); // default capacity 16
[Link]("Hello");
[Link]([Link]()); // current capacity 16
[Link]("java is my favourite language");
[Link]([Link]()); // current capacity (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
Example:
public class Example2
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("string buffer");
// printing the current capacity of the string buffer i.e. 16 + 13
[Link]("capacity: " + [Link]());
sb = new StringBuffer("A");
// printing the current capacity of the string buffer i.e. 16 + 1
[Link]("capacity: " + [Link]());
}
}
Example:
// Java Program to demonstrate
// the capacity function
public class capacity_example {
public static void main(String args[])
{
StringBuffer s = new StringBuffer("Gfgstring");
[Link]("capacity:" + [Link]());
// Output:
// capacity:25 (because 16+9 where length of Gfgstring is 9)
}
}
Vector:
• Vector is like the dynamic array which can grow or shrink its size.
• Unlike array, we can store n-number of elements in it as there is no
size limit.
• It is found in the [Link] package.
Creating a Vector:
• Here is how we can create vectors in Java.
Example:
Constructors:
1. Vector(): Creates a default vector of the initial capacity is 10.
Vector<E> v = new Vector<E>();
2. Vector(int size): Creates a vector whose initial capacity is specified by size.
Vector<E> v = new Vector<E>(int size);
3. Vector(int size, int incr): Creates a vector whose initial capacity is specified by size and
increment is specified by incr. It specifies the number of elements to allocate each time a
vector is resized upward.
Vector<E> v = new Vector<E>(int size, int incr);
4. Vector(Collection c): Creates a vector that contains the elements of collection c.
Vector<E> v = new Vector<E>(Collection c);
1) Adding Elements:
• In order to add the elements to the Vector, we use the add() method.
• This method is overloaded to perform multiple operations based on
different parameters. They are listed below as follows:
• add(Object): This method is used to add an element at the end of the
Vector.
• add(int index, Object): This method is used to add an element at a
specific index in the Vector.
2) Updating Elements:
• After adding the elements, if we wish to change the element, it can be
done using the set() method.
• Since a Vector is indexed, the element which we wish to change is
referenced by the index of the element.
• Therefore, this method takes an index and the updated element to be
inserted at that index.
3) Removing Elements:
• In order to remove an element from a Vector, we can use
the remove() method.
• This method is overloaded to perform multiple operations based on different
parameters. They are:
• remove(Object):
This method is used to remove an object from the Vector.
If there are multiple such objects, then the first occurrence of the object is
removed.
• remove(int index):
Since a Vector is indexed, this method takes an integer value which simply
removes the element present at that specific index in the Vector.
After removing the element, all the elements are moved to the left to fill the
space and the indices of the objects are updated.
4) Iterating the Vector:
• There are multiple ways to iterate through the Vector.
• The most famous ways are by using the basic for loop in combination
with a get() method to get the element at a specific index and
the advanced for a loop.
• Example:
import [Link];
import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner([Link]);
Vector v = new Vector<>();
[Link]("Enter the total number of elements : ");
n = [Link]();
for (int i = 0; i < n; i++)
{
[Link]("Enter element for position " + (i + 1) + " : ");
[Link]([Link]());
}
Iterator itr = [Link]();
[Link]("You have entered : ");
while ([Link]())
{
[Link]([Link]());
}
}
}