[go: up one dir, main page]

0% found this document useful (0 votes)
7 views2 pages

Practical 5

Uploaded by

dnyanesh.agale
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)
7 views2 pages

Practical 5

Uploaded by

dnyanesh.agale
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/ 2

Experiment no 5

Aim:
To write a Program in Java to add two matrices.
The objective of this assignment is to learn Arrays in Java
Theory:
Arrays:
An array is a group/collection of variables of the same type that are referred to by a
common name and an index
Examples:
• Collection of numbers
• Collection of names
• Collection of suffixes

Advantage and disadvantages


Advantage of Array
1. Code Optimization: It makes the code optimized, we can retrieve or sort the data
easily.
2. Random access: We can get any data located at any index position. Disadvantage
of Array
3. Size Limit: We can store only fixed size of elements in the array. It doesn't grow
its size at runtime.

Syntax
•Accessing elements in the array:
•Specific element in the array is accessed by specifying name of the array followed
the index of the element. All array indexes in Java start at zero.

variable-name[index] = value;
eg. marks[0] = 10;
This will assign the value 10 to the 1st element in the array.
marks[2] = 863;

Allocation and declaration


1) STEP 1 : (Declaration)
int marks[];
marks null

2) STEP 2: (Memory Allocation)


marks = new int[5];
•marks marks[0] marks[1] marks[2] marks[3] marks[4]

3) STEP 3: (Accessing Elements)


marks[0] = 10;
•marks marks[0] marks[1] marks[2] marks[3] marks[4]

Alternative Syntax
Combined declaration & memory allocation:
data-type variable-name[] = new data-type[size];
eg. int marks[] = new int[5];
This will declare an int array ‘marks’ and will also allocate memory
of 5 integers to it.

•Combined declaration, allocation & assignment:


data-type variable-name[] = {comma-separated values};
•eg. int marks[] = {10, 35, 84, 23, 5};

This will declare an int array ‘marks’, will allocate memory


of 5 integer to it and will also assign the values as

Multi-Dimensional Arrays (2D, 3D … arrays)


Multi dimensional arrays represent 2D, 3D ..arrays. A two dimensional array is
combination of two or more (1D) one dimensional arrays. A three dimensional array
is a combination of two or more (2D) two dimensional arrays.
Two Dimensional Arrays (2d array): A two dimensional array represents several rows
and columns data.To represent a two dimensional array, we should use two pair of
square braces [ ] [ ] after the array name.
For example:- The marks obtained by a group of students in five different subjects can
be represented by a 2D array.

2 D array declaration
We can declare a two dimensional array and directly store elements at the time of its
declaration,
as:
int marks[] [] = {{50, 60, 55, 67, 70},{62, 65, 70, 70, 81}, {72, 66, 77, 80, 69} };
We can create a two dimensional array by declaring the array first and then we can
allocate memory for it by using new operator as:

int marks[ ] [ ]; //declare marks array


marks = new int[3][5]; //allot memory for storing 15 elements.
These two statements also can be written as:
int marks [ ][ ] = new int[3][5];

Program :

Output:

Conclusion-

Questions-
1) What is an array ?What are applications of Arrays?
2) What is difference between array and vector?
3) What is need of Wrapper class in Java?

You might also like