Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views16 pages

Lecture 14. Array

Uploaded by

Mihoyo Impact
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)
13 views16 pages

Lecture 14. Array

Uploaded by

Mihoyo Impact
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/ 16

Object Oriented Programming

JAVA ARRAY

Md. Nurul Islam


Lecturer
Dept. of CSE
University of Scholars
What is an Java Array?

• An array is a collection of similar type of elements which has


contiguous memory location.

• Java array is an object which contains elements of a similar data


type.

• Array in Java is index-based, the first element of the array is


stored at the 0th index, 2nd element is stored on 1st index and
so on.
Array
Array

Advantages
• Code Optimization: It makes the code optimized, we can retrieve or sort the
data efficiently.
• Random access: We can get any data located at an index position.

Disadvantages
• Size Limit: We can store only the fixed size of elements in the array. It doesn't
grow its size at runtime. To solve this problem, collection framework is used in
Java which grows automatically.
Types of Array

There are two types of array:

• Single Dimensional Array


• Multidimensional Array
Single Dimensional Array in Java

Syntax to Declare an Array in Java

1. dataType[] arr;
2. dataType []arr;
3. dataType arr[];

Instantiation of an Array in Java


arrayRefVar=new datatype[size];
Example

class Testarray{ Output:


public static void main(String args[]){ 10
int a[]=new int[4]; //declaration and instantiation 20
a[0]=10; //initialization
a[1]=20; 70
a[2]=70; 40
a[3]=40;
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Single Dimensional Array in Java

Declaration, Instantiation and Initialization of Java Array:

We can declare, instantiate and initialize the java array


together by:

int a[]={33,3,4,5}; //declaration, instantiation and initialization


Passing Array to a Method in Java
class Testarray2{
//creating a method which receives an array as a parameter Output:
static void min(int arr[]){ 3
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i]) {
min=arr[i]; }
System.out.println(min);
}

public static void main(String args[]){


int a[]={33,3,4,5}; //declaring and initializing an array
min(a); //passing array to method
}}
Multidimensional Array
Syntax to Declare Multidimensional Array in Java

1. dataType[][] arrayRefVar;
2. dataType [][]arrayRefVar;
3. dataType arrayRefVar[][];
4. dataType []arrayRefVar[];

Example to instantiate Multidimensional Array in Java


int[][] arr=new int[3][3];//3 row and 3 column
Multidimensional Array

Example to initialize Multidimensional Array in Java

arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Multidimensional Array
class Testarray5{ for(int i=0;i<2;i++){
public static void main(String args[]){ for(int j=0;j<3;j++){
int a[][]={{1,3,4},{3,4,5}}; c[i][j]=a[i][j]+b[i][j];
int b[][]={{1,3,4},{3,4,5}}; System.out.print(c[i][j]+" ");
}
//matrix to store the sum of two matrices System.out.println(); //new line
int c[][]=new int[2][3]; }
}} Output:
//adding and printing addition of 2 matrices
268
6 8 10
Multiplication of 2 Matrices in Java
Multiplication of 2 Matrices in Java
//Java Program to multiply two matrices for(int i=0;i<3;i++){
public class MatrixMultiplicationExample{ for(int j=0;j<3;j++){
public static void main(String args[]){ c[i][j]=0;
//creating two matrices for(int k=0;k<3;k++)
int a[][]={{1,1,1},{2,2,2},{3,3,3}}; {
int b[][]={{1,1,1},{2,2,2},{3,3,3}}; c[i][j]+=a[i][k]*b[k][j];
} //end of k loop
//creating matrix to store the multiplication of System.out.print(c[i][j]+" ");
two matrices
//printing matrix element
int c[][]=new int[3][3]; //3 rows and 3 columns } //end of j loop
System.out.println(); //new line
//multiplying multiplication of 2 matrices }
}}
Multiplication of 2 Matrices in Java

Output:

666
12 12 12
18 18 18
Thank You

You might also like