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
PPTX
Arrays in java
PPSX
Data Types & Variables in JAVA
PPTX
Data types in java
PPTX
Classes objects in java
PPTX
Data Types, Variables, and Operators
PPTX
Methods in java
PPTX
Constructor in java
Arrays in Java
Arrays in java
Data Types & Variables in JAVA
Data types in java
Classes objects in java
Data Types, Variables, and Operators
Methods in java
Constructor in java

What's hot (20)

PPTX
Packages in java
PPTX
Java package
PPTX
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPS
Java Exception handling
PDF
Methods in Java
PPTX
Type casting in java
PPTX
Java string handling
PPTX
Constructor in java
PPTX
Interface in java
PPTX
PDF
Java threads
PPTX
Vectors in Java
PPTX
Method overloading
PPTX
Java exception handling
PPTX
Java Data Types
PPTX
Strings in Java
PPTX
String, string builder, string buffer
PPTX
Operators in java
Packages in java
Java package
Basic Concepts of OOPs (Object Oriented Programming in Java)
Java Exception handling
Methods in Java
Type casting in java
Java string handling
Constructor in java
Interface in java
Java threads
Vectors in Java
Method overloading
Java exception handling
Java Data Types
Strings in Java
String, string builder, string buffer
Operators in java
Ad

Similar to Arrays in Java (20)

PPTX
ARRAYS.pptx
PPTX
OOPs with java
PPTX
Arrays in programming
PDF
Class notes(week 4) on arrays and strings
PPT
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
PPT
Arrays in Java Programming Language slides
PPT
Array.ppt
PPT
array.ppt
DOCX
Java R20 - UNIT-3.docx
PPTX
Array.pptx
PPT
17-Arrays en java presentación documento
PPT
Core java day2
DOCX
Class notes(week 4) on arrays and strings
PPTX
6_Array.pptx
PDF
Java chapter 6 - Arrays -syntax and use
PPTX
Introduction-to-Arrays-in-Java . Exploring array
PPTX
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
PPT
ch11.ppt
PPT
Arrays in java programming language slides
ARRAYS.pptx
OOPs with java
Arrays in programming
Class notes(week 4) on arrays and strings
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
Arrays in Java Programming Language slides
Array.ppt
array.ppt
Java R20 - UNIT-3.docx
Array.pptx
17-Arrays en java presentación documento
Core java day2
Class notes(week 4) on arrays and strings
6_Array.pptx
Java chapter 6 - Arrays -syntax and use
Introduction-to-Arrays-in-Java . Exploring array
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
ch11.ppt
Arrays in java programming language slides
Ad

Recently uploaded (20)

PDF
Exploring AI Agents in Process Industries
PDF
How to Confidently Manage Project Budgets
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
Become an Agentblazer Champion Challenge Kickoff
PPTX
Save Business Costs with CRM Software for Insurance Agents
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
PDF
Community & News Update Q2 Meet Up 2025
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
10 Hidden App Development Costs That Can Sink Your Startup.pptx
PDF
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
PDF
The Future of Smart Factories Why Embedded Analytics Leads the Way
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
PDF
Jenkins: An open-source automation server powering CI/CD Automation
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
PPTX
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Exploring AI Agents in Process Industries
How to Confidently Manage Project Budgets
Materi_Pemrograman_Komputer-Looping.pptx
Become an Agentblazer Champion Challenge Kickoff
Save Business Costs with CRM Software for Insurance Agents
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
Community & News Update Q2 Meet Up 2025
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
10 Hidden App Development Costs That Can Sink Your Startup.pptx
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
The Future of Smart Factories Why Embedded Analytics Leads the Way
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Jenkins: An open-source automation server powering CI/CD Automation
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
How to Migrate SBCGlobal Email to Yahoo Easily
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
The Future of Smart Factories Why Embedded Analytics Leads the Way
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx

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 ?