SlideShare una empresa de Scribd logo
1 de 22
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

La actualidad más candente (20)

Data types in java
Data types in javaData types in java
Data types in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
java token
java tokenjava token
java token
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 
Java awt
Java awtJava awt
Java awt
 

Similar a Arrays in Java

Similar a Arrays in Java (20)

Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
 
Arrays in java.pptx
Arrays in java.pptxArrays in java.pptx
Arrays in java.pptx
 
Java best practices
Java best practicesJava best practices
Java best practices
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
OOPs with java
OOPs with javaOOPs with java
OOPs with java
 
DEMO.ppt
DEMO.pptDEMO.ppt
DEMO.ppt
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
PCSTt11 overview of java
PCSTt11 overview of javaPCSTt11 overview of java
PCSTt11 overview of java
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Arrays
ArraysArrays
Arrays
 
Java for newcomers
Java for newcomersJava for newcomers
Java for newcomers
 
Arrays accessing using for loops
Arrays accessing using for loopsArrays accessing using for loops
Arrays accessing using for loops
 

Último

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Último (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 

Arrays in Java

  • 1. Computer Institute In Sulaimanyah Java Object Oriented Programming Stage : 5th Lecturer : Naz N. Abdallah naz.institute@gmail.com 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 ?