SlideShare a Scribd company logo
From last time…
•

Please turn in Homework 2!

•

We made a beautiful object-oriented night sky!

•

Remember that a class has:!
1. a name!
2. attributes (variables)!
3. a constructor (run via ‘new’)!
4. and methods (actions)

!
!
!
!
Arrays I

CAP
Rules of an Array
•

An array is a ordered “list” object which which
holds multiple “elements” (variables or objects)!

•

It can hold only “elements” of one type!

•

It cannot change size after it has been created!

•

It starts at the 0th position!

•

An array, like a variable, is declared & initialized
Memory
int x;
How does the compiler know!
how much space to reserve?
Memory
Type

Bits

Signed?

Min Value

Max Value

byte

8

Yes

-128

127

short

16

Yes

-32768

32767

int

32

Yes

long

64

Yes

etc…

etc…

char (UTF-16)

16

No

0

65535

-2147483648 2147483647
Arrays…
•

are declared & initialized!

•

are ordered!

•

hold only elements of one type!

•

cannot change size!

•

start at 0!

•

…why!? Because of memory management.
1. Array Declaration
•

Two parts: a type and a name (just like a variable)!

•

Brackets after the variable type denote an array
of that variable type:!

! int[] xpositions; // array of integers
float[] widths;

// array of floats
// note that int != int[]
2. Array Initialization
•

An array is an object, so we use the new operator!

•

Much like variables, we have to give the initial
“state” of the array, which is the size:!

! xpositions = new int[42]; // empty array
widths = new float[78];

// empty array

// size must be an integer! new int[17.2]
1 & 2. One-line Array Creation
•

Just like variables we can declare and initialize
arrays in one line:!
int[] xpositions = new int[42];
float[] widths = new float[78];

•

Compare to declaring & initializing primitives:!
int xposition = 22;
float width = 32.5;
Getting an Element
To access an element in an array: !
arrayName[elementNumber]
int x = xpositions[0];
float myWidth = widths[13];
Setting an Element
1. Set single elements:!
! xpositions[0] = 13;
xpositions[1] = 26;
xpositions[2] = 39;
2. “Array Literal,” manually set elements:!
! int[] xpositions = {13,26,39};
// this only works at initialization
Demo!
Array Basics &!
Star Coordinates
Iteration!
•

!

We can iterate through an array using a loop:!
for(int i = 0; i < arrayName.length; i++) {
arrayName[i] = 0; // sets each to 0
}

•

!

We can also perform operations in the loop:!
for(int i = 0; i < arrayName.length; i++) {
arrayName[i] += 3; // increments each by 3
}
Demo!
Drawing a Line
For next time…
•

Friday: Quiz 3 (Function, Objects, & Arrays)!

•

Monday: Iteration 1 Group Presentations!

•

Read Shiffman, p. 153–162 (Arrays II)

More Related Content

Viewers also liked

13. Objects II
13. Objects II13. Objects II
13. Objects II
Joseph Murphy
 
25. Images I
25. Images I25. Images I
25. Images I
Joseph Murphy
 
Wc unit 3 work place commuication
Wc unit 3 work place commuicationWc unit 3 work place commuication
Wc unit 3 work place commuication
Ganesha Pandian
 
Rajesh babu
Rajesh babuRajesh babu
Rajesh babu
rajesh babu
 
Πασχαλινα εθιμα ανα την Ελλάδα
Πασχαλινα εθιμα ανα την ΕλλάδαΠασχαλινα εθιμα ανα την Ελλάδα
Πασχαλινα εθιμα ανα την Ελλάδα
kelesonia
 
การคิดราคาต้นทุน
การคิดราคาต้นทุนการคิดราคาต้นทุน
การคิดราคาต้นทุนJirawat Laiyung
 
Social media fox
Social media foxSocial media fox
Social media fox
tucker6
 
ero57 Introduction
ero57  Introductionero57  Introduction
ero57 Introduction
MoretonSmith
 
