SlideShare a Scribd company logo
1 of 20
CMP-2123-Object Oriented
Programming
Lecture 5
By
Abrar Ahmad
Abrar.ahmad14@ce.ceme.edu.pk
Java - Arrays
• Java provides a data structure, the array, which stores a fixed-size
sequential collection of elements of the same type. An array is used
to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
• Instead of declaring individual variables, such as number0, number1,
..., and number99, you declare one array variable such as numbers
and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables.
Badar Waseer arbabwaseergmail.com 2
Declaring Array Variables
• To use an array in a program, you must declare a variable to reference
the array, and you must specify the type of array the variable can
reference.
Badar Waseer arbabwaseergmail.com 3
Syntax for declaring an array variable
Badar Waseer arbabwaseergmail.com 4
Example
Badar Waseer arbabwaseergmail.com 5
• You can create an array by using the new operator with the following
syntax
arrayRefVar = new dataType[arraySize] ;
• The above statement does two things −
• It creates an array using new dataType[arraySize].
• It assigns the reference of the newly created array to the variable arrayRefVar.
• Declaring an array variable, creating an array, and assigning the
reference of the array to the variable can be combined in one
statement, as shown in next slide
Creating Arrays
Badar Waseer arbabwaseergmail.com 6
Badar Waseer arbabwaseergmail.com 7
• The array elements are accessed through the index. Array indices are
0-based; that is, they start from 0 to arrayRefVar.length-1.
Badar Waseer arbabwaseergmail.com 8
Example
Badar Waseer arbabwaseergmail.com 9
Processing Arrays
• 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.
Badar Waseer arbabwaseergmail.com 10
Badar Waseer arbabwaseergmail.com 11
Badar Waseer arbabwaseergmail.com 12
The foreach Loops
• JDK 1.5 introduced a new for loop known as foreach loop or
enhanced for loop, which enables you to traverse the complete array
sequentially without using an index variable.
Badar Waseer arbabwaseergmail.com 13
Badar Waseer arbabwaseergmail.com 14
Badar Waseer arbabwaseergmail.com 15
Passing Arrays to Methods
• Just as you can pass primitive type values to methods, you can also
pass arrays to methods
Badar Waseer arbabwaseergmail.com 16
Badar Waseer arbabwaseergmail.com 17
• You can invoke it by passing an array. For example, the following
statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2
−
Badar Waseer arbabwaseergmail.com 18
Returning an Array from a Method
• A method may also return an array.
Badar Waseer arbabwaseergmail.com 19
Chapter End
Badar Waseer arbabwaseergmail.com 20

More Related Content

What's hot

Lightning Talk on Metaprogramming in Ruby
Lightning Talk on Metaprogramming in RubyLightning Talk on Metaprogramming in Ruby
Lightning Talk on Metaprogramming in RubyEthan Fremen
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHPRick Ogden
 
Semantic DEX Components
Semantic DEX ComponentsSemantic DEX Components
Semantic DEX ComponentsDavid Price
 

What's hot (10)

Array andfunction
Array andfunctionArray andfunction
Array andfunction
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Java- language Lecture 6
Java- language Lecture 6Java- language Lecture 6
Java- language Lecture 6
 
Chapter 6 java
Chapter 6 javaChapter 6 java
Chapter 6 java
 
Lightning Talk on Metaprogramming in Ruby
Lightning Talk on Metaprogramming in RubyLightning Talk on Metaprogramming in Ruby
Lightning Talk on Metaprogramming in Ruby
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Semantic DEX Components
Semantic DEX ComponentsSemantic DEX Components
Semantic DEX Components
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 

Similar to Lec 1.5 Object Oriented Programming

Java Arrays
Java ArraysJava Arrays
Java ArraysOXUS 20
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute JavaShariq Alee
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptxakila m
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8mlrbrown
 
Javascript - Array - Creating Array
Javascript - Array - Creating ArrayJavascript - Array - Creating Array
Javascript - Array - Creating ArraySamuel Santos
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentationNeveen Reda
 
Introduction to System verilog
Introduction to System verilog Introduction to System verilog
Introduction to System verilog Pushpa Yakkala
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8RhettB
 

Similar to Lec 1.5 Object Oriented Programming (20)

Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
 
Mod 12
Mod 12Mod 12
Mod 12
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
Javascript - Array - Creating Array
Javascript - Array - Creating ArrayJavascript - Array - Creating Array
Javascript - Array - Creating Array
 
Arrays
ArraysArrays
Arrays
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
 
Array
ArrayArray
Array
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Arrays in java.pptx
Arrays in java.pptxArrays in java.pptx
Arrays in java.pptx
 
