0% found this document useful (0 votes)
9 views

Array

This document provides an overview of arrays in Java. It defines an array as a group of variables of the same type that share a common name. Arrays can be one-dimensional or multi-dimensional. The key points covered are: creating and initializing arrays; accessing array elements using indexes; finding the length of an array; and examples of one-dimensional and two-dimensional arrays, including adding elements and matrices.

Uploaded by

NIELIT Haridwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Array

This document provides an overview of arrays in Java. It defines an array as a group of variables of the same type that share a common name. Arrays can be one-dimensional or multi-dimensional. The key points covered are: creating and initializing arrays; accessing array elements using indexes; finding the length of an array; and examples of one-dimensional and two-dimensional arrays, including adding elements and matrices.

Uploaded by

NIELIT Haridwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

TOPIC: Arrays

COURSE: JAVA PROGRAMMING

PPT SL.NO.: 01 of 21

VERSION: 01 LAST UPDATED ON: 28/05/2021

Presentation By: Kailash Joshi


Array

 An array is a group of variables of the


same type that share a common name.

 Array of any type may be created.

 Each individual data item of an array is


called element.
Array
 Any element of the array may be accessed by
using array name and an index (also known as
array subscript).

 Index of the first element in an array is 0 (zero);

 Index of the last element in an array of size N, is


N-1.
Array
Array may be classified under two categories:
 1 – Dimensional Array
 N – Dimensional Array

(2 – Dimensional Array comes under the category of N – Dimensional


Array)
One Dimensional Array
creating an array in Java, is a 3 step process:

1) Create an array variable of the desired type.


type arrayName[ ];
OR
type[ ] arrayName;

2) Create physical array for the array variable.


arrayName = new type[SIZE];

3) Put values in the array elements


arrayName[index] = value;
the value of index may range from 0 to SIZE-1
An Example:

// creation of array variable month_days


int month_days[ ];
0 31
// creation of physical array
month_days = new int[12];
1 28
// Putting values in the elements 2
31
month_days[0] = 31; 3
month_days[1] = 28;
30
4
month_days[2] = 31; 31
month_days[3] = 30; 5
30
month_days[4] = 31; 6
month_days[5] = 30;
31
7
month_days[6] = 31; 31
month_days[7] = 31; 8
30
month_days[8] = 30; 9
31
month_days[9] = 31;
10
month_days[10] = 30; 30
month_days[11] = 31; 11
31
An Example:
Initialization of Arrays
type arrayName[ ] = {list of values};

e.g. int number[ ] = {35,40,20,25,19};

NOTE:
Unlike C, Java has Array Bounds Checking. Trying to access
an array element beyond its boundaries will generate an error
message. e.g.
number[5]=3; will generate an error message, as the last
element of the array will be number[4].
Using Loop to Assign Values
class arrayTest{
public static void main(String args[ ]){
int a[ ] = new int[10]; 0 1

1 3
for(int i=0;i<10;i++){ 2 5

a[i] = 2 * i +1; 3 7

4 9
System.out.println(a[i]); 5 11
6 13
}
7 15
} 8 17

9 19
}
Using Loop to Assign Values
Using Scanner to Assign Values
Array Length

In Java, arrays are implemented as objects. To find the


size of an array we may use the instance variable length
as follows:

int size = a.length; // a is the name of the array


Array Length
Array Length
Multidimensional Arrays

In Java, multi-dimensional arrays are actually arrays of arrays.

Multidimensional arrays are declared as:


• int A[ ][ ] = new int [4][5];
Right index
determines
columns

A[0][0] A[0][1] A[0][2] A[0][3] A[0][4]


A[1][0] A[1][1] A[1][2] A[1][3] A[1][4]
Left index
determines A[2][0] A[2][1] A[2][2] A[2][3] A[2][4]
rows
A[3][0] A[3][1] A[3][2] A[3][3] A[3][4]
Example: A program for 2D array

class TwoDimensional1
{
public static void main(String args[])
{
int[][] a={{10,20},{30,40}};//declaration and initialization
System.out.println("Two dimensional array elements are");
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[1][0]);
System.out.println(a[1][1]);
}}
Example:
Example:
Example:
Example: A program to add two matrices

Class AddMatrices{
public static void main (String args[ ]{
int m1[ ][ ]={ {1,2,1}, {0,1,2}};
int m2[ ][ ]={ {4,3,7}, {8,1,5}};
int m3[ ][ ]=new int[2][3];
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
System.out.println(“m3[” + i + “][” + j + “] : ” + m3[i][j]);
}}}
Thank you!

www.nielit.gov.in/haridwar

You might also like