SlideShare a Scribd company logo
4
Most read
5
Most read
17
Most read
Computer Institute In Sulaimanyah
Java Object Oriented Programming
Stage : 5th
Lecturer : Naz N. Abdallah
naz.institute@gmail.com
2018 – 2019
1
Chapter one
Arrays in Java
2
Objectives :
 Arrays in Java.
 Advantages and Disadvantages of array.
 Types of array (1D and 2D) :
 Declaring an Array.
 Creating an Array.
 Processing Arrays(Show,sort,max,min) .
 The foreach Loops.
 Jump in java(break,continue,return).
 Stars output. 3
Array
*Array is used to store a collection of variables of the same type. By declaring
an array, memory space is allocated for values of a particular type.
*Array in java is index-based, the first element of the array is stored at the 0
index. An index must be a non-negative integer.
4
Array Advantages :
Code Optimization: It makes the code optimized, we can retrieve or sort the
data efficiently.
Random access: We can get any data located at an index position easily.
Array Disadvantages :
Size Limit: We can store only the fixed size of elements in the array. It doesn't
grow its size at runtime.
5
Types of Array in java :
There are two types of array:
* Single Dimensional Array.
* Multidimensional Array.
6
1.One-Dimensional Array in Java (1-D) :
One-dimensional array(1-D): It is a single array that holds multiple values of the
same type.
Declaring 1D Array :
To use an array in a program, you must declare a variable to reference the array,
and you must specify the type of array.
Syntax to declare an array in java will be like one of them :
dataType[ ] arr ; // preferred way.
dataType [ ]arr ;
dataType arr[ ] ;
7
Creating Arrays:
You can create an array by using the new operator with the following syntax:
dataType[ ] arr = new dataType[arraySize];
Or alternatively you can create arrays as :
dataType[ ] arr = {value0, value1, ..., valuek};
8
Processing Arrays :
Arrays and Loops: When processing array elements, we often use either for
loop or foreach loop because all of the elements in an array are of the same type
and the size of the array is known.
Example:
Here is a complete example of showing how to create, initialize and showing the
array elements using for:
9
10
Example: Showing ,sum ,max number of the array elements using for loop:
The foreach Loops:
A new for loop known as foreach loop, enables you to traverse the complete
array sequentially without using an index variable.
Example:
The following code displays all the elements in the array myList using foreach:
11
12
Jump: Java supports three jump statement: break, continue and return.
These three statements transfer control to other part of the program.
Example for break and continue statement in an array :
13
Example for string data type and sorting the array:
14
Example for char data type in an array:
15
Example for arrays using scanner input:
16
Sorting (1d) array Ascending and Descending :
Multidimensional Array in Java (2D):
In such case, data is stored in row and column based index (matrix form).
Syntax to Declare 2D Array in Java :
dataType[ ][ ] arr ; (or)
dataType [ ][ ]arr ; (or)
dataType arr[ ][ ]; (or)
dataType [ ]arr[ ];
Example to instantiate Multidimensional Array in Java:
int[ ][ ] arr=new int[3][3]; //3 row and 3 column
Example to initialize Multidimensional Array in Java :
arr[0][0]=1; arr[0][1]=2; arr[0][2]=3;
arr[1][0]=4; arr[1][1]=5; arr[1][2]=6;
arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; 17
18
Example of Multidimensional (2D) Java Array :
19
Example of (2D) array using scanner and find max number :
20
Stars examples :
21
H.W /Write the program for this output ?
Any Questions?
What is OOP ?
22

More Related Content

PPTX
Arrays in Java
Abhilash Nair
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Arrays in java
Arzath Areeff
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Methods in java
chauhankapil
 
PPTX
Inheritance in java
RahulAnanda1
 
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
PDF
Class and Objects in Java
Spotle.ai
 
Arrays in Java
Abhilash Nair
 
Constructor in java
Pavith Gunasekara
 
Arrays in java
Arzath Areeff
 
Java threads
Prabhakaran V M
 
Methods in java
chauhankapil
 
Inheritance in java
RahulAnanda1
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Class and Objects in Java
Spotle.ai
 

What's hot (20)

PPTX
Packages in java
Elizabeth alexander
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PPTX
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPTX
OOPS In JAVA.pptx
Sachin33417
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Static keyword ppt
Vinod Kumar
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Polymorphism presentation in java
Ahsan Raja
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
Data types in java
HarshitaAshwani
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
Java Methods
OXUS 20
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPTX
java interface and packages
VINOTH R
 
Packages in java
Elizabeth alexander
 
Strings in Java
Abhilash Nair
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
OOPS In JAVA.pptx
Sachin33417
 
Inheritance in java
Tech_MX
 
Static keyword ppt
Vinod Kumar
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Polymorphism presentation in java
Ahsan Raja
 
9. Input Output in java
Nilesh Dalvi
 
Data types in java
HarshitaAshwani
 
Java exception handling
BHUVIJAYAVELU
 
Abstract class in java
Lovely Professional University
 
Interface in java
PhD Research Scholar
 
Java Methods
OXUS 20
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Methods in Java
Jussi Pohjolainen
 
java interface and packages
VINOTH R
 
Ad

Similar to Arrays in Java (20)

PDF
Arrays in java
TharuniDiddekunta
 
PPT
Ppt chapter09
Richard Styner
 
PPTX
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
PPTX
Arrays in java.pptx
Nagaraju Pamarthi
 
PPTX
Java best practices
Անուշիկ Միրզոյան
 
DOCX
arrays.docx
lakshmanarao027MVGRC
 
PPTX
Array.pptx
ssuser8698eb
 
