You are on page 1of 18

Arrays in JAVA

Arrays
• An array is a collection of similar type of elements.
• 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.
• Array in Java is index-based.
• A specific element in an array is accessed by its index.
• The variables in array are ordered & each has an index beginning from 0.
• Arrays are objects in Java, we can find their length using the object
property length.

2
Arrays
• 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.

3
Types of Arrays

• There are two types of array.


• Single/One Dimensional Array
• Multidimensional 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.

4
Single/One-Dimensional Arrays
• Use single index to store elements.
• To create an array, first must create an array variable of the desired type.
• The general form of a one-dimensional array declaration is
type var-name[ ];
OR
type[] var_name;
• Example- int month_days[];
OR
int[] month_days;

5
Single/One-Dimensional Arrays
• Declaration and Initialization of Array
• To allocate memory for arrays, new keyword is used.
• Example array-var=new type [size];
number=new int[5];
Array Initialization using Index
number=new int[5]; Declaration and instantiation
number[0]=10; Initialization
number[1]=20;
number[3]=40;
number[4]=50;
Array will be 10,20,0,40,50 6
Single/One-Dimensional Arrays

• Initialize while declaring


• Initialize the whole array while declaring it just by using curly brackets {…….}
• String must be in quotes
• Example:
• int [ ] my_arr= {1,2,3,4,5};
• String [ ] list= {“C”, “C++”, “PHP”, “PYTHON”};

7
Array Example
class Array{
public static void main(String args[]) class Array1{
{ public static void main(String args[])
int a[]=new int[5]; {
a[0]=10;
a[1]=20; int a[]={10,20,30,40,50};
a[2]=70;
a[3]=40; for(int i=0;i<a.length;i++)
a[4]=50; System.out.println(a[i]);
for(int i=0;i<a.length;i++)
System.out.println(a[i]); }
} }
}
8
Array Example
public class Array3 {
public static void main (String[] args)
{
Student[] arr; // declares an Array of integers.
arr = new Student[5]; // allocating memory for 5 objects of type Student.
arr[0] = new Student(1,“Ajay"); // initialize the first elements of the array
arr[1] = new Student(2,“Vaibhav"); // initialize the second elements of the array
arr[2] = new Student(3,“Shital");
arr[3] = new Student(4,“Mahesh");
arr[4] = new Student(5,“Mohit");
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : " +arr[i].roll_no +" "+ arr[i].name);
}
}

9
Multi-Dimensional Arrays
• It is used when data is stored in row and column based index
• It is also known as matrix form.
• In Java, multidimensional arrays are actually arrays of arrays.
• These look and act like regular multidimensional arrays.
• To declare a multidimensional array variable, specify each additional
index using another set of square brackets.

• Following declares a two dimensional array variable called two:


int two[][] = new int[4][5]; //4 row and 5 column

10
Multi-Dimensional Arrays

11
Multi-Dimensional Arrays
• How to instantiate Multidimensional Array in Java.
int[][] arr=new int[3][3]; //3 row and 3 column
• 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; 12
Multi-Dimensional Arrays Example
Program to display elements of matrix using Multidimensional array.
}
class Array3{
public static void main(String args[]) System.out.println();
{ }
//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]+" ");

13
Matrix Addition using Array

14
Multi-Dimensional Arrays Example
Program to perform matrix addition using multidimensional array.
class Aarray5{
public static void main(String args[]){
int a[][]={{1,3,4},{3,4,5}}; //creating two matrices
int b[][]={{1,3,4},{3,4,5}};
int c[][]=new int[2][3]; //creating another matrix to store the sum of two matrices
//adding and printing addition of 2 matrices
for(int i=0;i<2;i++) {
for(int j=0;j<3;j++) {
c[i][j]=a[i][j]+b[i][j];
System.out.print("\t"+c[i][j]);
}
System.out.println();//new line
} } } 15
Jagged Array in Java
• Jagged array means to have a multi dimensional array with uneven size of
columns in it.
• It is an array of arrays with different number of columns.
• Initialization of Jagged Array
• We must set column size for each row independently
int [][]arr= new int[3][];
arr[0]=new int[3]; int [][] arr={
OR
arr[1]=new int[4]; new int{1,2,3},
new int{4,5,6,7},
arr[2]=new int[5];
new int{10,11,12,13,14}
16
}
17
Example:-Jagged Array in Java
public class Main { // Displaying the elements of 2-D Jagged array
public static void main(String[] args) { System.out.println("Elements of 2-D Jagged
// Declaring a 2-D array with 3 rows Array");
int arr[][] = new int[3][];
for (int i = 0; i < arr.length; i++) {
// create a jagged array
for (int j = 0; j < arr[i].length; j++)
arr[0] = new int[]{99,100,101};
arr[1] = new int[]{199,200}; System.out.print(arr[i][j] + " ");
arr[2] = new int[]{299,300,301,302,303}; System.out.println();
}
}
}
18

You might also like