0% found this document useful (0 votes)
4 views33 pages

Arrays PPT

The document provides an overview of arrays in Java, detailing their definition, types (single-dimensional and multi-dimensional), and how to create and initialize them. It also covers searching techniques like linear and binary search, as well as sorting techniques including selection and bubble sort. Additionally, it discusses the advantages and disadvantages of using arrays.

Uploaded by

Shraddha Bardiya
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)
4 views33 pages

Arrays PPT

The document provides an overview of arrays in Java, detailing their definition, types (single-dimensional and multi-dimensional), and how to create and initialize them. It also covers searching techniques like linear and binary search, as well as sorting techniques including selection and bubble sort. Additionally, it discusses the advantages and disadvantages of using arrays.

Uploaded by

Shraddha Bardiya
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

Arrays

WHAT ARE ARRAYS, TYPES OF ARRAYS, HOW TO CREATE AN


ARRAY,INITIALIZE AN ARRAY,ADVANTAGES AND DISADVANTAGES OF AN
ARRAY.
What is an array?
An array is a collection of similar type of elements which has
contiguous memory location.
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.
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.
What is an array?
Array can contains primitives (int, char, etc) as well as object (or
non-primitives) references of a class depending on the definition of
array. In case of primitives data types, the actual values are stored
in contiguous memory locations.
Types of arrays in Java

There are two types of array.


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


arrayRefVar=new datatype[size];
Single Dimensional Array in Java
For-each Loop for Java Array
For-each is another array traversing technique like for loop, while loop, do-while
loop introduced in Java 5.

It starts with the keyword for like a normal for-loop.


Instead of declaring and initializing a loop counter variable, you declare a
variable that is the same type as the base type of the array, followed by a colon,
which is then followed by the array name.
In the loop body, you can use the loop variable you created rather than using an
indexed array element.
It’s commonly used to iterate over an array or a Collections class
For-each Loop for Java Array
We can also print the Java array using for-each loop. The
Java for-each loop prints the array elements one by one. It
holds an array element in a variable, then executes the body
of the loop.
The syntax of the for-each loop is given below:
for( data_type variable : array) {
//body of the loop
}
For-each Loop for Java Array-Example
ArrayIndexOutOfBoundsException
The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if
length of the array in negative, equal to the array size or greater than the array size
while traversing the array.
Passing Array to a Method in Java
Multidimensional Array in Java

Multidimensional arrays are arrays of arrays with each element of the array holding the
reference of other array.

A multidimensional array is created by appending one set of square brackets ([ ])


per dimension.
Examples:
int [ ] [ ] intArray = new int [10] [20] ; //a 2D array or matrix
int [ ] [ ] [ ] intArray = new int [10] [20] [10] ; //a 3D array
Multidimensional Array in Java
Multidimensional Array in Java
Searching Techniques
Linear Search and Binary Search
Linear Search or Sequential
Search
Algorithm
 A simple approach is to
do linear search, i.e
 Start from the leftmost
element of arr[] and one by one
compare x with each element
of arr[]
 If x matches with an element,
return the index.
 If x doesn’t match with any of
elements, return -1.
Linear Search or Sequential
Search
Code Snippet
int n = [Link];
for(int i = 0; i < n; i++)
{
if(arr[i] == x) //x is search term
return i;
}
return -1;
Binary Search
Search a sorted array by
repeatedly dividing the search
interval in half. Begin with an
interval covering the whole
array. If the value of the search
key is less than the item in the
middle of the interval, narrow
the interval to the lower half.
Otherwise narrow it to the
upper half. Repeatedly check
until the value is found or the
interval is empty.
Binary Search
 We basically ignore half of the
elements just after one
comparison.
 Compare x with the middle
element.
 If x matches with middle
element, we return the mid
index.
 Else If x is greater than the mid
element, then x can only lie in
right half subarray after the mid
element. So we recur for right
half.
 Else (x is smaller) recur for the
left half.
Binary Search
int l = 0, r = [Link] - 1;