javaarray
javaarrayjavaarray
javaarray
 
Introduction to System verilog
Introduction to System verilog Introduction to System verilog
Introduction to System verilog
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 

More from Badar Waseer

Ch 03 s.e agile development
Ch 03 s.e agile developmentCh 03 s.e agile development
Ch 03 s.e agile developmentBadar Waseer
 
Ch 02 s.e software process models 1
Ch 02 s.e software process models   1Ch 02 s.e software process models   1
Ch 02 s.e software process models 1Badar Waseer
 
Ch 01 s.e introduction
Ch 01 s.e introductionCh 01 s.e introduction
Ch 01 s.e introductionBadar Waseer
 
Ch 14 s.e use case diagrams
Ch 14 s.e use case diagramsCh 14 s.e use case diagrams
Ch 14 s.e use case diagramsBadar Waseer
 
Lec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented ProgrammingLec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented ProgrammingBadar Waseer
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingBadar Waseer
 
Multimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skillsMultimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skillsBadar Waseer
 
Multimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animationMultimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animationBadar Waseer
 
Multimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 videoMultimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 videoBadar Waseer
 
Multimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 AudioMultimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 AudioBadar Waseer
 
Multimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 MultimediaMultimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 MultimediaBadar Waseer
 
Multimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 deliveringMultimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 deliveringBadar Waseer
 
Agriculture Information System (AIS)
Agriculture Information System (AIS)Agriculture Information System (AIS)
Agriculture Information System (AIS)Badar Waseer
 
Output Devices UoS Fsd
Output Devices UoS FsdOutput Devices UoS Fsd
Output Devices UoS FsdBadar Waseer
 

More from Badar Waseer (15)

Ch 03 s.e agile development
Ch 03 s.e agile developmentCh 03 s.e agile development
Ch 03 s.e agile development
 
Ch 02 s.e software process models 1
Ch 02 s.e software process models   1Ch 02 s.e software process models   1
Ch 02 s.e software process models 1
 
Ch 01 s.e introduction
Ch 01 s.e introductionCh 01 s.e introduction
Ch 01 s.e introduction
 
Ch 14 s.e use case diagrams
Ch 14 s.e use case diagramsCh 14 s.e use case diagrams
Ch 14 s.e use case diagrams
 
Ch 13 s.e cmmi
Ch 13 s.e cmmiCh 13 s.e cmmi
Ch 13 s.e cmmi
 
Lec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented ProgrammingLec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented Programming
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented Programming
 
Multimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skillsMultimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skills
 
Multimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animationMultimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animation
 
Multimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 videoMultimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 video
 
Multimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 AudioMultimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 Audio
 
Multimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 MultimediaMultimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 Multimedia
 
Multimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 deliveringMultimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 delivering
 
Agriculture Information System (AIS)
Agriculture Information System (AIS)Agriculture Information System (AIS)
Agriculture Information System (AIS)
 
Output Devices UoS Fsd
Output Devices UoS FsdOutput Devices UoS Fsd
Output Devices UoS Fsd
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

Lec 1.5 Object Oriented Programming

  • 1. CMP-2123-Object Oriented Programming Lecture 5 By Abrar Ahmad Abrar.ahmad14@ce.ceme.edu.pk
  • 2. Java - Arrays • Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. • Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. Badar Waseer arbabwaseergmail.com 2
  • 3. Declaring Array Variables • To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Badar Waseer arbabwaseergmail.com 3
  • 4. Syntax for declaring an array variable Badar Waseer arbabwaseergmail.com 4
  • 6. • You can create an array by using the new operator with the following syntax arrayRefVar = new dataType[arraySize] ; • The above statement does two things − • It creates an array using new dataType[arraySize]. • It assigns the reference of the newly created array to the variable arrayRefVar. • Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown in next slide Creating Arrays Badar Waseer arbabwaseergmail.com 6
  • 8. • The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arrayRefVar.length-1. Badar Waseer arbabwaseergmail.com 8
  • 10. Processing Arrays • 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. Badar Waseer arbabwaseergmail.com 10
  • 13. The foreach Loops • JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable. Badar Waseer arbabwaseergmail.com 13
  • 16. Passing Arrays to Methods • Just as you can pass primitive type values to methods, you can also pass arrays to methods Badar Waseer arbabwaseergmail.com 16
  • 18. • You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2 − Badar Waseer arbabwaseergmail.com 18
  • 19. Returning an Array from a Method • A method may also return an array. Badar Waseer arbabwaseergmail.com 19
  • 20. Chapter End Badar Waseer arbabwaseergmail.com 20