Arrays Strings Vectors Wrapper Class
Arrays Strings Vectors Wrapper Class
Arrays Strings Vectors Wrapper Class
Syntax
type arrayname[];
type[] arrayname;
e.g.
int no[];
float[] per;
float marks[];
Creation of Arrays
e.g.
no=new int[5];
per=new float[5];
Array Initialization
Int no[]={10,20,30,40,50};
Array Length
JAVA supports single dimentional,two dimentional ,multidimentional and variable size arrays.
import java.io.*;
class sort
{
public static void main(String args[])
{
int a[]={14,32,25,17,29};
int i,j,temp;
System.out.println(" elements before sorting are: ");
for(i=0;i<5;i++)
System.out.print("\t"+a[i]);
for(i=0;i<5;i++)
for(j=0;j<4-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
System.out.println(" elements after sorting are: ");
for(i=0;i<5;i++)
System.out.print("\t"+a[i]);
}
}
import java.io.*;
class diagonal
{
public static void main(String args[])
{
int i,j;
int a[][]=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
a[i][j]=0;
System.out.print(" "+a[i][j]);
}
else
{
a[i][j]=1;
System.out.print(" "+a[i][j]);
}
}
System.out.print("\n");
}
}
}
Strings
A string in JAVA is a sequence of characters.It is also called as array of characters.In JAVA strings
are implemented as object of String Class.
String stringname;
e.g.
String firstname;
Notes by Pritee Parwekar
Notes by Pritee Parwekar
Class Stringsort
{
static String name[] = {“pritee”,”devi”,”dev”,”chins”,”rahul”};
public static void main(String args[])
{
Int size = name.length;
String temp= null;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(name[j].compareTo(name[i]) < 0)
{
//swap the strings
temp= name[i];
name[i]= name[j];
name[j]= temp;
}
}
]
for(int i=0;i<size;i++)
{
System.out.println(name[i]);
}
}
}
StringTokenizer
Vector
The concept of variable arugments to methods is achieved in JAVA with the use of Vector Class
which is present in java.util package.This class is used to create a generic dynamic array known
as vector which can hold objects of any type and any number.The objects do not have to be
homogenous.Arrays can be easily implemented as vectors.
2.A vector can be used to store a list of objects that may vary in size
3.we can add and delete objects from the list as and when required
A major constraints of vectors is that we cannot directly store simple data type in a vector we
can only store objects.
list.removeElementAt(n) – removes the item stored in the nth position of the list
e.g.
import java.util.*;
Class TestVector
{
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 listaray[] = new String[size];
list.copyInto(listarray);
System.out.println(“List of Languages”);
for(int i=0;i<size;i++)
{
System.out.println(listarray[i]);
}
}
}
Wrapper Classes
Vectors can not handle primitive data types like int,float,long,double,char etc.Primitive data
types can be converted into objects types by using wrapper classes contained in the java.lang
package.
Following are the data types and their corresponding wrapper class types.
Following are the conversation from primitive data types to object numbers using constructor
methods
Converting String Object to Numeric Objects using the static method ValueOf()
int I = Integer.parseInt(str);
e.g
Class test
{
Public static void main(String args[])
{
try
{
DataInputStram in = new DataInputStram(System.in);
System.out.println(“ enter a no”);
String str= in.readLine();
int a=Integer.parseInt(str);
}
catch(IOException e)
{
System.out.println(“ I/O Error”);
}
System.out.println(“\n the square of a number entered by the user is “+(a*a));
}
}
Notes by Pritee Parwekar
Notes by Pritee Parwekar