UNIT - IV - Array in Java 2018
UNIT - IV - Array in Java 2018
Programming
Using Java
Prof.Pradnya Sadigale
Prof: Pradnya Sadigale
(E&TC Department)
SOET- Lohegaon
What is an array
• An array is a group of like-typed variables that are referred
to by a common name. or
Initializing Array
Declaration of array:
To create an array ,you first must create an array
variable
of the desired type.
Data_Type variable_name[ ];
e.g. int month_days[];
Number[1]
Number[0]
Number[3]
Number[4]
Class ArrayDemo1
{
public static void main(String args[])
{
int number[] = new int[5];
int sum=0;
number[0]=5;
number[1]=10;
number[2]=15;
number[3]=20;
number[4]=25;
for(int i=0;i<5;i++)
sum=sum+number[i];
System.out.println("Average is : "+sum/5);
} }
Output:
Average is :15
Simple program
import java.util.*;
class Reverse
{
public static void main(String args[])
{
int n,i;
Scanner sc=new Scanner(System.in);
System.out.print("Enter no of elements:");
n=sc.nextInt();
int a[]=new int [n];
int b[]=new int [n];
for(i=0;i<=n-1;i++)
{
System.out.print("Enter a no:");
a[i]=sc.nextInt();
}
for(i=0;i<=n-1;i++)
{
b[n-i-1]=a[i];
}
for(i=0;i<=n-1;i++)
{
System.out.println(b[i]);
}
} }
/*********output**********
Enter no of elements :5
Enter a no:1
Enter a no:2
Enter a no:3
Enter a no:4
Enter a no:5
5
4
3
2
1 */
//WAP to find an element in array and display the index of the element
//OR WAP to implement squential search algorithm
import java.util.*;
class Search
{
public static void main(String args[])
{
int n,i,search;
Scanner sc=new Scanner(System.in)
System.out.print("Enter no of elements:");
5
sc.nextInt();
int a[]=new int [n];
for(i=0;i<=n-1;i++)
{
System.out.print("Enter a no:");
a[i]=sc.nextInt();
}
System.out.print("Enter the no to be searched:"); /*****output******
Enter no of elements:4
Search=sc.nextInt();; Enter a no.1
for(i=0;i<=(n-1);i++) Enter a no.2
Enter a no.3
{ Enter a no.4
if(search==a[i]) Enter the no to be searched :4
Index=3
break;
} */
5
if(i==n)
System.out.println("No. not found");
else
System.out.println("Index =" + i);
}
}
Array length
All Aarrys store the allocated size in a variable named
length.we can obtain the length of the array number
using number.length
Syntax :
Row o
Row 2
[1][0] [1][1] [1][2]
Row 3
[2][0] [2][1] [2][2]
}
}
/* wap that displays the marks of 5 subjects of 3 students and the
average of marks for each subject*/
class TwoDArray2 System.out.print(marks[i][j]+"\t");
{public static void main(String args[ ]) System.out.print("\n");
{ }
int i , j ; System.out.print("\n\nThe Average of
int marks[][] ={ {65,68,75,59,77}, each subject is : \n") ;
{62,85,57,66,80},{71,77,66,63,86 } } ; for (j=0 ; j<5 ; j++)
float avg ; {
System.out.print("\t\t"); System.out.print("Subject"+(j+1)+" :") ;
for (i = 0 ; i < 5 ; i++) for (i=0,avg=0 ; i<3 ; i++)
System.out.print("subj"+(i+1)+"\t"); avg = avg + (float)marks[i][j] ;
System.out.println("\n"); avg = avg / 3 ;
for (i=0 ; i<3 ; i++) System.out.print(avg+"\n");
{ }
System.out.print(" student"+(i+1)+"\t"); }
for(j=0 ; j<5 ; j++) }
//write a program to accept an m* n matrix and display it in
natural form
import java.util.*;
class array2d
{
public static void main(String args[])
{ int m,n,i,j;
Scanner sc=new Scanner(System.in);
System.out.print("Enter no of rows and columns:");
m=sc.nextInt();
n=sc.nextInt();
int a[][]=new int [m][n];
for(i=0;i<=m-1;i++)
{ for(j=0;j<=n-1;j++)
{
System.out.print("Enter a no:");
a[i][j]=sc.nextInt();
}
}
for(i=0;i<=m-1;i++)
{ for(j=0;j<=n-1;j++)
{ System.out.print(a[i][j]+"\t");
} System.out.println();
}}}
/* output ******
Enter no of rows and columns:2
3
Enter a no:1
5
Enter a no:2
Enter a no:3
Enter a no:4
Enter a no:5
Enter a no: 6
123
456
*****/
Program to find and display
The sum of the diagonal elements of a square matrix
import java.util.*; }
class Diagonal for(i=0;i<=m-1;i++)
{ {
for(j=0;j<=n-1;j++)
public static void main(String args[])
{
{
int m,n,i,j,sum=0; if(i==j)
sum=sum+a[i][j];
Scanner sc =new Scanner(System.in)
} }
System.out.print("Enter no of rows and
System.out.println("Sum="+sum);
columns:"); } }
m=sc.nextInt(); /*** output
n=sc.nextInt(); 5 Enter no of rows and columns:3
int a[][]=new int [m][n]; Enter a no:1
for(i=0;i<=m-1;i++) Enter a no:2
{ Enter a no:3
for(j=0;j<=n-1;j++) Enter a no:4
{ System.out.print("Enter a no:"); Enter a no:5
Enter a no:6
a[i][j]=sc.nextInt();
Enter a no:7
} Enter a no:8
Enter a no:9
Sum=15
*********/
Write a Program to add two matrices of size m*n
import java.util.*; for(i=0;i<=m-1;i++)
class Sum{ { for(j=0;j<=n-1;j++)
public static void main(String args[]) {
{ System.out.print("Enter a no:");
int m,n,i,j; b[i][j]=sc.nextInt();
Scanner sc=new Scanner(System.in); } }
System.out.print("Enter no of rows and for(i=0;i<=m-1;i++)
columns:"); {
m=sc.nextInt(); for(j=0;j<=n-1;j++)
n=sc.nextInt(); {
int a[][]=new int [m][n]; c[i][j]=a[i][j]+b[i][j];
int b[][]=new int [m][n]; 5 }
int c[][]=new int [m][n]; }
System.out.println("Matrix A"); System.out.println("Sum Matrix");
for(i=0;i<=m-1;i++) for(i=0;i<=m-1;i++)
{ for(j=0;j<=n-1;j++) {
{ for(j=0;j<=n-1;j++)
System.out.print("Enter a no:"); {
a[i][j]=sc.nextInt(); System.out.print(c[i][j]+"\t");
} } }
System.out.println("Matrix B"); System.out.println();
}
}}
Enter no of row and columns :2
3
Matrix A
Enter a no:1
Enter a no:2
Enter a no:3
Enter a no:4
Enter a no:5
Enter a no:6
Matrix B
Enter a no:1
Enter a no:2 5
Enter a no:3
Enter a no:4
Enter a no:5
Enter a no:6
Sum Matrix
2 4 6
8 10 12
Write a Program to multiply two matrix
import java.util.*; for(i=0;i<=n-1;i++)
class Sum{ { for(j=0;j<=p-1;j++)
public static void main(String args[]) {
{ System.out.print("Enter a no:");
int m,n,p,i,j,k; b[i][j]=sc.nextInt();
Scanner sc=new Scanner(System.in); } }
System.out.print("Enter values of m,n & for(i=0;i<=m-1;i++)
p:"); {
m=sc.nextInt(); for(j=0;j<=p-1;j++)
n=sc.nextInt(); {
p=sc.nextInt(); c[i][j]=0;
int a[][]=new int [m][n]; 5 for (k=0;k<=n-1;k++)
int b[][]=new int [m][p]; {
int c[][]=new int [m][p]; c[i][j]+=a[i][k]*b[k][j];
System.out.println("Matrix A"); }
for(i=0;i<=m-1;i++) }
{ for(j=0;j<=n-1;j++) }
{
System.out.print("Enter a no:");
a[i][j]=sc.nextInt();
} }
System.out.println("Matrix B");
System.out.println(“ product Matrix"); Enter no of row and columns :2
for(i=0;i<=m-1;i++) 3
{ 3
for(j=0;j<=n-1;j++) Matrix A
{ Enter a no:1
System.out.print(c[i][j]+"\t"); Enter a no:2
} Enter a no:3
System.out.println(); Enter a no:4
} Enter a no:5
} Enter a no:6
} Matrix B
5Enter a no:1
Enter a no:2
Enter a no:3
Enter a no:4
Enter a no:5
Enter a no:6
Enter a no:7
Enter a no:8
Enter a no:9
Sum Matrix
18 21 24
27 30 33
Program to find the transpose of a Square matrix
import java.util.*; for(i=0;i<=m-1;i++)
class transpose {
{ for(j=0;j<=n-1;j++)
{
public static void main(String args[])
temp=a[i][j];
{
a[i][j]=a[j][i];
int m,n,i,j,temp; a[j][i]=temp;
Scanner sc =new Scanner(System.in) } }
System.out.print("Enter value of m"); System.out.println(“transpose of
m=sc.nextInt(); matrix”);
n=m; for(i=0;i<=m-1;i++)
int a[][]=new int [m][n]; 5 {
for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++)
{
{
System.out.print(a[i][j]+”\t”);
for(j=0;j<=n-1;j++)
}
{ System.out.println();
System.out.print("Enter a no:"); }
a[i][j]=sc.nextInt();
} }
}
Using arraycopy( )
• The arraycopy( ) method :can be used to copy quickly
an array of any type from one place to another.
• This static method is available in the class “system” of
the package java.lang.this
SYNTAX :
System. arraycopy(source_array_name ,
starting _element_index_of source_array,
destination_array_name, destination_array_starting_element ,
number_of elements_to_be_copied )
e.g.
System.arraycopy(Array1,5,Array2,5,5);
Write a program to demonstrate the use of arraycopy()
method
/* Program to demonstrates System.arraycopy() function*/
class Arraycopy
{
public static void main(String args[ ])
{int i;
int Array1[ ] = {10,20,30,40,50,60,70,80,90,100};
int Array2[ ] = {1,2,3,4,5,6,7,8,9,10};
System.out.println("The first array is:");
for (i = 0; i < 10; i++)
System.out.print(Array1[i]+"\t");
5
System.out.println("The second array is:");
for (i = 0; i < 10; i++)
System.out.print(Array2[i]+"\t");