0% found this document useful (0 votes)
25 views4 pages

Experiment 5

Uploaded by

69bunt Y
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)
25 views4 pages

Experiment 5

Uploaded by

69bunt Y
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/ 4

Experiment No.

: 05

Title: Program to demonstrate the use of arrays.

Theory:

Java Arrays

Normally, an array is a collection of similar types of elements which have contiguous


memory locations.

Java array is an object which contains elements of a similar data type. Additionally,
The elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java array.

Arrays in Java are index-based, the first element of the array is stored at the 0th index,
2nd element is stored on the 1st index and so on.

In Java, an array is an object of a dynamically generated class. Java array inherits the
Object class, and implements the Serializable as well as Cloneable interfaces. We can store
primitive values or objects in an array in Java. Like C/C++, we can also create single
dimensional or multidimensional arrays in Java.

Advantages

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

Disadvantages

o 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, a collection framework is used in Java
which grows automatically.

Types of Array in java

There are two types of arrays.

o Single Dimensional Array


o Multidimensional Array
Single Dimensional Array in Java Syntax to Declare an Array in Java

1. dataType[] arr; (or)


2. dataType []arr; (or)
3. dataType arr[];

Instantiation of an Array in Java

Array_Ref_Var = new datatype[size];

Program 1: Java Program to illustrate how to declare, instantiate, initialize and traverse the Java
array.

//Java Program to illustrate how to declare, instantiate, initialize and traverse the
Java array.

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

Output

10
20
70
40
50

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


Program 2: Java Program to illustrate the use of declaration, instantiation and initialization
of Java array in a single line.

class Testarray2{
public static void main(String args[]) {
int a[]={33,3,4,5}; //declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
}
}

Output

33
3
4
5

Multidimensional Array in Java

In such case, data is stored in row and column based index (also known as matrix
form).

Syntax to instantiate Multidimensional Array in Java

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

Program 3: Java Program to illustrate the use of multidimensional array

class Testarray3{
public static void main(String args[]) {
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Output

123
245
445

Conclusion: In this way, we have studied how to use array for storing various types
of elements in java

You might also like