SlideShare a Scribd company logo
Introduction To Practical Computer Science I
Lecture 12: Arrays
Herat University
Computer Science Faculty
Lecturer: Abdul Khaleq Herawi
Date: 2024
2
Outline:
 Creating and initializing arrays
 IndexedVariables
 Array Initializers
 Array Initialization using shorthand notation
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 3
OPENING PROBLEM
Read one hundred numbers, compute their average, and find out how many numbers are
above the average.
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 4
Introducing Arrays
Array is a data structure that represents a collection of the same types of data.
5.6
4.5
3.3
13.2
4
34.33
34
45.45
99.993
11123
double[] myList = new double[10];
myList reference
myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
Element value
Array reference
variable
Array element at
index 5
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 5
Declaring ArrayVariables
 datatype[] arrayRefvar;
 example:
double[] mylist;
 datatype arrayRefvar[]; / / this style is allowed, but not preferred
 example:
double mylist[];
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 6
Creating Arrays
arrayRefvar = new datatype[arraysize];
example:
mylist = new double[10];
mylist[0] references the first element in the array.
mylist[9] references the last element in the array.
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 7
DECLARING AND CREATING IN ONE STEP
datatype[] arrayrefvar = new datatype[arraysize];
double[] mylist = new double[10];
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 8
The Length of an Array
Once an array is created, its size is fixed. It cannot be changed. You can find its size using
 arrayRefvar.length
For example,
 Mylist.length returns 10
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 9
DEFAULTVALUES
When an array is created, its elements are assigned the default value of
 0 for the numeric primitive data types,
 'u0000' for char types, and
 false for boolean types.
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 10
IndexedVariables
 The array elements are accessed through the index. The array indices are 0-based, i.e., It
starts from 0 to arrayrefvar.length-1.
 Each element in the array is represented using the following syntax, known as an indexed variable:
 arrayRefvar[index];
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 11
Using IndexedVariables
After an array is created, an indexed variable can be used in the same way as a regular
variable. For example, the following code adds the value in mylist[0] and mylist[1] to
mylist[2].
 mylist[2] = mylist[0] + mylist[1];
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 12
Array Initializers
 Declaring, creating, initializing in one step:
double[] mylist = {1.9, 2.9, 3.4, 3.5};
This shorthand syntax must be in one statement.
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 13
Declaring, creating, and initializing using the shorthand notation
double[] mylist = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the following statements:
double[] mylist = new double[4];
 mylist[0] = 1.9;
 mylist[1] = 2.9;
 mylist[2] = 3.4;
 mylist[3] = 3.5;
INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 14
Caution!
Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would
cause a syntax error. For example, the following is wrong:
double[] mylist;
mylist = {1.9, 2.9, 3.4, 3.5};
15
Any Question???

More Related Content

PPT
JavaYDL6
PPTX
Data Structures and Algorithoms 07slide - 1D Arrays.pptx
PPT
07slide.ppt
PPTX
Arrays in programming
PPT
Java™ (OOP) - Chapter 6: "Arrays"
PPTX
Chapter6 (4) (1).pptx plog fix down more
PPT
DOCX
Java R20 - UNIT-3.docx
JavaYDL6
Data Structures and Algorithoms 07slide - 1D Arrays.pptx
07slide.ppt
Arrays in programming
Java™ (OOP) - Chapter 6: "Arrays"
Chapter6 (4) (1).pptx plog fix down more
Java R20 - UNIT-3.docx

Similar to Introduction To Practical Computer Science I - Lec 12 (Arrays).pptx (20)

PPTX
Chapter 7.1
PPTX
07+08slide.pptx
PDF
An Introduction to Programming in Java: Arrays
PDF
PPTX
6_Array.pptx
PPT
Array
PPTX
PDF
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
PPT
9781111530532 ppt ch09
PPTX
ch 7 single dimension array in oop .pptx
PDF
Lecture 6 - Arrays
PDF
Java - Arrays Concepts
PPT
array Details
PPT
PPT
ch06.ppt
PPT
ch06.ppt
PPTX
Arrays and Strings engineering education
PPT
Java căn bản - Chapter10
PPTX
Chap1 array
PPT
Data Structure Midterm Lesson Arrays
Chapter 7.1
07+08slide.pptx
An Introduction to Programming in Java: Arrays
6_Array.pptx
Array
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
9781111530532 ppt ch09
ch 7 single dimension array in oop .pptx
Lecture 6 - Arrays
Java - Arrays Concepts
array Details
ch06.ppt
ch06.ppt
Arrays and Strings engineering education
Java căn bản - Chapter10
Chap1 array
Data Structure Midterm Lesson Arrays
Ad

