SlideShare a Scribd company logo
Arrays in Java | Edureka
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

PDF
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
PPTX
Variable and constants in Vb.NET
PPTX
Control statements in java
PPTX
Operators in java
PDF
Create table
PPT
MySQL Functions
PPTX
java programming- control statements
PPTX
Inheritance in JAVA PPT
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Variable and constants in Vb.NET
Control statements in java
Operators in java
Create table
MySQL Functions
java programming- control statements
Inheritance in JAVA PPT

What's hot (20)

PDF
PPSX
JDBC: java DataBase connectivity
PPTX
Type casting in java
PPT
Codd's rules
PPTX
Java constructors
PPTX
Control Statements in Java
PPT
CPU Scheduling Algorithms
PPTX
Arrays in java
PPTX
Java program structure
PPT
Chapter 13 - Recursion
PPTX
Jdbc ppt
PPTX
SQL commands
PPTX
Basics of JAVA programming
PPT
Algorithmic Notations
PPT
Introduction to JavaScript
PDF
Files and streams
PPT
Swing and AWT in java
PPTX
Free Space Management, Efficiency & Performance, Recovery and NFS
PPT
Generics in java
JDBC: java DataBase connectivity
Type casting in java
Codd's rules
Java constructors
Control Statements in Java
CPU Scheduling Algorithms
Arrays in java
Java program structure
Chapter 13 - Recursion
Jdbc ppt
SQL commands
Basics of JAVA programming
Algorithmic Notations
Introduction to JavaScript
Files and streams
Swing and AWT in java
Free Space Management, Efficiency & Performance, Recovery and NFS
Generics in java
Ad

Similar to Arrays in Java | Edureka (20)

DOCX
Java R20 - UNIT-3.docx
PPTX
Introduction-to-Arrays-in-Java . Exploring array
PPTX
A Comprehensive Comparative Study and Performance Evaluation
PPT
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
PPT
ch11.ppt
PPT
Arrays in java programming language slides
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
PPTX
Computer programming 2 Lesson 13
PDF
Aj unit2 notesjavadatastructures
PPTX
Bubble sort, Selection sort SORTING .pptx
PDF
Sorting_Algoritm-computee-scienceggn.pdf
PPTX
sorting and its types
PPT
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
PPT
Arrays in Java Programming Language slides
PPT
17-Arrays en java presentación documento
PPTX
Fundamental Algorithms: Sorting, Searching, Greedy Algorithms in Java.pptx
PPT
Arrays in JAVA.ppt
PPT
Ap Power Point Chpt6
PDF
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
PPTX
Lecture_14Sorting.pptx
Java R20 - UNIT-3.docx
Introduction-to-Arrays-in-Java . Exploring array
A Comprehensive Comparative Study and Performance Evaluation
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
ch11.ppt
Arrays in java programming language slides
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Computer programming 2 Lesson 13
Aj unit2 notesjavadatastructures
Bubble sort, Selection sort SORTING .pptx
Sorting_Algoritm-computee-scienceggn.pdf
sorting and its types
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
Arrays in Java Programming Language slides
17-Arrays en java presentación documento
Fundamental Algorithms: Sorting, Searching, Greedy Algorithms in Java.pptx
Arrays in JAVA.ppt
Ap Power Point Chpt6
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Lecture_14Sorting.pptx
Ad

More from Edureka! (20)

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

Recently uploaded (20)

PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
sap open course for s4hana steps from ECC to s4
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
A comparative analysis of optical character recognition models for extracting...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
MIND Revenue Release Quarter 2 2025 Press Release
sap open course for s4hana steps from ECC to s4
The AUB Centre for AI in Media Proposal.docx
Unlocking AI with Model Context Protocol (MCP)
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
The Rise and Fall of 3GPP – Time for a Sabbatical?
Programs and apps: productivity, graphics, security and other tools
Assigned Numbers - 2025 - Bluetooth® Document
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks

Arrays in Java | Edureka

  • 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); }