SlideShare a Scribd company logo
1 of 28
1
 Arrays
 Declaring and constructing arrays
 Using and returning arrays
 Aliasing
2
 ArrayList is used when the size of a collection
is not known in advance
 But in some situations the maximum collection size
can be pre-determined
 A special fixed-size collection type is available: the array
 Arrays can store object references or primitive values
 Arrays use a special syntax
3
 An array is an indexed sequence of variables
of the same type
 The array is called a
 Its elements are called a[0], a[1], a[2], etc.
◦ Each element is a separate variable
 Notice that (as with ArrayList) indexing starts from 0
a[0] a[1] a[2] a[3] a[4]
a
4
a[5]
 Arrays are used when we have large numbers of identical objects that
we want to operate on as a collection
◦ A collection of student marks that we want to analyse
◦ A collection of temperatures that we want to average
◦ A collection of names that we want to sort
 e.g. Bureau of Meteorology data
5
ALBANY
Max 25.1 25.1 24.1 21.5 18.7 16.6 15.7 15.9 17.4 18.8 20.8 23.4
Min 13.5 14.3 13.3 11.6 9.8 8.1 7.4 7.4 7.9 9.0 10.6 12.3
Rain 28 25 29 66 102 104 126 104 81 80 46 24
PERTH AIRPORT
Max 31.4 31.7 29.5 25.2 21.4 18.7 17.6 18.3 20.0 22.3 25.4 28.5
Min 16.7 17.4 15.7 12.7 10.2 9.0 8.0 7.9 8.8 10.1 12.4 14.6
Rain 8 14 15 46 108 175 164 117 68 48 25 12
 An array is declared using similar syntax to any other variable
int[] a;
 Declares a to be a variable representing an array of ints
double[] temps;
 Declares temps to be a variable representing an array of
doubles
String[] names;
 Declares names to be a variable representing an array of
Strings
Student[] marks;
 Declares marks to be a variable representing an array of
Student objects
6
 An array is an object in a Java program
 Therefore the declaration simply creates a reference to “point to” the
array, but does not create the array itself
 Hence the declaration
int[] a;
creates a space called a, big enough to hold an object
reference, and initially set to the special value null
a
null Only space for the array
reference has been
created, not for the array
itself
7
 In order to actually create the array, we must use the keyword new
(just like creating any other object)
int[] a;
a = new int[7];
0 0 0 0 00 0
a
An object is created that
contains seven variables of type
int
8
The expression a.length can be
used to refer to the size of a
• The seven variables do not have individual names
• They are referred to by the array name, and an index
0 0 0 0 00 0
a
9
• Array elements can be used in the same ways and
in the same contexts as any other variable of that
type
a[0] a[1]
0
a[2]
7
a[3]
-1
a[5]
0
a[4]
15
a[6]
17
a[4] = 15;
a[2] = 7;
a[3] = 2*a[2] – a[4];
a[6] = a[0] + 17;
10
 A lot of the power of arrays comes from the fact that
the index can be a variable or an expression
int x = 3;
a[x] = 5;
a[7-x] = 44;
a[0]
0
a[1]
0
a[2]
7
a[3]
5
a[5]
0
a[4]
44
a[6]
17
11
private int sum(int[] a)
{
int sum = 0;
for (int i : a)
{
sum = sum + i;
}
return sum;
}
12
• Here i is an element of the array a
private int sum(int[] a)
{
int sum = 0;
for (int i = 0; i < a.length; i++)
{
sum = sum + a[i];
}
return sum;
}
13
• Here i is being used as an index into the array a
• Note that this is only well-defined for non-empty
arrays!
• Again i is an element of the array a
private int max(int[] a)
{
int max = a[0];
for (int i : a)
{
if (i > max) max = i;
}
return max;
}
14
private int max(int[] a)
{
int max = a[0];
for (int i = 1; i < a.length; i++)
{
if (a[i] > max) max = a[i];
}
return max;
}
15
• Again i is being used as an index into the array a
private int max(int[] a)
{
int k = 0;
for (int i = 1; i < a.length; i++)
{
if (a[i] > a[k]) k = i;
}
return k;
}
16
• A for-each loop will not work for this example
 An array literal is written using {}
 Array literals in this form
can only be used in declarations
 Related uses require new