while (l <= r) {

int m = (r+l)/ 2;

// Check if x is present at mid

if (arr[m] == x)

return m;

// If x greater, ignore left half

if (arr[m] < x)

l = m + 1;

// If x is smaller, ignore right half

else

r = m - 1;

// if we reach here, then element was

// not present

return -1;
SORTING TECHNIQUES
Selection and Bubble sorting
In
Java
SELECTION
0 1 2 SORTING
3 4 5 6

Code snippet 30 56 5 19 27 87 43
for (int i=0; i < [Link]-1; i++) 30 56 5 19 27 87 43
{ 5 56 30 19 27 87 43
for (int j=i+1;j<[Link];j++)
5 56 30 19 27 87 43
{
if (a[i]>a[j]) 5 56 30 19 27 87 43
//swap the values 5 56 30 19 27 87 43
} 5 56 30 19 27 87 43
}
SELECTION
0 1 2 SORTING
3 4 5 6
5 56 30 19 27 87 43
Code snippet
for (int i=0; i < [Link]-1; i++) 5 30 56 19 27 87 43
{ 5 30 56 19 27 87 43
for (int j=i+1;j<[Link];j++)
5 19 56 30 27 87 43
{
if (a[i]>a[j]) 5 19 56 30 27 87 43
//swap the values 5 19 56 30 27 87 43
} 5 19 56 30 27 87 43
}
SELECTION SORTING
0 1 2 3 4 5 6
Code snippet 5 19 56 30 27 87 43
for (int i=0; i < [Link]-1; i++) 5 19 30 56 27 87 43
{ 5 19 30 56 27 87 43
for (int j=i+1;j<[Link];j++)
5 19 27 56 30 87 43
{
if (a[i]>a[j]) 5 19 27 56 30 87 43
//swap the values 5 19 27 56 30 87 43
}
}
SELECTION SORTING
0 1 2 3 4 5 6
Code snippet 5 19 27 56 30 87 43
for (int i=0; i < [Link]-1; i++) 5 19 27 30 56 87 43
{ 5 19 27 30 56 87 43
for (int j=i+1;j<[Link];j++)
5 19 27 30 56 87 43
{
if (a[i]>a[j])
//swap the values
}
}
SELECTION SORTING
0 1 2 3 4 5 6
Code snippet 5 19 27 30 56 87 43
for (int i=0; i < [Link]-1; i++) 5 19 27 30 56 87 43
{ 5 19 27 30 43 87 56
for (int j=i+1;j<[Link];j++)
{
if (a[i]>a[j])
//swap the values
}
}
SELECTION SORTING
0 1 2 3 4 5 6
Code snippet 5 19 27 30 43 87 56
for (int i=0; i < [Link]-1; i++) 5 19 27 30 43 56 87
{
for (int j=i+1;j<[Link];j++)
{
if (a[i]>a[j])
//swap the values
}
}
SELECTION SORTING

5 19 27 30 43 56 87
BUBBLE
0 1 2 SORTING
3 4 5 6

Code snippet 30 56 5 19 27 87 43
for (int i=0; i < [Link]-1; i++) 30 56 5 19 27 87 43
{
for (int j=0;j<[Link]-1;j++) 30 5 56 19 27 87 43
{ 30 5 56 19 27 87 43
if (a[j]>a[j+1])
//swap the values
30 5 19 56 27 87 43
{ temp=a[j]; 30 5 19 56 27 87 43
a[j]=a[j+1];
30 5 19 27 56 87 43
a[j+1]=temp; }
} 30 5 19 27 56 87 43
} 30 5 19 27 56 87 43
30 5 19 27 56 43 87
BUBBLE
0 1 2 SORTING
3 4 5 6

Code snippet 30 5 19 27 56 43 87
for (int i=0; i < [Link]-1; i++) 5 30 19 27 56 43 87
{ 5 30 19 27 56 43 87
for (int j=0;j<[Link]-1;j++)
5 19 30 27 56 43 87
{
if (a[j]>a[j+1]) 5 19 30 27 56 43 87
//swap the values 5 19 27 30 56 43 87
} 5 19 27 30 56 43 87
}
5 19 27 30 56 43 87
5 19 27 30 43 56 87
5 19 27 30 43 56 87
BUBBLE
0 1 2 SORTING
3 4 5 6

Code snippet 5 19 27 30 43 56 87
for (int i=0; i < [Link]; i++)
{
for (int j=0;j<[Link]-1;j++)
{
if (a[j]>a[j+1])
//swap the values
//counter can be set to stop
//the iteration once array is
//sorted
}
}
BUBBLE SORTING

5 19 27 30 43 56 87

You might also like