0% found this document useful (0 votes)
80 views6 pages

Department of Information Technology

This document contains 3 Java programs that demonstrate various array and vector operations: 1) The first program allows a user to input the size of an array and then elements, and prints out the elements. 2) The second program allows a user to input the rows and columns of a 2D array and elements, and prints out the 2D array. 3) The third program allows a user to input two matrices, adds them together, and prints out the resulting matrix. The document also contains two programs demonstrating vector operations - the first outputs size, capacity, and elements of a vector, while the second checks for contains, indexOf, and lastIndexOf elements in a vector.

Uploaded by

Parvi Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views6 pages

Department of Information Technology

This document contains 3 Java programs that demonstrate various array and vector operations: 1) The first program allows a user to input the size of an array and then elements, and prints out the elements. 2) The second program allows a user to input the rows and columns of a 2D array and elements, and prints out the 2D array. 3) The third program allows a user to input two matrices, adds them together, and prints out the resulting matrix. The document also contains two programs demonstrating vector operations - the first outputs size, capacity, and elements of a vector, while the second checks for contains, indexOf, and lastIndexOf elements in a vector.

Uploaded by

Parvi Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Information Technology

Academic Year: 2019-20 Name of Student:Piyush Kurkure


Semester: III Student ID:18104031
Class / Branch: IT Date of Performance: 14/10/19
Subject: JAVA Date of Submission:14/10/19
Name of Instructor: Rujata Chaudhari

Experiment No. 8

AIM : Write a java program to demonstrate Array operations .

Program1 :
import java.util.*;
class AA
{
public static void main(String Af[])
{
int i,n;
Scanner sc = new Scanner(System.in);
System.out.println("enter array size:");
n=sc.nextInt();
int A[]=new int[n];
System.out.println("enter array element:");
for(i=0;i<=n;i++)
{
A[i]=sc.nextInt();
}
System.out.println("element:");for(i=0;i<=n;i++)
{
System.out.println(A[i]);
}
}
}
Output :
Program2:
import java.util.*;
class AS
{
public static void main(String Af[])
{
int i,n,m,j;
Scanner sc = new Scanner(System.in);
System.out.println("enter array ROWS AND COLUMNS:");
m=sc.nextInt();
n=sc.nextInt();
int A[][]=new int[n][m];
System.out.println("enter array element:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
A[i][j]=sc.nextInt();
}
}System.out.println("element:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
}
}
Output:

Program3:
import java.util.*;
class array_add
{
public static void main(String agr[])
{
int m,n,i,j;
Scanner s= new Scanner(System.in);
System.out.println("ENTER VALUE FOR ROWS AND COULMNS:");
m=s.nextInt();
n=s.nextInt();
int A[][]= new int [m][n];
int B[][]= new int [m][n];int C[][]= new int [m][n];
System.out.println("ENTER ELEMENTS FROM 1st MATRIX:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
A[i][j]=s.nextInt();
}
}
System.out.println("ENTER ELEMENTS FROM 2nd MATRIX:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
B[i][j]=s.nextInt();
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
System.out.println("ADDITION OF MATRICES");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}
}
Output:
VECTOR
Program1:
import java.util.*;
class vector
{
public static void main(String args[])
{
Vector<String>v1=new Vector<String>(2);
v1.addElement("DOG");
v1.addElement("CAT");
v1.addElement("WOLF");
v1.addElement("FOX");
System.out.println("Size is:"+v1.size());
System.out.println("Default capacity increment is:"+v1.capacity());
v1.addElement("Animal1");
v1.addElement("Animal2");
v1.addElement("Animal3");
System.out.println("Size after addition:"+v1.size());
System.out.println("Capacity after increment is:"+v1.capacity());
Enumeration en= v1.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.println(en.nextElement()+"\n");}
}
Output:
Program2:
import java.util.*;
class Vector1
{
public static void main(String args[])
{
Vector<String>v=new Vector<String>(2);
v.addElement("LION");
v.addElement("TIGER");
v.addElement("ELEPHANT");
v.addElement("FOX");
v.addElement("Wolf");
boolean x=v.contains("TIGER");
System.out.println("Does vector contain TIGER??"+x);int index=v.indexOf("FOX");
if(index==-1)
System.out.println("Vector does not contains FOX");
else
System.out.println("Vector contains FOX at index: "+index);
int lastIndex=v.lastIndexOf("Wolf");
if(lastIndex==-1)
System.out.println("Vector does not contains Wolf");
else
System.out.println(" Last occurrence of Wolf in vector is at
index :"+lastIndex);
}
}
Output:

You might also like