private int[] numbers = {3, 15, 4, 5};
declaration,
creation, and
initialization
numbers = new int[] {3, 15, 4, 5};
17
private int[] sums(int[] a)
{
int[] sums = new int[a.length];
sums[0] = a[0];
for (int i = 1; i < a.length; i++)
{
sums[i] = sums[i–1] + a[i];
}
return sums;
} 18
• Suppose we want to find the running sums of a
sums({2,5,–8,3}) = {2,7,–1,2}
private int[] avs(int[] a)
{
int[] avs = new int[a.length / 2];
for (int i = 0; i < a.length; i = i+2)
{
avs[i / 2] = (a[i] + a[i+1]) / 2;
}
return avs;
}
19
• Suppose we want to average each pair of elements of a
avs({2,6,–8,2}) = {4,–3}
private void sums(int[] a)
{
for (int i = 1; i < a.length; i++)
{
a[i] = a[i–1] + a[i];
}
}
20
• Sometimes instead of creating a new
array, we want to update the old one
• But this method doesn’t return
anything…
21
int[] a, b;
a b
22
int[] a, b;
a = new int[3];
a[0]
0
a[1]
0
a[2]
0
a b
23
int[] a, b;
a = new int[3];
b = a;
a[0]
0
a[1]
0
a[2]
0
a b
24
int[] a, b;
a = new int[3];
b = a;
System.out.println(b[2]);
a[0]
0
a[1]
0
a[2]
0
a b
0
25
int[] a, b;
a = new int[3];
b = a;
System.out.println(b[2]);
a[2] = 9;
a[0]
0
a[1]
0
a[2]
9
a b
0
26
int[] a, b;
a = new int[3];
b = a;
System.out.println(b[2]);
a[2] = 9;
System.out.println(b[2]);
a[0]
0
a[1]
0
a[2]
9
a b
0
9
27
• This is called aliasing
• Basically one array with two names
• It applies in the same way with any
other object-types too
• Be careful with equality over arrays
• In some applications we might want to
use aliasing deliberately
 Arrays were discussed.
 Declaration and construction of arrays
 Different methods for using and returning arrays discussed.
 Aliasing with an example was discussed.
28

More Related Content

What's hot (20)

Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Arrays
ArraysArrays
Arrays
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Array data structure
Array data structureArray data structure
Array data structure
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
array
array array
array
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Python array
Python arrayPython array
Python array
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Array
ArrayArray
Array
 
Array in Java
Array in JavaArray in Java
Array in Java
 

Viewers also liked

Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2jomerson remorosa
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureZaid Shabbir
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...sethuraman R
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppteShikshak
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)EngineerBabu
 
Array in c language
Array in c languageArray in c language
Array in c languagehome
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 

Viewers also liked (12)

Arrays
ArraysArrays
Arrays
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Array in C
Array in CArray in C
Array in C
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Arrays
ArraysArrays
Arrays
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similar to Lecture 6 - Arrays

Similar to Lecture 6 - Arrays (20)

Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Arrays
ArraysArrays
Arrays
 
2 arrays
2   arrays2   arrays
2 arrays
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structures
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Generative Coding Lecture notes using coding
Generative Coding Lecture notes using codingGenerative Coding Lecture notes using coding
Generative Coding Lecture notes using coding
 

More from Syed Afaq Shah MACS CP

Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysSyed Afaq Shah MACS CP
 
Lecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loopsLecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loopsSyed Afaq Shah MACS CP
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsSyed Afaq Shah MACS CP
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and VariablesSyed Afaq Shah MACS CP
 
Lecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and ConstructorsLecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and ConstructorsSyed Afaq Shah MACS CP
 

More from Syed Afaq Shah MACS CP (7)

Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 
Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arrays
 
Lecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loopsLecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loops
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
Lecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and ConstructorsLecture 2 - Classes, Fields, Parameters, Methods and Constructors
Lecture 2 - Classes, Fields, Parameters, Methods and Constructors
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 

Recently uploaded

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 

Recently uploaded (20)

Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 

