SlideShare a Scribd company logo
1 of 40
Array in Java
Types of Arrays
Topics For Today’s Discussion
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Topics for Today’s Session
DeclaringAccessingUpdatingCreatingWorking with Array
Sorting in Arrays
Searching in Arrays
Java Arrays
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Array
Array is a static
Data Structure
Contains
collections of
homogenous
elements
All the
elements are
stored under
one variable
name
It occupies a
contiguous
memory location
Types of Arrays
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Types Of Arrays
01Single
Dimensional
Single Dimensional or
1-D array is a type of
linear array in which
elements are stored in
a continuous row
02Two
Dimensional
Two Dimensional or 2-D
array is a type of
matrix in which
elements are stored in
rows and columns
03Multi
Dimensional
Multi Dimensional
array is a type of
nested array
Working With Arrays
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Working With Arrays – 1D
arrayRefVar = new dataType[arraySize];
=myArray new int[5]
Declaring and Initializing an One Dimensional Array
datatype[] arrayRefVar = new dataType[arraySize];
=int[5] myArray new int[5]
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Working With Arrays – 1D
myArray
myArray[0] myArray[1] myArray[2] myArray[3] myArray[4]
=myArray[0] 10
Declaring and Initializing an One Dimensional Array
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Working With Arrays – 1D
myArray
myArray[0] myArray[1] myArray[2] myArray[3] myArray[4]
=myArray[2]
10
30
=myArray[3] 40
=myArray[4] 50
=myArray[1] 20
Declaring and Initializing an One Dimensional Array
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
myArray
myArray[0] myArray[1] myArray[2] myArray[3] myArray[4]
10 30 40 5020
Working With Arrays – 1D
Declaring and Initializing an One Dimensional Array
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
10 30 40 5020
Working With Arrays – 1D
dataType[] arrayRefVar = new dataType{e1,e2,e3,…,eN};
myArray[0] myArray[1] myArray[2] myArray[3] myArray[4]
=int[] myArray new int{10,20,30,40,50}
Declaring and Initializing an One Dimensional Array
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
myArray[0] myArray[1] myArray[2] myArray[3] myArray[4]
10 30 40 5020
Working With Arrays – 1D
int[] myArray
dataType[] arrayRefVar = new dataType{e1,e2,e3,…,eN};
Declaring and Initializing an One Dimensional Array
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Working With Arrays – 1D
Accessing a specific array element
myArray[0]
myArray[1]
myArray[2]
myArray[3]
myArray[4]
10
30
40
50
20
arrayRefVar[index]
myArray[4]
myArray[1]
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
100
Working With Arrays – 1D
Accessing a specific array element
myArray[0]
myArray[1]
myArray[2]
myArray[3]
myArray[4]
10
30
40
50
20
arrayRefVar[index]
myArray[4]
myArray[1]
Updating a specific array element
arrayRefVar[index] = newValue
myArray[4] = 100
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
myArray[4] = 100
Working With Arrays – 1D
Accessing a specific array element
myArray[0]
myArray[1]
myArray[2]
myArray[3]
myArray[4]
10
30
50
20
arrayRefVar[index]
myArray[4]
myArray[1]
Updating a specific array element
arrayRefVar[index] = newValue
100
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Working With Arrays – 2D
Declaring and Initializing an Two Dimensional Array
datatype[][] arrayRefVar = new dataType[row][col];
=int[][] myArray new int[2][2]
myArray[0][0] myArray[0][1]
myArray[1][0] myArray[1][1]
=myArray[0][0] 100
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
myArray[0][0] myArray[0][1]
myArray[1][0] myArray[1][1]
Working With Arrays – 2D
Declaring and Initializing an Two Dimensional Array
datatype[][] arrayRefVar = new dataType[row][col];
=int[][] myArray new int[2][2]
=myArray[0][1] 200
100 =myArray[1][0] 300
=myArray[1][1] 400
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
myArray[0][0] myArray[0][1]
myArray[1][0] myArray[1][1]
Working With Arrays – 2D
Declaring and Initializing an Two Dimensional Array
datatype[][] arrayRefVar = new dataType[row][col];
=int[][] myArray new int[2][2]
100 200
300 400
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
564
myArray[0][0] myArray[0][1]
myArray[1][0] myArray[1][1]
Working With Arrays – 2D
arrayRefVar[row][col]
100 200
300 400
Accessing a specific array element
myArray[0][1]
Updating a specific array element
arrayRefVar[row][col] = newValue
myArray[0][1] = 564
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
myArray[0][1] = 564
myArray[0][0] myArray[0][1]
myArray[1][0] myArray[1][1]
Working With Arrays – 2D
arrayRefVar[row][col]
100
300 400
Accessing a specific array element
myArray[0][1]
Updating a specific array element
arrayRefVar[row][col] = newValue
564
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Operations With Arrays
1 2 3
6
987
4 5
1 2 1
3
271
5 4
Addition of Matrices
a[3][3] b[3][3]
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Operations With Arrays
1 2 3
6
987
4 5
1 2 1
3
271
5 4
2 4 4
9
11158
9 9
a[3][3]
b[3][3]
c[3][3]
int[][] c = new int[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
1 2 3
6
987
4 5
1 2 1
3
271
5 4
Division
a[3][3] b[3][3]
Operations With Arrays
Multiplication
Subtraction
Sorting in Arrays
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Sorting in Array
Java offers various sorting algorithms that helps in putting elements of a list in a certain order
Bubble Sort Insertion Sort Merge Sort
Quick SortSelection Sort
Types of Sorting Algorithms
Merge SortQuick SortInsertion SortSelection SortBubble Sort
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
4 1 10 -3 12
4 1 10 -3 12
1 4 10 -3 12
1 4 10 -3 12
1 4 -3 10 12
1 4 -3 10 12
1 -3 4 10 12
1 -3 4 10 12
1 -3 4 10 12
-3 1 4 10 12
-3 1 4 10 12
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
Merge SortQuick SortInsertion SortSelection SortBubble Sort
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
4 1 10 -3 12
4 1 10 -3 12
-3 1 10 4 12
-3 1 10 4 12
-3 1 4 10 12
void selectionSort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
Merge SortQuick SortInsertion SortSelection SortBubble Sort
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
4 1 10 -3 12
4 1 10 -3 12
1 4 10 -3 12
1 4 10 -3 12
-3 1 4 10 12
public static int[] insertionSort(int[] input){
int temp;
for (int i = 1; i < input.length; i++) {
for(int j = i ; j > 0 ; j--){
if(input[j] < input[j-1]){
// Swap with the smallest value
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}
return input;
}
-3 1 4 10 12
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
2 6 7 4 1 -2 8 4 3
2 6 7 4 1 -2 8 4 3
2 7 3 1 -2 8 4 46
2 1 6 7 -2 8 4 43
2 1 -2 4 6 8 7 43
2 1 -2 4 4 8 7 63
private void quickSort(int low, int high) {
int i = low;
int j = high;
// pivot is middle index
int pivot = input[low + (high - low) / 2];
// Divide into two arrays
while (i <= j) {
while (input[i] < pivot) {
i++;
}
while (input[j] > pivot) {
j--;
}
if (i <= j) {
swap(i, j);
// move index to next position on both sides
i++;
j--;
}
}
Merge SortQuick SortInsertion SortSelection SortBubble Sort
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
2 6 7 4 1 -2 8 4 3
2 6 7 4 1 -2 8 4 3
2 7 3 1 -2 8 4 46
2 1 6 7 -2 8 4 43
2 1 -2 4 6 8 7 43
2 1 -2 4 4 8 7 63
swap(i, j);
// move index to next position on both sides
i++;
j--;
}
}
Merge SortQuick SortInsertion SortSelection SortBubble Sort
// calls quickSort() method recursively
if (low < j) {
quickSort(low, j);
}
if (i < high) {
quickSort(i, high);
}
}
private void swap(int i, int j) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Merge SortQuick SortInsertion SortSelection SortBubble Sort
4 1 10 -3 12
4 1 10 -3 12
4 1 10 -3 12
4 1 10 -3 12
1 4 10 -3 12
1 4 10 -3 12
-3 1 4 10 12
// Merges two subarrays of arr[]
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void mergeSort(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
4 1 10 -3 12
4 1 10 -3 12
4 1 10 -3 12
1 4 10 -3 12
1 4 10 -3 12
-3 1 4 10 12
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
4 1 10 -3 12
Merge SortQuick SortInsertion SortSelection SortBubble Sort
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
4 1 10 -3 12
4 1 10 -3 12
4 1 10 -3 12
4 1 10 -3 12
1 4 10 -3 12
1 4 10 -3 12
-3 1 4 10 12
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
k++;
}
Merge SortQuick SortInsertion SortSelection SortBubble Sort
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
4 1 10 -3 12
4 1 10 -3 12
4 1 10 -3 12
4 1 10 -3 12
1 4 10 -3 12
1 4 10 -3 12
-3 1 4 10 12
k++;
}
}
Merge SortQuick SortInsertion SortSelection SortBubble Sort
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
Searching in Arrays
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Sorting in Array
Java offers various sorting algorithms that helps in putting elements of a list in a certain order
Types of Searching Algorithms
Binary SearchLinear Search
Binary SearchLinear Search
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
4
1
10
-3
12
static int search(int arr[], int n, int x)
{
for (int i = 0; i < n; i++)
{
// Return the index of the element if the
element
// is found
if (arr[i] == x)
return i;
}
// return -1 if the element is not found
return -1;
}
Binary SearchLinear Search
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
2 6 7 9 15 26 50 79 83
2 6 7 9 15 26 50 79 83
15 26 50 79 83
int binarySearch(int arr[], int l, int r, int x)
{
if (r>=l)
{
int mid = l + (r - l)/2;
// If the element is present at the
// middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid+1, r, x);
}
Arrays in Java | Edureka

More Related Content

What's hot

Java Methods
Java MethodsJava Methods
Java Methods
OXUS 20
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

What's hot (20)

Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Java platform
Java platformJava platform
Java platform
 
Packages in java
Packages in javaPackages in java
Packages in java
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searching
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Similar to Arrays in Java | Edureka

Lecture no 9.ppt operating system semester four
Lecture  no 9.ppt operating system semester fourLecture  no 9.ppt operating system semester four
Lecture no 9.ppt operating system semester four
VaibhavBhagwat18
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
MaruMengesha
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
Gouda Mando
 

Similar to Arrays in Java | Edureka (20)

Lecture no 9.ppt operating system semester four
Lecture  no 9.ppt operating system semester fourLecture  no 9.ppt operating system semester four
Lecture no 9.ppt operating system semester four
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaJava Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Selection sort
Selection sortSelection sort
Selection sort
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Array
ArrayArray
Array
 
DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Array
ArrayArray
Array
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S Guide
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
 
06slide
06slide06slide
06slide
 
Grid gain paper
Grid gain paperGrid gain paper
Grid gain paper
 

More from Edureka!

More from Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Arrays in Java | Edureka

  • 1.
  • 2. Array in Java Types of Arrays Topics For Today’s Discussion JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Topics for Today’s Session DeclaringAccessingUpdatingCreatingWorking with Array Sorting in Arrays Searching in Arrays
  • 4. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Java Array Array is a static Data Structure Contains collections of homogenous elements All the elements are stored under one variable name It occupies a contiguous memory location
  • 6. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Types Of Arrays 01Single Dimensional Single Dimensional or 1-D array is a type of linear array in which elements are stored in a continuous row 02Two Dimensional Two Dimensional or 2-D array is a type of matrix in which elements are stored in rows and columns 03Multi Dimensional Multi Dimensional array is a type of nested array
  • 8. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Working With Arrays – 1D arrayRefVar = new dataType[arraySize]; =myArray new int[5] Declaring and Initializing an One Dimensional Array datatype[] arrayRefVar = new dataType[arraySize]; =int[5] myArray new int[5]
  • 9. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Working With Arrays – 1D myArray myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] =myArray[0] 10 Declaring and Initializing an One Dimensional Array
  • 10. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Working With Arrays – 1D myArray myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] =myArray[2] 10 30 =myArray[3] 40 =myArray[4] 50 =myArray[1] 20 Declaring and Initializing an One Dimensional Array
  • 11. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training myArray myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] 10 30 40 5020 Working With Arrays – 1D Declaring and Initializing an One Dimensional Array
  • 12. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 10 30 40 5020 Working With Arrays – 1D dataType[] arrayRefVar = new dataType{e1,e2,e3,…,eN}; myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] =int[] myArray new int{10,20,30,40,50} Declaring and Initializing an One Dimensional Array
  • 13. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] 10 30 40 5020 Working With Arrays – 1D int[] myArray dataType[] arrayRefVar = new dataType{e1,e2,e3,…,eN}; Declaring and Initializing an One Dimensional Array
  • 14. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Working With Arrays – 1D Accessing a specific array element myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] 10 30 40 50 20 arrayRefVar[index] myArray[4] myArray[1]
  • 15. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 100 Working With Arrays – 1D Accessing a specific array element myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] 10 30 40 50 20 arrayRefVar[index] myArray[4] myArray[1] Updating a specific array element arrayRefVar[index] = newValue myArray[4] = 100
  • 16. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training myArray[4] = 100 Working With Arrays – 1D Accessing a specific array element myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] 10 30 50 20 arrayRefVar[index] myArray[4] myArray[1] Updating a specific array element arrayRefVar[index] = newValue 100
  • 17. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Working With Arrays – 2D Declaring and Initializing an Two Dimensional Array datatype[][] arrayRefVar = new dataType[row][col]; =int[][] myArray new int[2][2] myArray[0][0] myArray[0][1] myArray[1][0] myArray[1][1] =myArray[0][0] 100
  • 18. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training myArray[0][0] myArray[0][1] myArray[1][0] myArray[1][1] Working With Arrays – 2D Declaring and Initializing an Two Dimensional Array datatype[][] arrayRefVar = new dataType[row][col]; =int[][] myArray new int[2][2] =myArray[0][1] 200 100 =myArray[1][0] 300 =myArray[1][1] 400
  • 19. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training myArray[0][0] myArray[0][1] myArray[1][0] myArray[1][1] Working With Arrays – 2D Declaring and Initializing an Two Dimensional Array datatype[][] arrayRefVar = new dataType[row][col]; =int[][] myArray new int[2][2] 100 200 300 400
  • 20. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 564 myArray[0][0] myArray[0][1] myArray[1][0] myArray[1][1] Working With Arrays – 2D arrayRefVar[row][col] 100 200 300 400 Accessing a specific array element myArray[0][1] Updating a specific array element arrayRefVar[row][col] = newValue myArray[0][1] = 564
  • 21. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training myArray[0][1] = 564 myArray[0][0] myArray[0][1] myArray[1][0] myArray[1][1] Working With Arrays – 2D arrayRefVar[row][col] 100 300 400 Accessing a specific array element myArray[0][1] Updating a specific array element arrayRefVar[row][col] = newValue 564
  • 22. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Operations With Arrays 1 2 3 6 987 4 5 1 2 1 3 271 5 4 Addition of Matrices a[3][3] b[3][3]
  • 23. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Operations With Arrays 1 2 3 6 987 4 5 1 2 1 3 271 5 4 2 4 4 9 11158 9 9 a[3][3] b[3][3] c[3][3] int[][] c = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { c[i][j] = a[i][j] + b[i][j]; } }
  • 24. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 1 2 3 6 987 4 5 1 2 1 3 271 5 4 Division a[3][3] b[3][3] Operations With Arrays Multiplication Subtraction
  • 26. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Sorting in Array Java offers various sorting algorithms that helps in putting elements of a list in a certain order Bubble Sort Insertion Sort Merge Sort Quick SortSelection Sort Types of Sorting Algorithms
  • 27. Merge SortQuick SortInsertion SortSelection SortBubble Sort JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 4 1 10 -3 12 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 1 4 -3 10 12 1 4 -3 10 12 1 -3 4 10 12 1 -3 4 10 12 1 -3 4 10 12 -3 1 4 10 12 -3 1 4 10 12 void bubbleSort(int arr[]) { int n = arr.length; for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) { // swap temp and arr[i] int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } }
  • 28. Merge SortQuick SortInsertion SortSelection SortBubble Sort JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 4 1 10 -3 12 4 1 10 -3 12 -3 1 10 4 12 -3 1 10 4 12 -3 1 4 10 12 void selectionSort(int arr[]) { int n = arr.length; // One by one move boundary of unsorted subarray for (int i = 0; i < n-1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } }
  • 29. Merge SortQuick SortInsertion SortSelection SortBubble Sort JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 4 1 10 -3 12 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 -3 1 4 10 12 public static int[] insertionSort(int[] input){ int temp; for (int i = 1; i < input.length; i++) { for(int j = i ; j > 0 ; j--){ if(input[j] < input[j-1]){ // Swap with the smallest value temp = input[j]; input[j] = input[j-1]; input[j-1] = temp; } } } return input; } -3 1 4 10 12
  • 30. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 2 6 7 4 1 -2 8 4 3 2 6 7 4 1 -2 8 4 3 2 7 3 1 -2 8 4 46 2 1 6 7 -2 8 4 43 2 1 -2 4 6 8 7 43 2 1 -2 4 4 8 7 63 private void quickSort(int low, int high) { int i = low; int j = high; // pivot is middle index int pivot = input[low + (high - low) / 2]; // Divide into two arrays while (i <= j) { while (input[i] < pivot) { i++; } while (input[j] > pivot) { j--; } if (i <= j) { swap(i, j); // move index to next position on both sides i++; j--; } } Merge SortQuick SortInsertion SortSelection SortBubble Sort
  • 31. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 2 6 7 4 1 -2 8 4 3 2 6 7 4 1 -2 8 4 3 2 7 3 1 -2 8 4 46 2 1 6 7 -2 8 4 43 2 1 -2 4 6 8 7 43 2 1 -2 4 4 8 7 63 swap(i, j); // move index to next position on both sides i++; j--; } } Merge SortQuick SortInsertion SortSelection SortBubble Sort // calls quickSort() method recursively if (low < j) { quickSort(low, j); } if (i < high) { quickSort(i, high); } } private void swap(int i, int j) { int temp = input[i]; input[i] = input[j]; input[j] = temp; } }
  • 32. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Merge SortQuick SortInsertion SortSelection SortBubble Sort 4 1 10 -3 12 4 1 10 -3 12 4 1 10 -3 12 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 -3 1 4 10 12 // Merges two subarrays of arr[] // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void mergeSort(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j];
  • 33. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 4 1 10 -3 12 4 1 10 -3 12 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 -3 1 4 10 12 /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; 4 1 10 -3 12 Merge SortQuick SortInsertion SortSelection SortBubble Sort
  • 34. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 4 1 10 -3 12 4 1 10 -3 12 4 1 10 -3 12 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 -3 1 4 10 12 /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } k++; } Merge SortQuick SortInsertion SortSelection SortBubble Sort
  • 35. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 4 1 10 -3 12 4 1 10 -3 12 4 1 10 -3 12 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 -3 1 4 10 12 k++; } } Merge SortQuick SortInsertion SortSelection SortBubble Sort // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } }
  • 37. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Sorting in Array Java offers various sorting algorithms that helps in putting elements of a list in a certain order Types of Searching Algorithms Binary SearchLinear Search
  • 38. Binary SearchLinear Search JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 4 1 10 -3 12 static int search(int arr[], int n, int x) { for (int i = 0; i < n; i++) { // Return the index of the element if the element // is found if (arr[i] == x) return i; } // return -1 if the element is not found return -1; }
  • 39. Binary SearchLinear Search JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 2 6 7 9 15 26 50 79 83 2 6 7 9 15 26 50 79 83 15 26 50 79 83 int binarySearch(int arr[], int l, int r, int x) { if (r>=l) { int mid = l + (r - l)/2; // If the element is present at the // middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid+1, r, x); }