DOCX
Java collections notes
Surendar Meesala
 
PPTX
OCA Java SE 8 Exam Chapter 3 Core Java APIs
İbrahim Kürce
 
PPTX
ARRAYS.pptx
akila m
 
PDF
Java arrays (1)
Liza Abello
 
PPTX
OOPs with java
AAKANKSHA JAIN
 
PPT
DEMO.ppt
ChristianCabadongga
 
PPTX
Java_Interview Qns
ManikandanRamanujam
 
PPTX
ppt on arrays in c programming language.pptx
AmanRai352102
 
PPTX
PCSTt11 overview of java
Archana Gopinath
 
PPTX
Arrays in programming
TaseerRao
 
PPTX
arraylist in java a comparison of the array and arraylist
PriyadharshiniG41
 
PPTX
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 
Arrays in java
TharuniDiddekunta
 
Ppt chapter09
Richard Styner
 
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
Arrays in java.pptx
Nagaraju Pamarthi
 
Java best practices
Անուշիկ Միրզոյան
 
Array.pptx
ssuser8698eb
 
Java collections notes
Surendar Meesala
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
İbrahim Kürce
 
ARRAYS.pptx
akila m
 
Java arrays (1)
Liza Abello
 
OOPs with java
AAKANKSHA JAIN
 
Java_Interview Qns
ManikandanRamanujam
 
ppt on arrays in c programming language.pptx
AmanRai352102
 
PCSTt11 overview of java
Archana Gopinath
 
Arrays in programming
TaseerRao
 
arraylist in java a comparison of the array and arraylist
PriyadharshiniG41
 
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
VTUOOPMCA5THMODULECollection OverV .pptx
VeenaNaik23
 
Ad

Recently uploaded (20)

PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PPTX
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
RanuFajar1
 
PPT
FALLSEM2025-26_ISWE304L_TH_VL2025260102786_2025-07-10_Reference-Material-II.ppt
AKSHAYA255427
 
DOCX
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPTX
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPT
Overview of Oracle Receivables Process.ppt
nbvreddy229
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
Rise With SAP partner in Mumbai.........
pts464036
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
introduction to dart --- Section one .pptx
marknaiem92
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PDF
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
Materi-Enum-and-Record-Data-Type (1).pptx
RanuFajar1
 
FALLSEM2025-26_ISWE304L_TH_VL2025260102786_2025-07-10_Reference-Material-II.ppt
AKSHAYA255427
 
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Overview of Oracle Receivables Process.ppt
nbvreddy229
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Rise With SAP partner in Mumbai.........
pts464036
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
introduction to dart --- Section one .pptx
marknaiem92
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 

Arrays in Java

  • 1. Computer Institute In Sulaimanyah Java Object Oriented Programming Stage : 5th Lecturer : Naz N. Abdallah [email protected] 2018 – 2019 1
  • 3. Objectives :  Arrays in Java.  Advantages and Disadvantages of array.  Types of array (1D and 2D) :  Declaring an Array.  Creating an Array.  Processing Arrays(Show,sort,max,min) .  The foreach Loops.  Jump in java(break,continue,return).  Stars output. 3
  • 4. Array *Array is used to store a collection of variables of the same type. By declaring an array, memory space is allocated for values of a particular type. *Array in java is index-based, the first element of the array is stored at the 0 index. An index must be a non-negative integer. 4
  • 5. Array Advantages : Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently. Random access: We can get any data located at an index position easily. Array Disadvantages : Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. 5
  • 6. Types of Array in java : There are two types of array: * Single Dimensional Array. * Multidimensional Array. 6
  • 7. 1.One-Dimensional Array in Java (1-D) : One-dimensional array(1-D): It is a single array that holds multiple values of the same type. Declaring 1D Array : To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array. Syntax to declare an array in java will be like one of them : dataType[ ] arr ; // preferred way. dataType [ ]arr ; dataType arr[ ] ; 7
  • 8. Creating Arrays: You can create an array by using the new operator with the following syntax: dataType[ ] arr = new dataType[arraySize]; Or alternatively you can create arrays as : dataType[ ] arr = {value0, value1, ..., valuek}; 8
  • 9. Processing Arrays : Arrays and Loops: When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known. Example: Here is a complete example of showing how to create, initialize and showing the array elements using for: 9
  • 10. 10 Example: Showing ,sum ,max number of the array elements using for loop:
  • 11. The foreach Loops: A new for loop known as foreach loop, enables you to traverse the complete array sequentially without using an index variable. Example: The following code displays all the elements in the array myList using foreach: 11
  • 12. 12 Jump: Java supports three jump statement: break, continue and return. These three statements transfer control to other part of the program. Example for break and continue statement in an array :
  • 13. 13 Example for string data type and sorting the array:
  • 14. 14 Example for char data type in an array:
  • 15. 15 Example for arrays using scanner input:
  • 16. 16 Sorting (1d) array Ascending and Descending :
  • 17. Multidimensional Array in Java (2D): In such case, data is stored in row and column based index (matrix form). Syntax to Declare 2D Array in Java : dataType[ ][ ] arr ; (or) dataType [ ][ ]arr ; (or) dataType arr[ ][ ]; (or) dataType [ ]arr[ ]; Example to instantiate Multidimensional Array in Java: int[ ][ ] arr=new int[3][3]; //3 row and 3 column Example to initialize Multidimensional Array in Java : arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; 17
  • 18. 18 Example of Multidimensional (2D) Java Array :
  • 19. 19 Example of (2D) array using scanner and find max number :
  • 21. 21 H.W /Write the program for this output ?