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

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

PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
PPT
ajava arrays topic brief explanation data
PPT
Lecture no 9.ppt operating system semester four
PPTX
Lecture 7 arrays
PDF
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
PPTX
CAP615-Unit1.pptx
PPTX
SessionPlans_f3efa1.6 Array in java.pptx
PPT
object oriented programming java lectures
PPTX
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
PPTX
Arrays in Java
PDF
CP Handout#9
PDF
Selection sort
PPT
Array 31.8.2020 updated
PPT
Fp201 unit4
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
PPTX
Java Programming
PPTX
DSA 103 Object Oriented Programming :: Week 5
PPTX
6_Array.pptx
PDF
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
ajava arrays topic brief explanation data
Lecture no 9.ppt operating system semester four
Lecture 7 arrays
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
CAP615-Unit1.pptx
SessionPlans_f3efa1.6 Array in java.pptx
object oriented programming java lectures
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
Arrays in Java
CP Handout#9
Selection sort
Array 31.8.2020 updated
Fp201 unit4
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-...
Java Programming
DSA 103 Object Oriented Programming :: Week 5
6_Array.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
cuic standard and advanced reporting.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
A Presentation on Artificial Intelligence
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Spectroscopy.pptx food analysis technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Mobile App Security Testing_ A Comprehensive Guide.pdf
Assigned Numbers - 2025 - Bluetooth® Document
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Machine Learning_overview_presentation.pptx
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MYSQL Presentation for SQL database connectivity
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
A Presentation on Artificial Intelligence
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectroscopy.pptx food analysis technology
Chapter 3 Spatial Domain Image Processing.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”

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