Recipe for a Custom Portfolio
Recipe for a Custom PortfolioRecipe for a Custom Portfolio
Recipe for a Custom Portfolio
Niccole @ KNG
 
Evaluation 2
Evaluation 2Evaluation 2
Evaluation 2
alishaparker
 
12. Objects I
12. Objects I12. Objects I
12. Objects I
Joseph Murphy
 
20140220pln final2
20140220pln final220140220pln final2
20140220pln final2
pguenthe
 
Resume Guidelines
Resume Guidelines Resume Guidelines
Resume Guidelines
Shama Salman
 
MoretonSmith Receivables Management Intro
MoretonSmith Receivables Management IntroMoretonSmith Receivables Management Intro
MoretonSmith Receivables Management Intro
MoretonSmith
 
4. Interaction
4. Interaction4. Interaction
4. Interaction
Joseph Murphy
 
Evaluation 4
Evaluation 4Evaluation 4
Evaluation 4
alishaparker
 
Evaluation 4
Evaluation 4Evaluation 4
Evaluation 4
alishaparker
 
Memoria DIAS 2013
Memoria DIAS 2013Memoria DIAS 2013
Memoria DIAS 2013dias_info
 
Socialmedia_Mitchell
Socialmedia_MitchellSocialmedia_Mitchell
Socialmedia_Mitchell
Abbi20
 

Viewers also liked (19)

13. Objects II
13. Objects II13. Objects II
13. Objects II
 
25. Images I
25. Images I25. Images I
25. Images I
 
Wc unit 3 work place commuication
Wc unit 3 work place commuicationWc unit 3 work place commuication
Wc unit 3 work place commuication
 
Rajesh babu
Rajesh babuRajesh babu
Rajesh babu
 
Πασχαλινα εθιμα ανα την Ελλάδα
Πασχαλινα εθιμα ανα την ΕλλάδαΠασχαλινα εθιμα ανα την Ελλάδα
Πασχαλινα εθιμα ανα την Ελλάδα
 
การคิดราคาต้นทุน
การคิดราคาต้นทุนการคิดราคาต้นทุน
การคิดราคาต้นทุน
 
Social media fox
Social media foxSocial media fox
Social media fox
 
ero57 Introduction
ero57  Introductionero57  Introduction
ero57 Introduction
 
Recipe for a Custom Portfolio
Recipe for a Custom PortfolioRecipe for a Custom Portfolio
Recipe for a Custom Portfolio
 
Evaluation 2
Evaluation 2Evaluation 2
Evaluation 2
 
12. Objects I
12. Objects I12. Objects I
12. Objects I
 
20140220pln final2
20140220pln final220140220pln final2
20140220pln final2
 
Resume Guidelines
Resume Guidelines Resume Guidelines
Resume Guidelines
 
MoretonSmith Receivables Management Intro
MoretonSmith Receivables Management IntroMoretonSmith Receivables Management Intro
MoretonSmith Receivables Management Intro
 
4. Interaction
4. Interaction4. Interaction
4. Interaction
 
Evaluation 4
Evaluation 4Evaluation 4
Evaluation 4
 
Evaluation 4
Evaluation 4Evaluation 4
Evaluation 4
 
Memoria DIAS 2013
Memoria DIAS 2013Memoria DIAS 2013
Memoria DIAS 2013
 
Socialmedia_Mitchell
Socialmedia_MitchellSocialmedia_Mitchell
Socialmedia_Mitchell
 

Similar to 14. Arrays I

Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
LavanyaJ28
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked lists
AfriyieCharles
 
Array
ArrayArray
Array
PRN USM
 
Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1
Vikram Nandini
 
Generative Coding Lecture notes using coding
Generative Coding Lecture notes using codingGenerative Coding Lecture notes using coding
Generative Coding Lecture notes using coding
ssuserff773c
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
SatyajeetGaur3
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
Ahmad Bashar Eter
 