Lecture 6 - Arrays

  • 1. 1
  • 2.  Arrays  Declaring and constructing arrays  Using and returning arrays  Aliasing 2
  • 3.  ArrayList is used when the size of a collection is not known in advance  But in some situations the maximum collection size can be pre-determined  A special fixed-size collection type is available: the array  Arrays can store object references or primitive values  Arrays use a special syntax 3
  • 4.  An array is an indexed sequence of variables of the same type  The array is called a  Its elements are called a[0], a[1], a[2], etc. ◦ Each element is a separate variable  Notice that (as with ArrayList) indexing starts from 0 a[0] a[1] a[2] a[3] a[4] a 4 a[5]
  • 5.  Arrays are used when we have large numbers of identical objects that we want to operate on as a collection ◦ A collection of student marks that we want to analyse ◦ A collection of temperatures that we want to average ◦ A collection of names that we want to sort  e.g. Bureau of Meteorology data 5 ALBANY Max 25.1 25.1 24.1 21.5 18.7 16.6 15.7 15.9 17.4 18.8 20.8 23.4 Min 13.5 14.3 13.3 11.6 9.8 8.1 7.4 7.4 7.9 9.0 10.6 12.3 Rain 28 25 29 66 102 104 126 104 81 80 46 24 PERTH AIRPORT Max 31.4 31.7 29.5 25.2 21.4 18.7 17.6 18.3 20.0 22.3 25.4 28.5 Min 16.7 17.4 15.7 12.7 10.2 9.0 8.0 7.9 8.8 10.1 12.4 14.6 Rain 8 14 15 46 108 175 164 117 68 48 25 12
  • 6.  An array is declared using similar syntax to any other variable int[] a;  Declares a to be a variable representing an array of ints double[] temps;  Declares temps to be a variable representing an array of doubles String[] names;  Declares names to be a variable representing an array of Strings Student[] marks;  Declares marks to be a variable representing an array of Student objects 6
  • 7.  An array is an object in a Java program  Therefore the declaration simply creates a reference to “point to” the array, but does not create the array itself  Hence the declaration int[] a; creates a space called a, big enough to hold an object reference, and initially set to the special value null a null Only space for the array reference has been created, not for the array itself 7
  • 8.  In order to actually create the array, we must use the keyword new (just like creating any other object) int[] a; a = new int[7]; 0 0 0 0 00 0 a An object is created that contains seven variables of type int 8 The expression a.length can be used to refer to the size of a
  • 9. • The seven variables do not have individual names • They are referred to by the array name, and an index 0 0 0 0 00 0 a 9
  • 10. • Array elements can be used in the same ways and in the same contexts as any other variable of that type a[0] a[1] 0 a[2] 7 a[3] -1 a[5] 0 a[4] 15 a[6] 17 a[4] = 15; a[2] = 7; a[3] = 2*a[2] – a[4]; a[6] = a[0] + 17; 10
  • 11.  A lot of the power of arrays comes from the fact that the index can be a variable or an expression int x = 3; a[x] = 5; a[7-x] = 44; a[0] 0 a[1] 0 a[2] 7 a[3] 5 a[5] 0 a[4] 44 a[6] 17 11
  • 12. private int sum(int[] a) { int sum = 0; for (int i : a) { sum = sum + i; } return sum; } 12 • Here i is an element of the array a
  • 13. private int sum(int[] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; } return sum; } 13 • Here i is being used as an index into the array a
  • 14. • Note that this is only well-defined for non-empty arrays! • Again i is an element of the array a private int max(int[] a) { int max = a[0]; for (int i : a) { if (i > max) max = i; } return max; } 14
  • 15. private int max(int[] a) { int max = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] > max) max = a[i]; } return max; } 15 • Again i is being used as an index into the array a
  • 16. private int max(int[] a) { int k = 0; for (int i = 1; i < a.length; i++) { if (a[i] > a[k]) k = i; } return k; } 16 • A for-each loop will not work for this example
  • 17.  An array literal is written using {}  Array literals in this form can only be used in declarations  Related uses require new private int[] numbers = {3, 15, 4, 5}; declaration, creation, and initialization numbers = new int[] {3, 15, 4, 5}; 17
  • 18. private int[] sums(int[] a) { int[] sums = new int[a.length]; sums[0] = a[0]; for (int i = 1; i < a.length; i++) { sums[i] = sums[i–1] + a[i]; } return sums; } 18 • Suppose we want to find the running sums of a sums({2,5,–8,3}) = {2,7,–1,2}
  • 19. private int[] avs(int[] a) { int[] avs = new int[a.length / 2]; for (int i = 0; i < a.length; i = i+2) { avs[i / 2] = (a[i] + a[i+1]) / 2; } return avs; } 19 • Suppose we want to average each pair of elements of a avs({2,6,–8,2}) = {4,–3}
  • 20. private void sums(int[] a) { for (int i = 1; i < a.length; i++) { a[i] = a[i–1] + a[i]; } } 20 • Sometimes instead of creating a new array, we want to update the old one • But this method doesn’t return anything…
  • 22. 22 int[] a, b; a = new int[3]; a[0] 0 a[1] 0 a[2] 0 a b
  • 23. 23 int[] a, b; a = new int[3]; b = a; a[0] 0 a[1] 0 a[2] 0 a b
  • 24. 24 int[] a, b; a = new int[3]; b = a; System.out.println(b[2]); a[0] 0 a[1] 0 a[2] 0 a b 0
  • 25. 25 int[] a, b; a = new int[3]; b = a; System.out.println(b[2]); a[2] = 9; a[0] 0 a[1] 0 a[2] 9 a b 0
  • 26. 26 int[] a, b; a = new int[3]; b = a; System.out.println(b[2]); a[2] = 9; System.out.println(b[2]); a[0] 0 a[1] 0 a[2] 9 a b 0 9
  • 27. 27 • This is called aliasing • Basically one array with two names • It applies in the same way with any other object-types too • Be careful with equality over arrays • In some applications we might want to use aliasing deliberately
  • 28.  Arrays were discussed.  Declaration and construction of arrays  Different methods for using and returning arrays discussed.  Aliasing with an example was discussed. 28