Вы находитесь на странице: 1из 57

Object Oriented

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

• 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 array.length 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];
System.out.println("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];
System.out.println("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 java.util.*; for(i=0;i<=n-1;i++)
class Array {
{ System.out.println(a[i]);
public static void main(String args[]) }
{ }
int n,i; }
Scanner sc=new Scanner (System.in); /***Output******
System.out.print("Enter no of elements:"); Enter no of element:4
n=sc.nextInt(); 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
System.out.print("Enter a no:");
a[i]=sc.nextInt();
}
WAP Program to accept 'n‘ integer from user into an array & display the average
of these number .
import java.util.*; 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; System.out.println("Average="+avg);
float avg; }
Scanner sc=new Scanner(System.in); }
System.out.print("Enter no of elements:"); /* output
n=sc.nextInt(); 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
System.out.print("Enter a no:"); Enter a no:4
a[i]=sc.nextInt(); Avarage=2.5
} */
/* WAP to find the largest of 'n' number taken from user */
import java.util.*; 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(System.in); System.out.println("Largest no=“
System.out.print("Enter no of elements:"); +large);
n=sc.nextInt(); }
int a[]=new int [n]; }
for(i=0;i<=n-1;i++) /******output******
Enter no of elements :4
{ Enter a no.:23
System.out.print("Enter a no:"); Enter a no.:43
a[i]=sc.nextInt(); Enter a no.: 1
} Enter a no.:34
Largest no=43
*/
/*Wap to find the smallest of n numbers taken from user */

import java.util.*; 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(System.in); System.out.println("Smallest
System.out.print("Enter no of no="+small);
}
elements:"); }
n=sc.nextInt(); /********output *********
int a[]=new int [n]; Enter no of elements :4
for(i=0;i<=n-1;i++) Enter a no.:23
{ System.out.print("Enter a no:"); Enter a no.:43
Enter a no.: 1
a[i]=sc.nextInt(); 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 java.util.*; 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(System.in); temp=a[j];
System.out.print("Enter no of elements:"); a[j]=a[j+1];
n=sc.nextInt(); a[j+1]=temp;
int a[]=new int [n]; }}}
for(i=0;i<=n-1;i++) System.out.println("After Sorting");
{ for(i=0;i<=n-1;i++)
System.out.print("Enter a no:"); {
a[i]=sc.nextInt(); System.out.println(a[i]);
} }
}
}
WAP to sort numbers in descending order Or
WAP to implement bubble sorting algorithm for sorting numbers in descending order

import java.util.*; 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(System.in); } }
System.out.print("Enter no of elements:"); System.out.println("After Sorting");
n=sc.nextInt();; for(i=0;i<=n-1;i++)
int a[]=new int [n]; { System.out.println(a[i]);
for(i=0;i<=n-1;i++) } } }
{ /******output ******
System.out.print("Enter a no:"); Enter no of elements :5
a[i]=sc.nextInt();; 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 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 :

e.g [int size=number.length ]


//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 =number.length; number[j+1]=temp;
}
for(int i=0;i<=size-1;i++) }
}
System.out.print(number[i]+”\t”); System.out.println("After Sorting");
for(i=0;i<=size-1;i++)
System.out.println(); {
System.out.println(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] ;
}
System.out.println("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 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");

// Copying last 5 elements of Array1 to Array2


System.arraycopy(Array1,5,Array2,5,5);
System.out.println("The second array after calling arraycopy( ) : ");
for (i = 0; i < 10; i++)
System.out.print(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 java.util.*;
class ArrayCopy{ System.out.println();
public static void main(String args[])
{ System.arraycopy(a,0,b,1,a.length-1);
int n,i;
Scanner br=new Scanner(System.in); for(i=0;i<=n-1;i++)
System.out.print("Enter number of {
elements:"); System.out.print(b[i]+"\t");
n=br.nextInt(); }
int a[]=new int [n]; System.out.println();
int b[]=new int [n];
for(i=0;i<=n-1;i++) 5 System.arraycopy(a,1,b,0,a.length-1);
{
System.out.print("Enter a no:"); for(i=0;i<=n-1;i++)
a[i]=br.nextInt(); {
} System.out.print(b[i]+"\t");
System.arraycopy(a,0,b,0,a.length); }
for(i=0;i<=n-1;i++) }
{
System.out.print(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;
System.out.println("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];

Вам также может понравиться