SlideShare a Scribd company logo
1 of 84
Javaā„¢ Software Solutions: Foundations of
Program Design
Ninth Edition
Chapter 8
Arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays (1 of 7)
ā€¢ Arrays are objects that help us organize large amounts of
information
ā€¢ Chapter 8 focuses on:
ā€“ array declaration and use
ā€“ bounds checking and capacity
ā€“ arrays that store object references
ā€“ variable length parameter lists
ā€“ multidimensional arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Outline (1 of 7)
ā€¢ Declaring and Using Arrays
ā€¢ Arrays of Objects
ā€¢ Variable Length Parameter Lists
ā€¢ Two-Dimensional Arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays (2 of 7)
ā€¢ The ArrayList class, introduced in Chapter 5, is used to
organize a list of objects
ā€¢ It is a class in the Java API
ā€¢ An array is a programming language construct used to
organize a list of objects
ā€¢ It has special syntax to access elements
ā€¢ As its name implies, the ArrayList class uses an array
internally to manage the list of objects
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays (3 of 7)
ā€¢ An array is an ordered list of values:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays (4 of 7)
ā€¢ A particular value in an array is referenced using the array
name followed by the index in brackets
ā€¢ For example, the expression
refers to the value 94 (the 3rd value in the array)
ā€¢ That expression represents a place to store a single
integer and can be used wherever an integer variable can
be used
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays (5 of 7)
ā€¢ For example, an array element can be assigned a value,
printed, or used in a calculation:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays (6 of 7)
ā€¢ The values held in an array are called array elements
ā€¢ An array stores multiple values of the same type - the
element type
ā€¢ The element type can be a primitive type or an object
reference
ā€¢ Therefore, we can create an array of integers, an array of
characters, an array of String objects, an array of Coin
objects, etc.
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays (7 of 7)
ā€¢ In Java, the array itself is an object that must be
instantiated
ā€¢ Another way to depict the scores array:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Declaring Arrays (1 of 2)
ā€¢ The scores array could be declared as follows:
ā€¢ The type of the variable scores is int[] (an array of
integers)
ā€¢ Note that the array type does not specify its size, but each
object of that type has a specific size
ā€¢ The reference variable scores is set to a new array object
that can hold 10 integers
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Declaring Arrays (2 of 2)
ā€¢ Some other examples of array declarations:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Using Arrays
ā€¢ The for-each version of the for loop can be used when
processing array elements:
ā€¢ This is only appropriate when processing all array
elements starting at index 0
ā€¢ It canā€™t be used to set the array values
ā€¢ See BasicArray.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.1 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.1 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Basic Array Example
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 1 (1 of 2)
Write an array declaration to represent the ages of 100
children.
Write code that prints each value in an array of integers
named values.
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 1 (2 of 2)
Write an array declaration to represent the ages of 100
children.
Write code that prints each value in an array of integers
named values.
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Bounds Checking (1 of 3)
ā€¢ Once an array is created, it has a fixed size
ā€¢ An index used in an array reference must specify a valid
element
ā€¢ That is, the index value must be in range 0 to Nāˆ’1
ā€¢ The Java interpreter throws an
ArrayIndexOutOfBoundsException if an array index
is out of bounds
ā€¢ This is called automatic bounds checking
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Bounds Checking (2 of 3)
ā€¢ For example, if the array codes can hold 100 values, it
can be indexed from 0 to 99
ā€¢ If the value of count is 100, then the following reference
will cause an exception to be thrown:
ā€¢ Itā€™s common to introduce off-by-one errors when using
arrays:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Bounds Checking (3 of 3)
ā€¢ Each array object has a public constant called length
that stores the size of the array
ā€¢ It is referenced using the array name:
ā€¢ Note that length holds the number of elements, not the
largest index
ā€¢ See ReverseOrder.java
ā€¢ See LetterCount.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.2 (1 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.2 (2 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.2 (3 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.3 (1 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.3 (2 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.3 (3 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.3 (4 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Alternate Array Syntax
ā€¢ The brackets of the array type can be associated with the
element type or with the name of the array
ā€¢ Therefore the following two declarations are equivalent:
ā€¢ The first format generally is more readable and should be
used
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Initializer Lists (1 of 2)
ā€¢ An initializer list can be used to instantiate and fill an
array in one step
ā€¢ The values are delimited by braces and separated by
commas
ā€¢ Examples:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Initializer Lists (2 of 2)
ā€¢ Note that when an initializer list is used:
ā€“ the new operator is not used
ā€“ no size value is specified
ā€¢ The size of the array is determined by the number of items
in the list
ā€¢ An initializer list can be used only in the array declaration
ā€¢ See Primes.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.4 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.4 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays as Parameters
ā€¢ An entire array can be passed as a parameter to a method
ā€¢ Like any other object, the reference to the array is passed,
making the formal and actual parameters aliases of each
other
ā€¢ Therefore, changing an array element within the method
changes the original
ā€¢ An individual array element can be passed to a method as
well, in which case the type of the formal parameter is the
same as the element type
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Outline (2 of 7)
ā€¢ Declaring and Using Arrays
ā€¢ Arrays of Objects
ā€¢ Variable Length Parameter Lists
ā€¢ Two-Dimensional Arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects (1 of 7)
ā€¢ The elements of an array can be object references
ā€¢ The following declaration reserves space to store 5
references to String objects
ā€¢ It does NOT create the String objects themselves
ā€¢ Initially an array of objects holds null references
ā€¢ Each object stored in an array must be instantiated
separately
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects (2 of 7)
ā€¢ The words array when initially declared:
ā€¢ At this point, the following line of code would throw a
NullPointerException:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects (3 of 7)
ā€¢ After some String objects are created and stored in the
array:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects (4 of 7)
ā€¢ Keep in mind that String objects can be created using
literals
ā€¢ The following declaration creates an array object called
verbs and fills it with four String objects created using
string literals
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects (5 of 7)
ā€¢ The following example creates an array of Grade objects,
each with a string representation and a numeric lower
bound
ā€¢ The letter grades include plus and minus designations, so
must be stored as strings instead of char
ā€¢ See GradeRange.java
ā€¢ See Grade.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.5 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.5 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.6 (1 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.6 (2 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.6 (3 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects (6 of 7)
ā€¢ Now letā€™s look at an example that manages a collection
of DVD objects
ā€¢ An initial capacity of 100 is created for the collection
ā€¢ If more room is needed, a private method is used to
create a larger array and transfer the current DVDs
ā€¢ See Movies.java
ā€¢ See DVDCollection.java
ā€¢ See DVD.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.7 (1 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.7 (2 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.7 (3 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.8 (1 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.8 (2 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.8 (3 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.8 (4 of 4)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.9 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.9 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects (7 of 7)
ā€¢ A UML diagram for the Movies program:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Command-Line Arguments
ā€¢ The signature of the main method indicates that it takes
an array of String objects as a parameter
ā€¢ These values come from command-line arguments that
are provided when the interpreter is invoked
ā€¢ For example, the following invocation of the interpreter
passes three String objects into the main method of the
StateEval program:
ā€¢ See NameTag.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.10 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.10 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Outline (3 of 7)
ā€¢ Declaring and Using Arrays
ā€¢ Arrays of Objects
ā€¢ Variable Length Parameter Lists
ā€¢ Two-Dimensional Arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Variable Length Parameter Lists (1 of 7)
ā€¢ Suppose we wanted to create a method that processed a
different amount of data from one invocation to the next
ā€¢ For example, letā€™s define a method called average that
returns the average of a set of integer parameters
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Variable Length Parameter Lists (2 of 7)
ā€¢ We could define overloaded versions of the average
method
ā€“ Downside: weā€™d need a separate version of the method
for each additional parameter
ā€¢ We could define the method to accept an array of integers
ā€“ Downside: weā€™d have to create the array and store the
integers prior to calling the method each time
ā€¢ Instead, Java provides a convenient way to create variable
length parameter lists
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Variable Length Parameter Lists (3 of 7)
ā€¢ Using special syntax in the formal parameter list, we can
define a method to accept any number of parameters of the
same type
ā€¢ For each call, the parameters are automatically put into an
array for easy processing in the method
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Variable Length Parameter Lists (4 of 7)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Variable Length Parameter Lists (5 of 7)
ā€¢ The type of the parameter can be any primitive or object
type:
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 2 (1 of 2)
Write method called distance that accepts a variable
number of integers (which each represent the distance of
one leg of a trip) and returns the total distance of the trip.
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 2 (2 of 2)
Write method called distance that accepts a variable
number of integers (which each represent the distance of
one leg of a trip) and returns the total distance of the trip.
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Variable Length Parameter Lists (6 of 7)
ā€¢ A method that accepts a variable number of parameters
can also accept other parameters
ā€¢ The following method accepts an int, a String object,
and a variable number of double values into an array
called nums
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Variable Length Parameter Lists (7 of 7)
ā€¢ The varying number of parameters must come last in the
formal arguments
ā€¢ A method cannot accept two sets of varying parameters
ā€¢ Constructors can also be set up to accept a variable
number of parameters
ā€¢ See VariableParameters.java
ā€¢ See Family.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.11 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.11 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.12 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.12 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Outline (4 of 7)
ā€¢ Declaring and Using Arrays
ā€¢ Arrays of Objects
ā€¢ Variable Length Parameter Lists
ā€¢ Two-Dimensional Arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Two-Dimensional Arrays (1 of 3)
ā€¢ A one-dimensional array stores a list of elements
ā€¢ A two-dimensional array can be thought of as a table of
elements, with rows and columns
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Two-Dimensional Arrays (2 of 3)
ā€¢ To be precise, in Java a two-dimensional array is an array
of arrays
ā€¢ A two-dimensional array is declared by specifying the size
of each dimension separately:
ā€¢ A array element is referenced using two index values:
ā€¢ The array stored in one row can be specified using one
index
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Two-Dimensional Arrays (3 of 3)
Expression Type Description
table
Computer code reads,
i n t left bracket
right bracket left
bracket right bracket
2D array of integers, or
array of integer arrays
table[5]
Computer code reads,
i n t left bracket
right bracket
array of integers
table[5][12]
Computer code reads,
i n t Integer
int[][]
int[]
int
ā€¢ See TwoDArray.java
ā€¢ See SodaSurvey.java
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.13 (1 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.13 (2 of 2)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.14 (1 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.14 (2 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Listing 8.14 (3 of 3)
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Multidimensional Arrays
ā€¢ An array can have many dimensions - if it has more than
one dimension, it is called a multidimensional array
ā€¢ Each dimension subdivides the previous one into the
specified number of elements
ā€¢ Each dimension has its own length constant
ā€¢ Because each dimension is an array of array references,
the arrays within one dimension can be of different lengths
ā€“ these are sometimes called ragged arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Summary
ā€¢ Chapter 8 has focused on:
ā€“ array declaration and use
ā€“ bounds checking and capacity
ā€“ arrays that store object references
ā€“ variable length parameter lists
ā€“ multidimensional arrays
Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
Copyright
This work is protected by United States copyright laws and is
provided solely for the use of instructors in teaching their
courses and assessing student learning. Dissemination or sale of
any part of this work (including on the World Wide Web) will
destroy the integrity of the work and is not permitted. The work
and materials from it should never be made available to students
except by instructors using the accompanying text in their
classes. All recipients of this work are expected to abide by these
restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.

More Related Content

What's hot

Machine Learning - Supervised learning
Machine Learning - Supervised learningMachine Learning - Supervised learning
Machine Learning - Supervised learningManeesha Caldera
Ā 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in dschauhankapil
Ā 
Data strucutre basic introduction
Data strucutre basic introductionData strucutre basic introduction
Data strucutre basic introductionmanirajan12
Ā 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and AnalysisAkashBorse2
Ā 
Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5dplunkett
Ā 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2dplunkett
Ā 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eGina Bullock
Ā 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsAnushdika Jeganathan
Ā 
Dsa ā€“ data structure and algorithms sorting
Dsa ā€“ data structure and algorithms  sortingDsa ā€“ data structure and algorithms  sorting
Dsa ā€“ data structure and algorithms sortingsajinis3
Ā 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesHoang Nguyen
Ā 
An Overview of NaĆÆve Bayes Classifier
An Overview of NaĆÆve Bayes Classifier An Overview of NaĆÆve Bayes Classifier
An Overview of NaĆÆve Bayes Classifier ananth
Ā 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset PreparationAndrew Ferlitsch
Ā 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data TypeskarthikeyanC40
Ā 

What's hot (18)

DSA-Lecture-05
DSA-Lecture-05DSA-Lecture-05
DSA-Lecture-05
Ā 
Naive Bayes | Statistics
Naive Bayes | StatisticsNaive Bayes | Statistics
Naive Bayes | Statistics
Ā 
Machine Learning - Supervised learning
Machine Learning - Supervised learningMachine Learning - Supervised learning
Machine Learning - Supervised learning
Ā 
DSA - Lecture 04
DSA - Lecture 04DSA - Lecture 04
DSA - Lecture 04
Ā 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
Ā 
Data strucutre basic introduction
Data strucutre basic introductionData strucutre basic introduction
Data strucutre basic introduction
Ā 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
Ā 
Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5
Ā 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
Ā 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
Ā 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
Ā 
Dsa ā€“ data structure and algorithms sorting
Dsa ā€“ data structure and algorithms  sortingDsa ā€“ data structure and algorithms  sorting
Dsa ā€“ data structure and algorithms sorting
Ā 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Ā 
An Overview of NaĆÆve Bayes Classifier
An Overview of NaĆÆve Bayes Classifier An Overview of NaĆÆve Bayes Classifier
An Overview of NaĆÆve Bayes Classifier
Ā 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
Ā 
Mahout part2
Mahout part2Mahout part2
Mahout part2
Ā 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
Ā 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Ā 

Similar to Upstate CSCI 200 Java Chapter 8 - Arrays

Week06
Week06Week06
Week06hccit
Ā 
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...Amazon Web Services
Ā 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8mlrbrown
Ā 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementationLavanyaJ28
Ā 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collectionsphanleson
Ā 
Week02
Week02Week02
Week02hccit
Ā 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesDanWooster1
Ā 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08Terry Yoast
Ā 
Learning core java
Learning core javaLearning core java
Learning core javaAbhay Bharti
Ā 
Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...
Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...
Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...Amazon Web Services
Ā 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxAmanRai352102
Ā 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classesteach4uin
Ā 

Similar to Upstate CSCI 200 Java Chapter 8 - Arrays (20)

pl12ch6.ppt
pl12ch6.pptpl12ch6.ppt
pl12ch6.ppt
Ā 
Variable
VariableVariable
Variable
Ā 
Week06
Week06Week06
Week06
Ā 
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Search Your DynamoDB Data with Amazon Elasticsearch Service (ANT302) - AWS re...
Ā 
Apex code (Salesforce)
Apex code (Salesforce)Apex code (Salesforce)
Apex code (Salesforce)
Ā 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
Ā 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
Ā 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
Ā 
Generics collections
Generics collectionsGenerics collections
Generics collections
Ā 
Week02
Week02Week02
Week02
Ā 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using Classes
Ā 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
Ā 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
Ā 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
Ā 
Learning core java
Learning core javaLearning core java
Learning core java
Ā 
Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...
Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...
Searching Your Data with Amazon Elasticsearch Service (ANT384) - AWS re:Inven...
Ā 
Arrays
ArraysArrays
Arrays
Ā 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
Ā 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
Ā 
java.pdf
java.pdfjava.pdf
java.pdf
Ā 

More from DanWooster1

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentDanWooster1
Ā 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingDanWooster1
Ā 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9DanWooster1
Ā 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4DanWooster1
Ā 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4DanWooster1
Ā 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3DanWooster1
Ā 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2DanWooster1
Ā 
Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1DanWooster1
Ā 
Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3DanWooster1
Ā 
Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2DanWooster1
Ā 
Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1DanWooster1
Ā 
Upstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPUpstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPDanWooster1
Ā 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsDanWooster1
Ā 
CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01DanWooster1
Ā 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsDanWooster1
Ā 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQueryDanWooster1
Ā 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13DanWooster1
Ā 
Upstate CSCI 450 PHP
Upstate CSCI 450 PHPUpstate CSCI 450 PHP
Upstate CSCI 450 PHPDanWooster1
Ā 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesDanWooster1
Ā 
C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7DanWooster1
Ā 

More from DanWooster1 (20)

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
Ā 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
Ā 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
Ā 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
Ā 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
Ā 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
Ā 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
Ā 
Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1
Ā 
Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3
Ā 
Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2
Ā 
Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1
Ā 
Upstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPUpstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOP
Ā 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & Expressions
Ā 
CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01
Ā 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
Ā 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
Ā 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
Ā 
Upstate CSCI 450 PHP
Upstate CSCI 450 PHPUpstate CSCI 450 PHP
Upstate CSCI 450 PHP
Ā 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
Ā 
C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7
Ā 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
Ā 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
Ā 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
Ā 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
Ā 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
Ā 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
Ā 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
Ā 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
Ā 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
Ā 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
Ā 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
Ā 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
Ā 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
Ā 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
Ā 
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
Ā 
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
Ā 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
Ā 
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
Ā 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
Ā 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
Ā 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
Ā 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Ā 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Ā 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Ā 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
Ā 
Model Call Girl in Bikash Puri Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Bikash Puri  Delhi reach out to us at šŸ”9953056974šŸ”Model Call Girl in Bikash Puri  Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Bikash Puri Delhi reach out to us at šŸ”9953056974šŸ”
Ā 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
Ā 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
Ā 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Ā 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
Ā 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
Ā 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
Ā 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
Ā 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
Ā 
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
Ā 
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
Ā 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
Ā 
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
Ā 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
Ā 

Upstate CSCI 200 Java Chapter 8 - Arrays

  • 1. Javaā„¢ Software Solutions: Foundations of Program Design Ninth Edition Chapter 8 Arrays Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved
  • 2. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays (1 of 7) ā€¢ Arrays are objects that help us organize large amounts of information ā€¢ Chapter 8 focuses on: ā€“ array declaration and use ā€“ bounds checking and capacity ā€“ arrays that store object references ā€“ variable length parameter lists ā€“ multidimensional arrays
  • 3. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Outline (1 of 7) ā€¢ Declaring and Using Arrays ā€¢ Arrays of Objects ā€¢ Variable Length Parameter Lists ā€¢ Two-Dimensional Arrays
  • 4. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays (2 of 7) ā€¢ The ArrayList class, introduced in Chapter 5, is used to organize a list of objects ā€¢ It is a class in the Java API ā€¢ An array is a programming language construct used to organize a list of objects ā€¢ It has special syntax to access elements ā€¢ As its name implies, the ArrayList class uses an array internally to manage the list of objects
  • 5. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays (3 of 7) ā€¢ An array is an ordered list of values:
  • 6. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays (4 of 7) ā€¢ A particular value in an array is referenced using the array name followed by the index in brackets ā€¢ For example, the expression refers to the value 94 (the 3rd value in the array) ā€¢ That expression represents a place to store a single integer and can be used wherever an integer variable can be used
  • 7. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays (5 of 7) ā€¢ For example, an array element can be assigned a value, printed, or used in a calculation:
  • 8. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays (6 of 7) ā€¢ The values held in an array are called array elements ā€¢ An array stores multiple values of the same type - the element type ā€¢ The element type can be a primitive type or an object reference ā€¢ Therefore, we can create an array of integers, an array of characters, an array of String objects, an array of Coin objects, etc.
  • 9. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays (7 of 7) ā€¢ In Java, the array itself is an object that must be instantiated ā€¢ Another way to depict the scores array:
  • 10. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Declaring Arrays (1 of 2) ā€¢ The scores array could be declared as follows: ā€¢ The type of the variable scores is int[] (an array of integers) ā€¢ Note that the array type does not specify its size, but each object of that type has a specific size ā€¢ The reference variable scores is set to a new array object that can hold 10 integers
  • 11. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Declaring Arrays (2 of 2) ā€¢ Some other examples of array declarations:
  • 12. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Using Arrays ā€¢ The for-each version of the for loop can be used when processing array elements: ā€¢ This is only appropriate when processing all array elements starting at index 0 ā€¢ It canā€™t be used to set the array values ā€¢ See BasicArray.java
  • 13. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.1 (1 of 2)
  • 14. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.1 (2 of 2)
  • 15. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Basic Array Example
  • 16. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Quick Check 1 (1 of 2) Write an array declaration to represent the ages of 100 children. Write code that prints each value in an array of integers named values.
  • 17. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Quick Check 1 (2 of 2) Write an array declaration to represent the ages of 100 children. Write code that prints each value in an array of integers named values.
  • 18. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Bounds Checking (1 of 3) ā€¢ Once an array is created, it has a fixed size ā€¢ An index used in an array reference must specify a valid element ā€¢ That is, the index value must be in range 0 to Nāˆ’1 ā€¢ The Java interpreter throws an ArrayIndexOutOfBoundsException if an array index is out of bounds ā€¢ This is called automatic bounds checking
  • 19. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Bounds Checking (2 of 3) ā€¢ For example, if the array codes can hold 100 values, it can be indexed from 0 to 99 ā€¢ If the value of count is 100, then the following reference will cause an exception to be thrown: ā€¢ Itā€™s common to introduce off-by-one errors when using arrays:
  • 20. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Bounds Checking (3 of 3) ā€¢ Each array object has a public constant called length that stores the size of the array ā€¢ It is referenced using the array name: ā€¢ Note that length holds the number of elements, not the largest index ā€¢ See ReverseOrder.java ā€¢ See LetterCount.java
  • 21. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.2 (1 of 3)
  • 22. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.2 (2 of 3)
  • 23. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.2 (3 of 3)
  • 24. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.3 (1 of 4)
  • 25. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.3 (2 of 4)
  • 26. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.3 (3 of 4)
  • 27. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.3 (4 of 4)
  • 28. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Alternate Array Syntax ā€¢ The brackets of the array type can be associated with the element type or with the name of the array ā€¢ Therefore the following two declarations are equivalent: ā€¢ The first format generally is more readable and should be used
  • 29. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Initializer Lists (1 of 2) ā€¢ An initializer list can be used to instantiate and fill an array in one step ā€¢ The values are delimited by braces and separated by commas ā€¢ Examples:
  • 30. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Initializer Lists (2 of 2) ā€¢ Note that when an initializer list is used: ā€“ the new operator is not used ā€“ no size value is specified ā€¢ The size of the array is determined by the number of items in the list ā€¢ An initializer list can be used only in the array declaration ā€¢ See Primes.java
  • 31. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.4 (1 of 2)
  • 32. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.4 (2 of 2)
  • 33. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays as Parameters ā€¢ An entire array can be passed as a parameter to a method ā€¢ Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other ā€¢ Therefore, changing an array element within the method changes the original ā€¢ An individual array element can be passed to a method as well, in which case the type of the formal parameter is the same as the element type
  • 34. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Outline (2 of 7) ā€¢ Declaring and Using Arrays ā€¢ Arrays of Objects ā€¢ Variable Length Parameter Lists ā€¢ Two-Dimensional Arrays
  • 35. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays of Objects (1 of 7) ā€¢ The elements of an array can be object references ā€¢ The following declaration reserves space to store 5 references to String objects ā€¢ It does NOT create the String objects themselves ā€¢ Initially an array of objects holds null references ā€¢ Each object stored in an array must be instantiated separately
  • 36. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays of Objects (2 of 7) ā€¢ The words array when initially declared: ā€¢ At this point, the following line of code would throw a NullPointerException:
  • 37. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays of Objects (3 of 7) ā€¢ After some String objects are created and stored in the array:
  • 38. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays of Objects (4 of 7) ā€¢ Keep in mind that String objects can be created using literals ā€¢ The following declaration creates an array object called verbs and fills it with four String objects created using string literals
  • 39. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays of Objects (5 of 7) ā€¢ The following example creates an array of Grade objects, each with a string representation and a numeric lower bound ā€¢ The letter grades include plus and minus designations, so must be stored as strings instead of char ā€¢ See GradeRange.java ā€¢ See Grade.java
  • 40. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.5 (1 of 2)
  • 41. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.5 (2 of 2)
  • 42. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.6 (1 of 3)
  • 43. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.6 (2 of 3)
  • 44. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.6 (3 of 3)
  • 45. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays of Objects (6 of 7) ā€¢ Now letā€™s look at an example that manages a collection of DVD objects ā€¢ An initial capacity of 100 is created for the collection ā€¢ If more room is needed, a private method is used to create a larger array and transfer the current DVDs ā€¢ See Movies.java ā€¢ See DVDCollection.java ā€¢ See DVD.java
  • 46. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.7 (1 of 3)
  • 47. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.7 (2 of 3)
  • 48. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.7 (3 of 3)
  • 49. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.8 (1 of 4)
  • 50. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.8 (2 of 4)
  • 51. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.8 (3 of 4)
  • 52. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.8 (4 of 4)
  • 53. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.9 (1 of 2)
  • 54. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.9 (2 of 2)
  • 55. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Arrays of Objects (7 of 7) ā€¢ A UML diagram for the Movies program:
  • 56. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Command-Line Arguments ā€¢ The signature of the main method indicates that it takes an array of String objects as a parameter ā€¢ These values come from command-line arguments that are provided when the interpreter is invoked ā€¢ For example, the following invocation of the interpreter passes three String objects into the main method of the StateEval program: ā€¢ See NameTag.java
  • 57. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.10 (1 of 2)
  • 58. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.10 (2 of 2)
  • 59. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Outline (3 of 7) ā€¢ Declaring and Using Arrays ā€¢ Arrays of Objects ā€¢ Variable Length Parameter Lists ā€¢ Two-Dimensional Arrays
  • 60. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Variable Length Parameter Lists (1 of 7) ā€¢ Suppose we wanted to create a method that processed a different amount of data from one invocation to the next ā€¢ For example, letā€™s define a method called average that returns the average of a set of integer parameters
  • 61. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Variable Length Parameter Lists (2 of 7) ā€¢ We could define overloaded versions of the average method ā€“ Downside: weā€™d need a separate version of the method for each additional parameter ā€¢ We could define the method to accept an array of integers ā€“ Downside: weā€™d have to create the array and store the integers prior to calling the method each time ā€¢ Instead, Java provides a convenient way to create variable length parameter lists
  • 62. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Variable Length Parameter Lists (3 of 7) ā€¢ Using special syntax in the formal parameter list, we can define a method to accept any number of parameters of the same type ā€¢ For each call, the parameters are automatically put into an array for easy processing in the method
  • 63. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Variable Length Parameter Lists (4 of 7)
  • 64. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Variable Length Parameter Lists (5 of 7) ā€¢ The type of the parameter can be any primitive or object type:
  • 65. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Quick Check 2 (1 of 2) Write method called distance that accepts a variable number of integers (which each represent the distance of one leg of a trip) and returns the total distance of the trip.
  • 66. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Quick Check 2 (2 of 2) Write method called distance that accepts a variable number of integers (which each represent the distance of one leg of a trip) and returns the total distance of the trip.
  • 67. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Variable Length Parameter Lists (6 of 7) ā€¢ A method that accepts a variable number of parameters can also accept other parameters ā€¢ The following method accepts an int, a String object, and a variable number of double values into an array called nums
  • 68. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Variable Length Parameter Lists (7 of 7) ā€¢ The varying number of parameters must come last in the formal arguments ā€¢ A method cannot accept two sets of varying parameters ā€¢ Constructors can also be set up to accept a variable number of parameters ā€¢ See VariableParameters.java ā€¢ See Family.java
  • 69. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.11 (1 of 2)
  • 70. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.11 (2 of 2)
  • 71. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.12 (1 of 2)
  • 72. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.12 (2 of 2)
  • 73. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Outline (4 of 7) ā€¢ Declaring and Using Arrays ā€¢ Arrays of Objects ā€¢ Variable Length Parameter Lists ā€¢ Two-Dimensional Arrays
  • 74. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Two-Dimensional Arrays (1 of 3) ā€¢ A one-dimensional array stores a list of elements ā€¢ A two-dimensional array can be thought of as a table of elements, with rows and columns
  • 75. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Two-Dimensional Arrays (2 of 3) ā€¢ To be precise, in Java a two-dimensional array is an array of arrays ā€¢ A two-dimensional array is declared by specifying the size of each dimension separately: ā€¢ A array element is referenced using two index values: ā€¢ The array stored in one row can be specified using one index
  • 76. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Two-Dimensional Arrays (3 of 3) Expression Type Description table Computer code reads, i n t left bracket right bracket left bracket right bracket 2D array of integers, or array of integer arrays table[5] Computer code reads, i n t left bracket right bracket array of integers table[5][12] Computer code reads, i n t Integer int[][] int[] int ā€¢ See TwoDArray.java ā€¢ See SodaSurvey.java
  • 77. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.13 (1 of 2)
  • 78. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.13 (2 of 2)
  • 79. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.14 (1 of 3)
  • 80. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.14 (2 of 3)
  • 81. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Listing 8.14 (3 of 3)
  • 82. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Multidimensional Arrays ā€¢ An array can have many dimensions - if it has more than one dimension, it is called a multidimensional array ā€¢ Each dimension subdivides the previous one into the specified number of elements ā€¢ Each dimension has its own length constant ā€¢ Because each dimension is an array of array references, the arrays within one dimension can be of different lengths ā€“ these are sometimes called ragged arrays
  • 83. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Summary ā€¢ Chapter 8 has focused on: ā€“ array declaration and use ā€“ bounds checking and capacity ā€“ arrays that store object references ā€“ variable length parameter lists ā€“ multidimensional arrays
  • 84. Copyright Ā© 2018 Pearson Education, Inc. All Rights Reserved Copyright This work is protected by United States copyright laws and is provided solely for the use of instructors in teaching their courses and assessing student learning. Dissemination or sale of any part of this work (including on the World Wide Web) will destroy the integrity of the work and is not permitted. The work and materials from it should never be made available to students except by instructors using the accompanying text in their classes. All recipients of this work are expected to abide by these restrictions and to honor the intended pedagogical purposes and the needs of other instructors who rely on these materials.

Editor's Notes

  1. If this PowerPoint presentation contains mathematical equations, you may need to check that your computer has the following installed: 1) MathType Plugin 2) Math Player (free versions available) 3) NVDA Reader (free versions available)