Unit 4
Unit 4Unit 4
L10 array
L10 arrayL10 array
L10 array
teach4uin
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
Marcus Denker
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
ESUG
 
JavaScript 101 - Class 2
JavaScript 101 - Class 2JavaScript 101 - Class 2
JavaScript 101 - Class 2
Robert Pearce
 
Intro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and ArrayIntro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and Array
Jeongbae Oh
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
MamataAnilgod
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
AshishNayyar11
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
berekethailu2
 
Python review2
Python review2Python review2
Python review2
vibrantuser
 
C# Lesson 3
C# Lesson 3C# Lesson 3
C# Lesson 3
Bohdan Pashkovskyi
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
Yonas D. Ebren
 
Python review2
Python review2Python review2
Python review2
vibrantuser
 

Similar to 14. Arrays I (20)

Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked lists
 
Array
ArrayArray
Array
 
Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1
 
Generative Coding Lecture notes using coding
Generative Coding Lecture notes using codingGenerative Coding Lecture notes using coding
Generative Coding Lecture notes using coding
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
 
Unit 4
Unit 4Unit 4
Unit 4
 
L10 array
L10 arrayL10 array
L10 array
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
JavaScript 101 - Class 2
JavaScript 101 - Class 2JavaScript 101 - Class 2
JavaScript 101 - Class 2
 
Intro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and ArrayIntro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and Array
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
Python review2
Python review2Python review2
Python review2
 
C# Lesson 3
C# Lesson 3C# Lesson 3
C# Lesson 3
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Python review2
Python review2Python review2
Python review2
 

More from Joseph Murphy

30. Text II
30. Text II30. Text II
30. Text II
Joseph Murphy
 
29. Text I
29. Text I29. Text I
29. Text I
Joseph Murphy
 
28. Video II
28. Video II28. Video II
28. Video II
Joseph Murphy
 
27. Video I
27. Video I27. Video I
27. Video I
Joseph Murphy
 
26. Images II
26. Images II26. Images II
26. Images II
Joseph Murphy
 
24. Translation & Rotation II
24. Translation & Rotation II24. Translation & Rotation II
24. Translation & Rotation II
Joseph Murphy
 
23. Final Project Iteration II
23. Final Project Iteration II23. Final Project Iteration II
23. Final Project Iteration II
Joseph Murphy
 
34. Final Project Iteration 3
34. Final Project Iteration 334. Final Project Iteration 3
34. Final Project Iteration 3
Joseph Murphy
 
22. Translation & Rotation I
22. Translation & Rotation I22. Translation & Rotation I
22. Translation & Rotation I
Joseph Murphy
 
21. Mathematics II
21. Mathematics II21. Mathematics II
21. Mathematics II
Joseph Murphy
 
11. Functions II
11. Functions II11. Functions II
11. Functions II
Joseph Murphy
 
10. Function I
10. Function I10. Function I
10. Function I
Joseph Murphy
 
9. Loops II
9. Loops II9. Loops II
9. Loops II
Joseph Murphy
 
8. Loops I
8. Loops I8. Loops I
8. Loops I
Joseph Murphy
 
7. Conditionals II
7. Conditionals II7. Conditionals II
7. Conditionals II
Joseph Murphy
 
6. Conditionals I
6. Conditionals I6. Conditionals I
6. Conditionals I
Joseph Murphy
 
5. Variables
5. Variables5. Variables
5. Variables
Joseph Murphy
 
3. Final Project - Intro
3. Final Project - Intro3. Final Project - Intro
3. Final Project - Intro
Joseph Murphy
 
2. Processing
2. Processing2. Processing
2. Processing
Joseph Murphy
 

More from Joseph Murphy (19)

30. Text II
30. Text II30. Text II
30. Text II
 
29. Text I
29. Text I29. Text I
29. Text I
 
28. Video II
28. Video II28. Video II
28. Video II
 
