Object Oriented
Programming
Using Java
[Link] 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
• An array is a collection of variables of the same type that
are referred to through a common name.
• Arrays of any type can be created and may have one or
more dimensions.
• A specific element in an array is accessed by its index.
• The lowest address corresponds to the first element and the
highest address to the last element.
• Arrays offer a convenient means of grouping related
information.
• In Java and other programming languages, there is
one capability where in we can use one variable to
store a list of data and manipulate them more
efficiently. This type of variable is called an array.
• An array stores multiple data items of the same data
type, in a contiguous(Connecting without a break)
block of memory, divided into a number of slots
• Java arrays are created dyanmically
• The starting index of an array always o and the last
index is always (n-1)
Where n is the no. of values contained in array
Also note that when you do [Link] you're not invoking any methods
but just accessing the array's length field. There are plenty of static
methods in the Arrays class
A list of variables name using only one subscript of and
such a variable is called single subscript variable or one-
dimeansional array .
A Single dimensional array represents is a single row or a
single column of an elements.
The general form of a one-dimensional
Declaration of array
Creating memory location
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[];
ALTERNATIVE ARRAY DECLARATION :
Data_ type [ ] variable_name;
e.g.
int month_days[];
int [] month_days;
Creating Arrays:
• Need to use it now to allocate memory for arrays.
• In java array does not have static memory allocation i.e
memory size can be allocated for an array during run time .
• Java allows us to create arrays using new operator.
(new special operator that allocates memory)
• New operator dynamically allocates(i.e allocates at run
time)memory for an object & returns a reference to it
• The address in memory of the object allocated by new this
referance is then stores in the variable .
• general form of new as it applies to one-dimensional arrays.
array-variable = new data_type[array_size];
Creating Arrays:
• Data_type:- Specifies the type of data being allocated
• Array_ size: is an integer value which determines size of
array or memory locations to be allocate to an array
• The elements in the array allocated by new will
automatically be initialized to zero.
• Example : number = new int[5];
Combine 2 steaps array declaration & creation
Data_type array_name[]=new data_type[array_size];
int number[ ]=new int[5];
Statement Representation
int number[]; Number Null
number=new int[4]; Number
Point to int object
Number[0]
Number[1]
Number[0]
Number[3]
Number[4]
Fig: creation of an array in memory
Initializing array
Type 1:
number[0]=5;
number[1]=10;
number[2]=15;
number[3]=20;
Type 2:
Also initialize array automatically in same way
Type array-variable[]={list of variable} ;
ex:
int number1[]={5,10,15,20} ;
int number2[];
number2=number1;
Are valid in java both array have same value
Simple program
/* Program to calculate average of 5 numbers using Array*/
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];
[Link]("Average is : "+sum/5);
} }
Output:
Average is :15
Simple program
/* Program to calculate average of 5 numbers using Array*/
class ArrayDemo1
{
public static void main(String args[])
{
// int number[]=new int[5];
int number[]={5,10,15,20,25};
int sum=0;
for(int i=0;i<5;i++)
sum=sum+number[i];
[Link]("Average is : "+sum/5);
}
}
/********* output
Output:
Average is :15
*/
/* Wap program to accept n integer from user into an array and display them one
in each line */
import [Link].*; for(i=0;i<=n-1;i++)
class Array {
{ [Link](a[i]);
public static void main(String args[]) }
{ }
int n,i; }
Scanner sc=new Scanner ([Link]); /***Output******
[Link]("Enter no of elements:"); Enter no of element:4
n=[Link](); Enter a no:1
int a[]=new int [n]; Enter a no:2
for(i=0;i<=n-1;i++) Enter a no:3
{ Enter a no:4
[Link]("Enter a no:");
a[i]=[Link]();
}
WAP Program to accept 'n‘ integer from user into an array & display the average
of these number .
import [Link].*; for(i=0;i<=n-1;i++)
class Average {
{ sum=sum+a[i];
public static void main(String args[]) }
{ avg=(float)sum/n;
int n,i,sum=0; [Link]("Average="+avg);
float avg; }
Scanner sc=new Scanner([Link]); }
[Link]("Enter no of elements:"); /* output
n=[Link](); Enter no of elements:4
int a[ ]=new int [n]; Enter a no:2
for(i=0;i<=n-1;i++) Enter a no:3
{ Enter a no:1
[Link]("Enter a no:"); Enter a no:4
a[i]=[Link](); Avarage=2.5
} */
/* WAP to find the largest of 'n' number taken from user */
import [Link].*; large=a[0];
class Large for(i=0;i<=n-1;i++)
{ {
public static void main(String args[]) if(large<a[i])
{ large=a[i];
int n,i,large; }
Scanner sc=new Scanner([Link]); [Link]("Largest no=“
[Link]("Enter no of elements:"); +large);
n=[Link](); }
int a[]=new int [n]; }
for(i=0;i<=n-1;i++) /******output******
Enter no of elements :4
{ Enter a no.:23
[Link]("Enter a no:"); Enter a no.:43
a[i]=[Link](); Enter a no.: 1
} Enter a no.:34
Largest no=43
*/
/*Wap to find the smallest of n numbers taken from user */
import [Link].*; small=a[0];
class small for(i=0;i<=n-1;i++)
{
{public static void main(String args[]) if(small>a[i])
{ small=a[i];
int n,i,small; }
Scanner sc=new Scanner([Link]); [Link]("Smallest
[Link]("Enter no of no="+small);
}
elements:"); }
n=[Link](); /********output *********
int a[]=new int [n]; Enter no of elements :4
for(i=0;i<=n-1;i++) Enter a no.:23
{ [Link]("Enter a no:"); Enter a no.:43
Enter a no.: 1
a[i]=[Link](); Enter a no.:34
} Smallest no=1
*/
WAP a program to sort numbers in ascending order Or
WAP to implement bubble sorting algorithm for sorting numbers in ascending
order
import [Link].*; for(i=0;i<=n-2;i++)
class Ascend {
{ for(j=0;j<=n-2;j++)
public static void main(String args[]) {
{ if(a[j]>a[j+1])
int n,i,j,temp; {
Scanner sc=new Scanner([Link]); temp=a[j];
[Link]("Enter no of elements:"); a[j]=a[j+1];
n=[Link](); a[j+1]=temp;
int a[]=new int [n]; }}}
for(i=0;i<=n-1;i++) [Link]("After Sorting");
{ for(i=0;i<=n-1;i++)
[Link]("Enter a no:"); {
a[i]=[Link](); [Link](a[i]);
} }
}
}
WAP to sort numbers in descending order Or
WAP to implement bubble sorting algorithm for sorting numbers in descending order
import [Link].*; if(a[j]<a[j+1])
class Descend { temp=a[j];
{public static void main(String args[]) a[j]=a[j+1];
{ a[j+1]=temp;
int n,i,j,temp; }
Scanner sc=new Scanner([Link]); } }
[Link]("Enter no of elements:"); [Link]("After Sorting");
n=[Link]();; for(i=0;i<=n-1;i++)
int a[]=new int [n]; { [Link](a[i]);
for(i=0;i<=n-1;i++) } } }
{ /******output ******
[Link]("Enter a no:"); Enter no of elements :5
a[i]=[Link]();; Enter a no :4
} Enter a no:6
for(i=0;i<=n-1;i++) Enter a no:1
{ for(j=0;j<=n-1;j++) Enter a no:85
{ Enter a no:9
After sorting */
Write a program to find and display the reverse of an array
import [Link].*;
class Reverse
{
public static void main(String args[])
{
int n,i;
Scanner sc=new Scanner([Link]);
[Link]("Enter no of elements:");
n=[Link]();
int a[]=new int [n];
int b[]=new int [n];
for(i=0;i<=n-1;i++)
{
[Link]("Enter a no:");
a[i]=[Link]();
}
for(i=0;i<=n-1;i++)
{
b[n-i-1]=a[i];
}
for(i=0;i<=n-1;i++)
{
[Link](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 [Link].*;
class Search
{
public static void main(String args[])
{
int n,i,search;
Scanner sc=new Scanner([Link])
[Link]("Enter no of elements:");
5
[Link]();
int a[]=new int [n];
for(i=0;i<=n-1;i++)
{
[Link]("Enter a no:");
a[i]=[Link]();
}
[Link]("Enter the no to be searched:"); /*****output******
Enter no of elements:4
Search=[Link]();; 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)
[Link]("No. not found");
else
[Link]("Index =" + i);
}
}
Array length
All Aarrys store the allocated size in a variable named
[Link] can obtain the length of the array number
using [Link]
Syntax :
e.g [int size=[Link] ]
//arrange ascending order using array length
class array length if(a[j]>a[j+1])
{ {
int number[]={100,-34,78,23,10}; temp=number[j];
number[j]=a[j+1];
int size =[Link]; number[j+1]=temp;
}
for(int i=0;i<=size-1;i++) }
}
[Link](number[i]+”\t”); [Link]("After Sorting");
for(i=0;i<=size-1;i++)
[Link](); {
[Link](number[i]+”\t”);
for(i=0;i<=size-2;i++) }
}
{ }
for(j=0;j<=size-2;j++)
{
Note Down….
To declare an array, write the data type, followed by a set of square
brackets[], followed by the identifier name.
For example,
int []ages;
or int ages[];
After declaring, we must create the array and specify its length with
a constructor statement.
Definitions:
Instantiation
In Java, this means creation
Constructor
In order to instantiate an object, we need to use a constructor for
this. A constructor is a method that is called to create a certain
object.
Multidimentional Array ….
• multidimensional arrays are actually arrays of arrays.
• Declares a two-dimensional array variable called twoD.
data_type array-variable[][] = new type[size][size];
e.g. int TwoDimensional[ ][ ] = new int[4][3];
Coloum 0 Coloum 1 Coloum 2
Row o
Row 1 [0][0] [0][1] [0][2]
Row 2
[1][0] [1][1] [1][2]
Row 3
[2][0] [2][1] [2][2]
[3][0] [3][1] [3][2]
//initialization of 2 D arrays
class TwoDArray2
{
public static void main(String args[ ])
{
int i , j ;
int marks[][] ={ {65,68,75,59,77},
{62,85,57,66,80},
{71,77,66,63,86} } ;
for ( i = 0,sum = 0 ; i < 3 ; i++)
{
for (j=0 ; j< 5 ; j++)
sum = sum + elements[i][j] ;
}
[Link]("The result of addition = "+ sum) ;
}
}
/* wap that displays the marks of 5 subjects of 3 students and the
average of marks for each subject*/
class TwoDArray2 [Link](marks[i][j]+"\t");
{public static void main(String args[ ]) [Link]("\n");
{ }
int i , j ; [Link]("\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 ; {
[Link]("\t\t"); [Link]("Subject"+(j+1)+" :") ;
for (i = 0 ; i < 5 ; i++) for (i=0,avg=0 ; i<3 ; i++)
[Link]("subj"+(i+1)+"\t"); avg = avg + (float)marks[i][j] ;
[Link]("\n"); avg = avg / 3 ;
for (i=0 ; i<3 ; i++) [Link](avg+"\n");
{ }
[Link](" 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 [Link].*;
class array2d
{
public static void main(String args[])
{ int m,n,i,j;
Scanner sc=new Scanner([Link]);
[Link]("Enter no of rows and columns:");
m=[Link]();
n=[Link]();
int a[][]=new int [m][n];
for(i=0;i<=m-1;i++)
{ for(j=0;j<=n-1;j++)
{
[Link]("Enter a no:");
a[i][j]=[Link]();
}
}
for(i=0;i<=m-1;i++)
{ for(j=0;j<=n-1;j++)
{ [Link](a[i][j]+"\t");
} [Link]();
}}}
/* 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 [Link].*; }
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([Link])
} }
[Link]("Enter no of rows and
[Link]("Sum="+sum);
columns:"); } }
m=[Link](); /*** output
n=[Link](); 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
{ [Link]("Enter a no:"); Enter a no:5
Enter a no:6
a[i][j]=[Link]();
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 [Link].*; for(i=0;i<=m-1;i++)
class Sum{ { for(j=0;j<=n-1;j++)
public static void main(String args[]) {
{ [Link]("Enter a no:");
int m,n,i,j; b[i][j]=[Link]();
Scanner sc=new Scanner([Link]); } }
[Link]("Enter no of rows and for(i=0;i<=m-1;i++)
columns:"); {
m=[Link](); for(j=0;j<=n-1;j++)
n=[Link](); {
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]; }
[Link]("Matrix A"); [Link]("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++)
[Link]("Enter a no:"); {
a[i][j]=[Link](); [Link](c[i][j]+"\t");
} } }
[Link]("Matrix B"); [Link]();
}
}}
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 [Link].*; for(i=0;i<=n-1;i++)
class Sum{ { for(j=0;j<=p-1;j++)
public static void main(String args[]) {
{ [Link]("Enter a no:");
int m,n,p,i,j,k; b[i][j]=[Link]();
Scanner sc=new Scanner([Link]); } }
[Link]("Enter values of m,n & for(i=0;i<=m-1;i++)
p:"); {
m=[Link](); for(j=0;j<=p-1;j++)
n=[Link](); {
p=[Link](); 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];
[Link]("Matrix A"); }
for(i=0;i<=m-1;i++) }
{ for(j=0;j<=n-1;j++) }
{
[Link]("Enter a no:");
a[i][j]=[Link]();
} }
[Link]("Matrix B");
[Link](“ 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
[Link](c[i][j]+"\t"); Enter a no:2
} Enter a no:3
[Link](); 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 [Link].*; 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([Link]) } }
[Link]("Enter value of m"); [Link](“transpose of
m=[Link](); 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++)
{
{
[Link](a[i][j]+”\t”);
for(j=0;j<=n-1;j++)
}
{ [Link]();
[Link]("Enter a no:"); }
a[i][j]=[Link]();
} }
}
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 [Link]
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.
[Link](Array1,5,Array2,5,5);
Write a program to demonstrate the use of arraycopy()
method
/* Program to demonstrates [Link]() 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};
[Link]("The first array is:");
for (i = 0; i < 10; i++)
[Link](Array1[i]+"\t");
5
[Link]("The second array is:");
for (i = 0; i < 10; i++)
[Link](Array2[i]+"\t");
// Copying last 5 elements of Array1 to Array2
[Link](Array1,5,Array2,5,5);
[Link]("The second array after calling arraycopy( ) : ");
for (i = 0; i < 10; i++)
[Link](Array2[i]+"\t");
}
}
/**Output:
The first array is:
10 20 30 40 50 60 70 80 90 100
The second array is:
1 2 3 4 5 6 7 8 9 10
The second array after calling arraycopy():
1 2 3 4 5 60 70 80 90 100
*/
import [Link].*;
class ArrayCopy{ [Link]();
public static void main(String args[])
{ [Link](a,0,b,1,[Link]-1);
int n,i;
Scanner br=new Scanner([Link]); for(i=0;i<=n-1;i++)
[Link]("Enter number of {
elements:"); [Link](b[i]+"\t");
n=[Link](); }
int a[]=new int [n]; [Link]();
int b[]=new int [n];
for(i=0;i<=n-1;i++) 5 [Link](a,1,b,0,[Link]-1);
{
[Link]("Enter a no:"); for(i=0;i<=n-1;i++)
a[i]=[Link](); {
} [Link](b[i]+"\t");
[Link](a,0,b,0,[Link]); }
for(i=0;i<=n-1;i++) }
{
[Link](b[i]+"\t");
}
}
Three Dimensional array
/* Program for Matrix Multiplication */
class ThreeDArray
{
public static void main(String args[ ])
{
int a[][][]={ { {5,0,2}, {0,0,9}, {4,1,0}, {7,7,7}},
{ {3,0,0}, {8,5,0}, {0,0,0}, {2,0,9}}
};
int count = 0;
for (int i = 0; i < 2; i++)
for (int j= 0; j < 4; j++)
for (int k = 0; k < 3; k++)
if (a[i][j][k]==0)
++count;
[Link]("This Array has "+count+" zeros.");
}
}
• Uneven (or irregular ) multidimensional arrays:
• When you allocate memory for a multidimensional
array,
• you need only specify the memory for the first
(leftmost) dimension . You can allocate the remaining
dimensions separately
int TwoDimensional [][] = new int[4][];
TwoDimensional [0] = new int[5];
TwoDimensional [1] = new int[5];
TwoDimensional [2] = new int[5];
TwoDimensional [3] = new int[5];