SlideShare a Scribd company logo
1 of 16
Introduction to Computer Programming
               Lab 2




          Arrays

            Neveen Reda
Arrays in action
0 There are many uses for arrays
0 For example image pixels are represented using
 arrays




                      Neuroph (2012)
Arrays in action

                0 Arrays can also be used
                 in creating grids for
                 games.




Eckert (2009)
Lesson Objectives
After this lesson you will be able to:

3.Develop Java programs that create arrays and perform
different operations on them.

5.Determine the functionality and output of any given
program that performs array manipulations.
Arrays
0 Why arrays?
    Individual variables can only hold one value at a time, Arrays
     can holds multiple values (Abdennadher , 2010).
0 “An array is a data structure that defines an indexed
  collection of a fixed number of data elements.” (Mughal &
  Rasmussen, 2009).
Arrays
0 The position of an element in an array is called the index.
0 If array X contains 10 elements, the first element will be at
 position (index) '0' and the last element is at position '9'




                            Oracle (2012)
Arrays Declarations
0 All the elements in an array have the same data type.
(For example all integers, all Strings, all doubles and so on)

Examples:


int [] x;        //x is an array of integers.
double [] myArray;    //myArray is an array of doubles.
Creating Arrays
0 The size if an array is fixed. It is specified when the array is
  created and cannot be changed (Abdennadher , 2010).




x = new int[5];        //array x is now of size 5
myArray = new double[3];
Creating Arrays
0 An array can be declared and created in one statement.
0 Until this point the arrays is empty (contains default values
  of its declared type)

Examples:

int [] x = new int [5];
double [] myArray = new double [3];
int number = 10;
char [] letters = new char[number];
Initializing Arrays
Double [] prices = new double [5];
Initializing Arrays
Double [] prices = {2.25, 3.0, 10.5, 5.3, 0.5};




                                                  2.25
                                                  3.0

                                                  10.5

                                                  5.3

                                                  0.5
Using Arrays
After creating and initializing an array, it can be used
    within a program:


•     An element in the array can be used within a
     statement
•    The reference to the array can be used within a
     statement. (Abdennadher , 2010)
Using Arrays
0 Reassign value to index:
                                           4.5
                                           2.25
                        Prices[0] = 4.5;
                                           3.0
                                           10.5

                                           5.3

                                           0.5
0 Using element within a statement:

if(Prices[1] >= 3.0){
Discount = 10;
}
Array Examples

0 Write a java program that creates an array of size 3 and
 prints all its elements using a for loop.
References
•   Abdennadher S. (2010). Arrays. Lecture notes.

•   Eckert T. (2009). Bimaru - Battleship Solitaire [image]. Retrieved from:
    http://www.cyrket.com/p/android/com.androidcan.bimaru/

•   Mughal K. A. & Rasmussen R. W. (2009). A Programmer's Guide to Java SCJP
    Certification. Upper Saddle River, NJ: Pearson Education.

•   Neuroph (2012). Image colors [image]. Retrieved from: http://
    neuroph.sourceforge.net/image_recognition.html

•   Oracle (2012). An array of 10 elements [image]. Retrieved from: http
    ://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

More Related Content

What's hot

A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)Imdadul Himu
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysMartin Chapman
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Array in c language
Array in c languageArray in c language
Array in c languagehome
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java languageHareem Naz
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN Cnaveed jamali
 
Java Arrays
Java ArraysJava Arrays
Java ArraysOXUS 20
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2Rajendran
 

What's hot (20)

Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array in C
Array in CArray in C
Array in C
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Array in c#
Array in c#Array in c#
Array in c#
 
Chap09
Chap09Chap09
Chap09
 
Array in C
Array in CArray in C
Array in C
 

Similar to Introduction to Computer Programming Lab 2 Arrays

Similar to Introduction to Computer Programming Lab 2 Arrays (20)

JavaYDL6
JavaYDL6JavaYDL6
JavaYDL6
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
06slide
06slide06slide
06slide
 
Arrays
ArraysArrays
Arrays
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Arrays in java.pptx
Arrays in java.pptxArrays in java.pptx
Arrays in java.pptx
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
Array
ArrayArray
Array
 
Create a Structure in VBNet.pptx
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
9781111530532 ppt ch09
9781111530532 ppt ch099781111530532 ppt ch09
9781111530532 ppt ch09
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Object Relation Mapping in Odoo 16
Object Relation Mapping in Odoo 16Object Relation Mapping in Odoo 16
Object Relation Mapping in Odoo 16
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structures
 

Recently uploaded

Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 

Recently uploaded (20)

prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 

Introduction to Computer Programming Lab 2 Arrays

  • 1. Introduction to Computer Programming Lab 2 Arrays Neveen Reda
  • 2. Arrays in action 0 There are many uses for arrays 0 For example image pixels are represented using arrays Neuroph (2012)
  • 3. Arrays in action 0 Arrays can also be used in creating grids for games. Eckert (2009)
  • 4. Lesson Objectives After this lesson you will be able to: 3.Develop Java programs that create arrays and perform different operations on them. 5.Determine the functionality and output of any given program that performs array manipulations.
  • 5. Arrays 0 Why arrays?  Individual variables can only hold one value at a time, Arrays can holds multiple values (Abdennadher , 2010). 0 “An array is a data structure that defines an indexed collection of a fixed number of data elements.” (Mughal & Rasmussen, 2009).
  • 6. Arrays 0 The position of an element in an array is called the index. 0 If array X contains 10 elements, the first element will be at position (index) '0' and the last element is at position '9' Oracle (2012)
  • 7. Arrays Declarations 0 All the elements in an array have the same data type. (For example all integers, all Strings, all doubles and so on) Examples: int [] x; //x is an array of integers. double [] myArray; //myArray is an array of doubles.
  • 8. Creating Arrays 0 The size if an array is fixed. It is specified when the array is created and cannot be changed (Abdennadher , 2010). x = new int[5]; //array x is now of size 5 myArray = new double[3];
  • 9. Creating Arrays 0 An array can be declared and created in one statement. 0 Until this point the arrays is empty (contains default values of its declared type) Examples: int [] x = new int [5]; double [] myArray = new double [3]; int number = 10; char [] letters = new char[number];
  • 10. Initializing Arrays Double [] prices = new double [5];
  • 11. Initializing Arrays Double [] prices = {2.25, 3.0, 10.5, 5.3, 0.5}; 2.25 3.0 10.5 5.3 0.5
  • 12. Using Arrays After creating and initializing an array, it can be used within a program: • An element in the array can be used within a statement • The reference to the array can be used within a statement. (Abdennadher , 2010)
  • 13. Using Arrays 0 Reassign value to index: 4.5 2.25 Prices[0] = 4.5; 3.0 10.5 5.3 0.5 0 Using element within a statement: if(Prices[1] >= 3.0){ Discount = 10; }
  • 14. Array Examples 0 Write a java program that creates an array of size 3 and prints all its elements using a for loop.
  • 15.
  • 16. References • Abdennadher S. (2010). Arrays. Lecture notes. • Eckert T. (2009). Bimaru - Battleship Solitaire [image]. Retrieved from: http://www.cyrket.com/p/android/com.androidcan.bimaru/ • Mughal K. A. & Rasmussen R. W. (2009). A Programmer's Guide to Java SCJP Certification. Upper Saddle River, NJ: Pearson Education. • Neuroph (2012). Image colors [image]. Retrieved from: http:// neuroph.sourceforge.net/image_recognition.html • Oracle (2012). An array of 10 elements [image]. Retrieved from: http ://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html