demonstrate concept of array string and vector.

, by Prashant Gunjal


/*
Aim : Write a program to demonstrate concept of array string and vector.
*/
import java.lang.*;
import java.io.*;
import java.util.*;

class pr_3
{
public static void main(String argv[]) throws IOException
{
int i,j,cnt,temp;
String s1,s2,s3;
s1=argv[0];
s2=argv[1];
System.out.println("\n\n**************String Related Operations *****************");
System.out.println("\t 1) Display  ");
System.out.print("\n\t   String s1 = "+s1);
System.out.print("\n\t   String s2 = "+s2);

System.out.print("\n\n\t 2) Length ");
System.out.print("\n\t   Length of String s1 = "+s1.length());
System.out.print("\n\t   Length of String s2 = "+s2.length());

System.out.print("\n\n\t 3) Substring ");
s3=s1.substring(3,6);
System.out.print("\n\t   Substring of String s1(3,6) = "+s3);
s3=s2.substring(3);
System.out.print("\n\t   Substring of String s2(3) = "+s3);

System.out.print("\n\n\t 4) Concatination ");
System.out.print("\n\t   Concatination of String s1 & s2 is = "+s1+s2);

System.out.print("\n\n\t 5) Equality ");
System.out.println("\n\t   String s1 and s2 are equal to each other .. =>   "+s1.equals(s2));

System.out.print("\n****************One Diamentional array*************************");

int no[];
no=new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("\n\tEnter Count of numbers to insert in array : ");
cnt=Integer.parseInt(br.readLine());
for(i=0;i<cnt;i++)
{
System.out.print("\n\tEnter no of location "+ i +" =  " );
no[i]=Integer.parseInt(br.readLine());
}
System.out.print("\n\tContents of array are : ");
for(i=0;i<cnt;i++)
{
System.out.print("\t"+no[i]);
}
System.out.print("\n\tSorted array is : ");
for(i=0;i<cnt;i++)
{
for(j=0;j<cnt;j++)
{
if(no[i]>no[j])
{
temp=no[i];
no[i]=no[j];
no[j]=temp;
}
}
}
for(i=0;i<cnt;i++)
{
System.out.print("\t"+no[i]);
}

System.out.println("\n*************Multidiamentional (ie example of 2d) Array***********");
int mat1[][],mat2[][],mat3[][],r,c;
mat1 = new int [5][5];
mat2 = new int [5][5];
mat3 = new int [5][5];

System.out.print("\n\tEnter no of rows : ");
r=Integer.parseInt(br.readLine());
System.out.print("\n\tEnter no of  columns : ");
c=Integer.parseInt(br.readLine());

System.out.println("\n\tEnter Matrix 1 ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print("\t");
mat1[i][j]=Integer.parseInt(br.readLine());
}
System.out.print("\n");
}

System.out.println("\n\tEnter Matrix 2 ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print("\t");
mat2[i][j]=Integer.parseInt(br.readLine());
}
System.out.print("\n");
}
System.out.println("\n\tAddition of Matrix 1 and Matrix 2 is : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
System.out.print("\t"+mat3[i][j]);
}
System.out.print("\n");
}


System.out.print("\n********************Vector Demonstration *****************");
Vector<Integer> v = new Vector<Integer>(20);
System.out.print("\n\t1)Status....");
System.out.print("\n\n\tInitial size = "+v.size());
System.out.print("\n\tInitial Capacity = "+v.capacity());
System.out.print("\n\t2)Inserting elements last ....");
System.out.print("\n\tEnter no of Element to insert : ");
cnt=Integer.parseInt(br.readLine());
for(i=0;i<cnt;i++)
{
System.out.print("\n\t\tEnter "+ i +" no element  : ");
j=Integer.parseInt(br.readLine());
v.add(j) ;
}
//v.addElement(2); //method 2
//v.addElement(new Integer(3)); //method 3

System.out.print("\n\tNow size = "+v.size());
System.out.print("\n\n\t3)Display content  ");
System.out.print("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}
System.out.print("\n\n\t3)Inserting Element by index ");
System.out.print("\n\t\tEnter index no  : ");
j=Integer.parseInt(br.readLine());
System.out.print("\n\t\tEnter element : ");
temp=Integer.parseInt(br.readLine());
v.add(j,temp);

System.out.print("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}

System.out.print("\n\n\t4.Finding element ");
System.out.print("\n\t\tEnter element to find  : ");
j=Integer.parseInt(br.readLine());
System.out.print("\n\t\tElement present :   "+v.contains(j));

System.out.print("\n\n\t5.Finding First element ");
System.out.print("\n\t\tFirst Element is = "+v.firstElement());

System.out.print("\n\n\t6.Finding Last  ");
System.out.print("\n\t\tLast Element is = "+v.lastElement());

System.out.print("\n\n\t7.Replace by index ");
System.out.print("\n\t\tEnter index no  : ");
j=Integer.parseInt(br.readLine());
System.out.print("\n\t\tEnter element : ");
temp=Integer.parseInt(br.readLine());
v.set(j,temp);
System.out.print("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}

System.out.print("\n\n\t8.Remove element by index");
System.out.print("\n\t\tEnter index no  : ");
j=Integer.parseInt(br.readLine());
v.remove(j);
System.out.println("\n\tContent of Vector are : ");
for(i=0;i<v.size();i++)
{
System.out.print("  "+v.get(i));
}


}
}
/* OUTPUT
prashant@prashant-OptiPlex-755:~$ javac pr_3.java
prashant@prashant-OptiPlex-755:~$ java pr_3 prashant gunjal


******************String Related Operations *********************
1) Display  

  String s1 = prashant
  String s2 = gunjal

2) Length 
  Length of String s1 = 8
  Length of String s2 = 6

3) Substring 
  Substring of String s1(3,6) = sha
  Substring of String s2(3) = jal

4) Concatination 
  Concatination of String s1 & s2 is = prashantgunjal

5) Equality 
  String s1 and s2 are equal to each other .. =>   false

****************One Diamentional array**************************
Enter Count of numbers to insert in array : 5

Enter no of location 0 =  8

Enter no of location 1 =  5

Enter no of location 2 =  6

Enter no of location 3 =  1

Enter no of location 4 =  3

Contents of array are : 8 5 6 1 3
Sorted array is : 8 6 5 3 1
*************Multidiamentional (ie example of 2d) Array******************

Enter no of rows : 3

Enter no of  columns : 3

Enter Matrix 1 
1
2
3

4
5
6

7
8
9


Enter Matrix 2 
0
1
2

3
4
5

6
7
8


Addition of Matrix 1 and Matrix 2 is : 
1 3 5
7 9 11
13 15 17

*************************Vector Demonstration *********************
1)Status....

Initial size = 0
Initial Capacity = 20
2)Inserting elements last ....
Enter no of Element to insert : 4

Enter 0 no element  : 1

Enter 1 no element  : 2

Enter 2 no element  : 3

Enter 3 no element  : 4

Now size = 4

3)Display content  
Content of Vector are :   1  2  3  4

3)Inserting Element by index 
Enter index no  : 2

Enter element : 60

Content of Vector are :   1  2  60  3  4

4.Finding element 
Enter element to find  : 60

Element present :   true

5.Finding First element 
First Element is = 1

6.Finding Last  
Last Element is = 4

7.Replace by index 
Enter index no  : 0

Enter element : 8

Content of Vector are :   8  2  60  3  4

8.Remove element by index
Enter index no  : 3

Content of Vector are :   8  2  60  4
prashant@prashant-OptiPlex-755:~$ ^C
prashant@prashant-OptiPlex-755:~$ 
*/



0 comments: