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

What's hot (20)

PPTX
Multithreading in java
Raghu nath
 
PDF
Collections In Java
Binoj T E
 
PPSX
Collections - Array List
Hitesh-Java
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PPTX
Event Handling in java
Google
 
PPT
Collection Framework in java
CPD INDIA
 
PPTX
Java - Collections framework
Riccardo Cardin
 
PPTX
The Singleton Pattern Presentation
JAINIK PATEL
 
PPTX
java memory management & gc
exsuns
 
PPT
Java collections concept
kumar gaurav
 
PPTX
JAVA PROGRAMMING
Niyitegekabilly
 
PPTX
Java Beans
Ankit Desai
 
PDF
LinkedList vs ArrayList in Java | Edureka
Edureka!
 
PPTX
Threading in C#
Medhat Dawoud
 
PPTX
Windowforms controls c#
prabhu rajendran
 
PPTX
Design Pattern - Singleton Pattern
Mudasir Qazi
 
PPTX
Java Stack Data Structure.pptx
vishal choudhary
 
PPT
Design Patterns
Anuja Arosha
 
PDF
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Edureka!
 
Multithreading in java
Raghu nath
 
Collections In Java
Binoj T E
 
Collections - Array List
Hitesh-Java
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Java Server Pages(jsp)
Manisha Keim
 
Event Handling in java
Google
 
Collection Framework in java
CPD INDIA
 
Java - Collections framework
Riccardo Cardin
 
The Singleton Pattern Presentation
JAINIK PATEL
 
java memory management & gc
exsuns
 
Java collections concept
kumar gaurav
 
JAVA PROGRAMMING
Niyitegekabilly
 
Java Beans
Ankit Desai
 
LinkedList vs ArrayList in Java | Edureka
Edureka!
 
Threading in C#
Medhat Dawoud
 
Windowforms controls c#
prabhu rajendran
 
Design Pattern - Singleton Pattern
Mudasir Qazi
 
Java Stack Data Structure.pptx
vishal choudhary
 
Design Patterns
Anuja Arosha
 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Edureka!
 

Similar to Arrays in Java | Edureka (20)

DOCX
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
PPTX
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
PPT
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
trangiaphuc362003181
 
PPT
ch11.ppt
kavitamittal18
 
PPT
Arrays in java programming language slides
ssuser5d6130
 
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
PPTX
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
PDF
Aj unit2 notesjavadatastructures
Arthik Daniel
 
PPTX
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
PDF
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
PPTX
sorting and its types
SIVASHANKARIRAJAN
 
PPT
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
KierenReynolds3
 
PPT
Arrays in Java Programming Language slides
ssuser5d6130
 
PPT
17-Arrays en java presentación documento
DiegoGamboaSafla
 
PPTX
Fundamental Algorithms: Sorting, Searching, Greedy Algorithms in Java.pptx
JennyLynMasgong
 
PPT
Arrays in JAVA.ppt
SeethaDinesh
 
PPT
Ap Power Point Chpt6
dplunkett
 
PDF
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
PPTX
Lecture_14Sorting.pptx
jeevankjeevan
 
PPT
Ppt chapter11
Richard Styner
 
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
trangiaphuc362003181
 
ch11.ppt
kavitamittal18
 
Arrays in java programming language slides
ssuser5d6130
 
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
fickolatigo
 
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
Aj unit2 notesjavadatastructures
Arthik Daniel
 
Bubble sort, Selection sort SORTING .pptx
Kalpana Mohan
 
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
sorting and its types
SIVASHANKARIRAJAN
 
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
KierenReynolds3
 
Arrays in Java Programming Language slides
ssuser5d6130
 
17-Arrays en java presentación documento
DiegoGamboaSafla
 
Fundamental Algorithms: Sorting, Searching, Greedy Algorithms in Java.pptx
JennyLynMasgong
 
Arrays in JAVA.ppt
SeethaDinesh
 
Ap Power Point Chpt6
dplunkett
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
Lecture_14Sorting.pptx
jeevankjeevan
 
Ppt chapter11
Richard Styner
 
Ad

More from Edureka! (20)

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

Recently uploaded (20)

PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 

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