0% found this document useful (0 votes)
2 views23 pages

Array

The document provides an overview of arrays in Java, including their declaration, initialization, and access methods. It explains both one-dimensional and multidimensional arrays, highlighting how to declare and initialize them, as well as how to iterate through their elements. Additionally, it discusses practical examples such as searching for elements and finding maximum values in arrays.

Uploaded by

cfcl365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

Array

The document provides an overview of arrays in Java, including their declaration, initialization, and access methods. It explains both one-dimensional and multidimensional arrays, highlighting how to declare and initialize them, as well as how to iterate through their elements. Additionally, it discusses practical examples such as searching for elements and finding maximum values in arrays.

Uploaded by

cfcl365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Array

Rakib Mahmud
Introduction
• Array is a collection of similar data elements.
• For understanding the array, we need to understand
how it actually works. To understand this, we need to
follow the flow mentioned below:
• Declare
• Initialize
• Access
Declaring an Array

For Example:
int A[];
Or, int[ ] A;
How many elements can array this
hold?
For Example:
A = new int[x]

Combining both statements in one


For Example:
type var-name[ ] = new type [size]; int A [ ] = new int [ 5 ];
Or, Or,
type[ ] var-name= new type int[ ] A = new int [ 5 ];
[size];
How many elements can array this
hold? (Cont’d)
int A [ ] = new int [ 5 ];

• Where A[ ] is the reference. Means it does not store the


actual array values. Instead, it stores information about
where the object is stored in memory.
• int [5] is the object.
• Where object is created in the heap.
• And the reference is either in stack or heap.
Initialization an Array in Java
int A [ ] = new int [ 5 ]; We can also declare and
initialize an array at the
same time.
We can initialize arrays in
Java using index number.
For example: int A [ ] = { 1, 5, 4, 2, 13 }
A [ 0 ] = 1;
A [ 1 ] = 5; Now, if we want to find it’s
A [ 2 ] = 4; length
A [ 3 ] = 2; then “A.length” need to be
A [ 4 ] = 13; used.
How to Access Elements of an Array
in Java?
• We can access the element of an array using the index
number. Here is the syntax for accessing elements of an
array,
How to Access Elements of an Array
in Java?
(Cont’d)
How to Access Elements of an Array
in Java?
(Cont’d)
• We can use loops to access all the elements of the array
at once.
How to Access Elements of an Array
in Java?
(Cont’d)
• We can also use the for-each loop to iterate through the
elements of an array.
• The for-each loop in Java (also called the enhanced for
loop) was introduced in Java 5 to simplify iteration over
arrays.
How to Access Elements of an Array
in Java?
(Cont’d)
Write a Java Program for Searching
an Element from a 1D Array
Write a Java Program for Finding The
Maximum Element from a 1D Array
Write a Java Program for Finding The
second Maximum Element from a
1D Array
Java Multidimensional Arrays
Declaring
• A multidimensional array is an array of arrays.
• Each element of a multidimensional array is an array
itself.
• For example:

• Here, we have created a multidimensional array


named a.
• It is a 2-dimensional array, that can hold a maximum of
Java Multidimensional Arrays
Declaring (Cont’d)
int [ ] [ ] a;
Column 1 Column 2 Column 3 Column 4
a = new int [ 3 ] [ ] Row 1 a [0][0] a[0][1] a[0][2]
a[0]= new int [ 3 ] Row 2 a[1][0] a[1][1] a[1][2] a[1][3]
a[1]= new int [ 4 ] Row 3 a[2][0]
a[2]= new int [ 1]

• This type of array is known as jagged array.


Java Multidimensional Arrays
(Cont’d)
• Let's take another example of the multidimensional
array.
• This time we will be creating a 3-dimensional array. For
example,
int[ ] [ ] [ ] data = new int [ 3 ] [ 4 ] [ 2 ]

• Here, data is a 3d array that can hold a maximum of 24


(3*4*2) elements of type int.
How to initialize a 2d array in Java?
Example: 2-dimensional Array
Print all elements of 2d array Using
For Loop
Print all elements of 2d array Using
For…each Loop
Example: 3-dimensional Array
Thank You

You might also like