Arrays Strings and Vectors in java

Arrays in java:

An array is a group of contiguous or related data items that share a common name. In Java, there are two types of array exists:

  • One-Dimensional Array
  • Two-Dimensional Array

One-Dimensional Array:

A list of items can be given one variable name using only one subscript and this variable is called a One-Dimensional Array.
Example:

x[5]

one-dimensional array

String:

It represents a sequence of characters in Java by using a character array. It has the following Syntex:

String string-name;
string-name=new String("string");

Example:

String sk;
sk=new String("Hello World");

String Array in Java:

It can assign the strings to the string-name element by element using more different statements or using a for loop. It has the following syntax:

String string-name[]=new String[size];

Example:

String sk[]=new String[5]

Vectors in java:

Vector is a type of class that can be used to create a generic dynamic array that can hold objects of any type and any number. The arrays can be easily implemented as vectors. Vectors are created like arrays as follows:

Vector list=new Vector();
Vector list=new Vector(4);

Advantages of Vector:

(i) It is convenient to use vectors to store objects.
(ii) A vector can be used to store a list of objects that may vary in size.
(iii) We can add and delete objects from the list as and when required.

Some Vector Methods are given as follows:

Vector Methods
Description
list.addElement(item)It adds the item specified to the list at the end.
list.elementAt(n)It gives the name of the nth object.
list.size()It gives the number of objects present
list.removeElement(item)It removes the specified item from the list.
list.removeElementAt(n)It removes the item stored in the nth position of the list.
list.removeAllElements()It removes all the elements in the list.
list.copyInto(array)It copies all items from list to array.
list.insertElementAt(item, n)It inserts the item at nth position.

Program:

import java.util.*;
class test
{
public static void main(String args[])
{
Vector list=new Vector();
int length=args.length;
for(int i=0;i<length;i++)
{
list.addElement(args[i]);
}
list.insertElementAt("COBOL",2);
int size=list.size();
String listArray[]=new String[size];
list.copyInto(listArray);
System.out.println("List of Programming Language");
for(int i=o;i<size;i++)
{
System.out.println(listArray[i]);
}
}
}