27. Video I
27. Video I27. Video I
27. Video I
 
26. Images II
26. Images II26. Images II
26. Images II
 
24. Translation & Rotation II
24. Translation & Rotation II24. Translation & Rotation II
24. Translation & Rotation II
 
23. Final Project Iteration II
23. Final Project Iteration II23. Final Project Iteration II
23. Final Project Iteration II
 
34. Final Project Iteration 3
34. Final Project Iteration 334. Final Project Iteration 3
34. Final Project Iteration 3
 
22. Translation & Rotation I
22. Translation & Rotation I22. Translation & Rotation I
22. Translation & Rotation I
 
21. Mathematics II
21. Mathematics II21. Mathematics II
21. Mathematics II
 
11. Functions II
11. Functions II11. Functions II
11. Functions II
 
10. Function I
10. Function I10. Function I
10. Function I
 
9. Loops II
9. Loops II9. Loops II
9. Loops II
 
8. Loops I
8. Loops I8. Loops I
8. Loops I
 
7. Conditionals II
7. Conditionals II7. Conditionals II
7. Conditionals II
 
6. Conditionals I
6. Conditionals I6. Conditionals I
6. Conditionals I
 
5. Variables
5. Variables5. Variables
5. Variables
 
3. Final Project - Intro
3. Final Project - Intro3. Final Project - Intro
3. Final Project - Intro
 
2. Processing
2. Processing2. Processing
2. Processing
 

Recently uploaded

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 

Recently uploaded (20)

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 

14. Arrays I

  • 1. From last time… • Please turn in Homework 2! • We made a beautiful object-oriented night sky! • Remember that a class has:! 1. a name! 2. attributes (variables)! 3. a constructor (run via ‘new’)! 4. and methods (actions) ! ! ! !
  • 2.
  • 4. Rules of an Array • An array is a ordered “list” object which which holds multiple “elements” (variables or objects)! • It can hold only “elements” of one type! • It cannot change size after it has been created! • It starts at the 0th position! • An array, like a variable, is declared & initialized
  • 5. Memory int x; How does the compiler know! how much space to reserve?
  • 7. Arrays… • are declared & initialized! • are ordered! • hold only elements of one type! • cannot change size! • start at 0! • …why!? Because of memory management.
  • 8. 1. Array Declaration • Two parts: a type and a name (just like a variable)! • Brackets after the variable type denote an array of that variable type:! ! int[] xpositions; // array of integers float[] widths; // array of floats // note that int != int[]
  • 9. 2. Array Initialization • An array is an object, so we use the new operator! • Much like variables, we have to give the initial “state” of the array, which is the size:! ! xpositions = new int[42]; // empty array widths = new float[78]; // empty array // size must be an integer! new int[17.2]
  • 10. 1 & 2. One-line Array Creation • Just like variables we can declare and initialize arrays in one line:! int[] xpositions = new int[42]; float[] widths = new float[78]; • Compare to declaring & initializing primitives:! int xposition = 22; float width = 32.5;
  • 11. Getting an Element To access an element in an array: ! arrayName[elementNumber] int x = xpositions[0]; float myWidth = widths[13];
  • 12. Setting an Element 1. Set single elements:! ! xpositions[0] = 13; xpositions[1] = 26; xpositions[2] = 39; 2. “Array Literal,” manually set elements:! ! int[] xpositions = {13,26,39}; // this only works at initialization
  • 14. Iteration! • ! We can iterate through an array using a loop:! for(int i = 0; i < arrayName.length; i++) { arrayName[i] = 0; // sets each to 0 } • ! We can also perform operations in the loop:! for(int i = 0; i < arrayName.length; i++) { arrayName[i] += 3; // increments each by 3 }
  • 16.
  • 17. For next time… • Friday: Quiz 3 (Function, Objects, & Arrays)! • Monday: Iteration 1 Group Presentations! • Read Shiffman, p. 153–162 (Arrays II)