Recently uploaded (20)

PDF
Indian roads congress 037 - 2012 Flexible pavement
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
IGGE1 Understanding the Self1234567891011
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Introduction to Building Materials
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
Cell Types and Its function , kingdom of life
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
Indian roads congress 037 - 2012 Flexible pavement
UNIT III MENTAL HEALTH NURSING ASSESSMENT
IGGE1 Understanding the Self1234567891011
LDMMIA Reiki Yoga Finals Review Spring Summer
Chinmaya Tiranga quiz Grand Finale.pdf
Introduction to Building Materials
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
202450812 BayCHI UCSC-SV 20250812 v17.pptx
History, Philosophy and sociology of education (1).pptx
What if we spent less time fighting change, and more time building what’s rig...
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Classroom Observation Tools for Teachers
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Cell Types and Its function , kingdom of life
Practical Manual AGRO-233 Principles and Practices of Natural Farming
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
A systematic review of self-coping strategies used by university students to ...
Unit 4 Skeletal System.ppt.pptxopresentatiom
Ad

Introduction To Practical Computer Science I - Lec 12 (Arrays).pptx

  • 1. Introduction To Practical Computer Science I Lecture 12: Arrays Herat University Computer Science Faculty Lecturer: Abdul Khaleq Herawi Date: 2024
  • 2. 2 Outline:  Creating and initializing arrays  IndexedVariables  Array Initializers  Array Initialization using shorthand notation
  • 3. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 3 OPENING PROBLEM Read one hundred numbers, compute their average, and find out how many numbers are above the average.
  • 4. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 4 Introducing Arrays Array is a data structure that represents a collection of the same types of data. 5.6 4.5 3.3 13.2 4 34.33 34 45.45 99.993 11123 double[] myList = new double[10]; myList reference myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] Element value Array reference variable Array element at index 5
  • 5. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 5 Declaring ArrayVariables  datatype[] arrayRefvar;  example: double[] mylist;  datatype arrayRefvar[]; / / this style is allowed, but not preferred  example: double mylist[];
  • 6. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 6 Creating Arrays arrayRefvar = new datatype[arraysize]; example: mylist = new double[10]; mylist[0] references the first element in the array. mylist[9] references the last element in the array.
  • 7. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 7 DECLARING AND CREATING IN ONE STEP datatype[] arrayrefvar = new datatype[arraysize]; double[] mylist = new double[10];
  • 8. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 8 The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using  arrayRefvar.length For example,  Mylist.length returns 10
  • 9. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 9 DEFAULTVALUES When an array is created, its elements are assigned the default value of  0 for the numeric primitive data types,  'u0000' for char types, and  false for boolean types.
  • 10. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 10 IndexedVariables  The array elements are accessed through the index. The array indices are 0-based, i.e., It starts from 0 to arrayrefvar.length-1.  Each element in the array is represented using the following syntax, known as an indexed variable:  arrayRefvar[index];
  • 11. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 11 Using IndexedVariables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in mylist[0] and mylist[1] to mylist[2].  mylist[2] = mylist[0] + mylist[1];
  • 12. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 12 Array Initializers  Declaring, creating, initializing in one step: double[] mylist = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.
  • 13. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 13 Declaring, creating, and initializing using the shorthand notation double[] mylist = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] mylist = new double[4];  mylist[0] = 1.9;  mylist[1] = 2.9;  mylist[2] = 3.4;  mylist[3] = 3.5;
  • 14. INTRODUCTIONTO JAVA PROGRAMMING (10TH ED.)Y. DANIEL LIANG 14 Caution! Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] mylist; mylist = {1.9, 2.9, 3.4, 3.5};