Arrays - James Madison University

Download Report

Transcript Arrays - James Madison University

Introduction to Arrays in Java
Corresponds with Chapter 6 of
textbook
What is an Array?
Array is a data structure that represents a
collection of the same types of data.
Java treats these arrays as objects. This
means array variables are references, not
value variable.
Individual data items in arrays are called
elements.
Elements are stored consecutively
(contiguously) in memory.
Each element of an array is indexed.Indexing
begins at 0.
myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
double[] myList = new double[10];
myList
reference
Array reference
variable
Array element at
index 5
myList[0]
5.6
myList[1]
4.5
myList[2]
3.3
myList[3]
13.2
myList[4]
4
myList[5]
34.33
myList[6]
34
myList[7]
45.45
myList[8]
99.993
myList[9]
11123
Element value
Declaring Arrays

datatype[] arrayname;
Example:
int[] myList;

datatype arrayname[];
Example:
int myList[];
Either syntax will
work. The []
indicates that the
variable being
declared will be a
reference to an
array. Each
element of the
array will be a
data item of the
specified data
type.
NOTE: declaring the array does not create it! No memory is allocated
for individual array elements. This requires a separate creation step.
Creating Arrays
arrayName = new datatype[arraySize];
Java reserved
word for object
creation
Integer value
indicates the
number of
elements
Example:
myList = new double[10];
NOTE: the new keyword creates an object or array. The
object or array is created in a location of memory called the
heap. A reference (pointer) to the array is assigned (via =) to
the variable.
Declaring and Creating
in One Step

datatype[] arrayname = new
datatype[arraySize];
double[] myList = new double[10];

datatype arrayname[] = new
datatype[arraySize];
double myList[] = new double[10];
NOTE: when creating an array, the value inside the square brackets [ ]
indicates the size you want the array to be (i.e. the number of
elements).
The Length of an Array
Once an array is created, its size is fixed. It
cannot be changed. You can find its size using
arrayRefVar.length
For example,
myList.length returns 10
Initializing Arrays

Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = someValue;

Declaring, creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
NOTE: when using an array, the value inside the square brackets [ ]
indicates the index that you are looking at (i.e. the specific element).
Declaring, creating, initializing
Using the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the
following statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
Listing 6.1 – TestArray Class
1)
2)
3)
4)
Declaring the
reference variable
Creating the array
Assigning the
array’s memory
location to the
reference variable
NOTE:
TOTAL_NUMBERS
is a constant with
value 6
Note: new is an
operator that creates an
object (an array is an
example of an object).
How Variables are Kept in Memory

Two categories of variables



Value – contains actual data (integer, float, double,
boolean, or other primitive data type)
Reference – contains a reference (pointer) to an
object or array that is located on the heap (as a result
of using the new operator).
Two different locations of memory (more to
come later)



Frame Stack – contains local variables and
parameters of methods
Heap – contains objects and arrays
The new keyword always creates an object or array
and places it in the heap.
3) Assigning the array’s memory
location to the reference variable
1)
main’s
frame
Declaring the
reference
variable
2) Creating the array
on the heap
args
numbers
Frame Stack
0
1
2
3
Heap
4
5
Listing 6.1
– TestArray Class
In a loop, read user
input and insert
into the array.
Note use of length
property to decide
when to end loop
Looping through the
array
Assume the user enters the
numbers: 3, 2, 7, 5, 7, 1
length is a property
of array objects,
indicating the number
of elements
Note the use of the loop
counter to index into the
array
main’s
frame
args
numbers
i
3
2
0
1
7
2
0
1
2
3
4
6
5
Frame Stack
Heap
5
3
7
1
4
5
Listing 6.1
– TestArray Class
This loop is a useful
technique for finding the
largest value in an array.
Note the if statement
nested in the for loop.
Note the use of the loop
counter to index into the
array.
Looping through the
array
args
main’s
frame
numbers
i
max
3
2
0
1
7
2
1
2
6
5
4
3
37
3
Frame Stack
Heap
5
3
7
1
4
5
Listing 6.1
– TestArray Class
This use of a loop with an array is
similar to a linear search. More on
search routines later.
Listing 6.1
– TestArray Class
Note the concatenation of array
elements to a string, again,
repetitively in a loop.
Parallel Arrays
Parallel arrays should have
same length
Same index position  same entity
Program output:
Multidimensional Arrays
A multidimensional array is a data
structure that represents a collection
of the same types of data.
Java treats these arrays as objects.
This means array variables are
references, not value variable.
Individual data items in arrays are
called elements.
Elements are stored consecutively
(contiguously) in memory.
Can vary in size from two to n.
Element [0][0] Element [0][1]
Element [1][0] Element [1][1]
Element [2][0] Element [2][1]
Element [3][0] Element [3][1]
Declaring Multidimensional Arrays

datatype[][] arrayname; Either syntax will
work. The [][]
indicates that the
Example:
variable being
int[][] myList;
declared will be a
reference to an
 datatype arrayname[][]; array. Each
element of the
array will be a
Example:
data item of the
int myList[][];
specified data
type.
NOTE: declaring the array does not create it! No memory is allocated
for individual array elements. This requires a separate creation step.
Creating Multidimensional Arrays
arrayName = new datatype[rowSize][columnSize];
Java reserved
word for object
creation
Integer value
indicates the
number of rows
Integer value
indicates the
number of
columns
Example: myList = new double[10][4];
NOTE: the new keyword creates an object or array. The
object or array is created in a location of memory called the
heap. A reference (pointer) to the array is assigned (via =) to
the variable.
Declaring and Creating
in One Step

datatype[][] arrayname = new
datatype[rowSize][columnsize];
double[][] myList = new double[10][3];

datatype arrayname[][] = new
datatype[rowSize][columnSize];
double myList[][] = new double[10][3];
NOTE: when creating a multidimensional array, the value inside the
square brackets [ ] [ ] indicates the number of rows and columns. The
total size of the array is the rows multiplied by the columns (10 * 3 = 30
elements for the example above).
Initializing 2-Dimensional Arrays
for (int r =0; r<row; r++) {
for (int c = 0; c < column; c++){
myList[r][c]=someValue;
}
Use a nested loop to access all elements
}
of a 2-D array, loop counters represent
row and column indexes to the array.
double[][] myList = {
{1.9, 2.9},
A 2-D array is an array of arrays
{3.4, 3.5}
};
NOTE: when using an array, the values inside the square brackets [] []
indicate the indexes that you are looking at (i.e. the specific element).
Obtaining Lengths of
Multidimensional Arrays
for (int r=0; r<myList.length; r++){
for (int c = 0; c<myList[r].length; c++) {
print(myList[r][c]);
}
}
NOTE: The length determines the number of elements
myList.length determines the number of rows
myList[r].length determines the number of elements for a given row
Ragged Arrays
When rows in a 2-d array can vary in size
Int [] [] myList = {
{1, 2, 3},
{1, 2},
{1}
};
Listing 6.12 – TotalScore Class
1)
2)
3)
Declaring the
reference variable
Creating the 3-d array
d1 = student (7)
d2 = exam (5)
d3 = exam part (2)
Assigning the array’s
memory location to
the reference variable
How Variables are Kept in Memory

Two categories of variables



Value – contains actual data (integer, float, double,
boolean, or other primitive data type)
Reference – contains a reference (pointer) to an
object or array that is located on the heap (as a result
of using the new operator).
Two different locations of memory (more to
come later)



Frame Stack – contains local variables and
parameters of methods
Heap – contains objects and arrays
The new keyword always creates an object or array
and places it in the heap.
3) Assigning the array’s memory
location to the reference variable
1)
Declaring the
reference
variable
Note: 7 rows, 5 columns with 2
parts each for a total of 70
elements – only 4 are shown
[0,0,0] [0,0,1] [0,1,0] [0,1,1]
main’s
frame
args
scores
Frame Stack
7.5
20.5
…
Heap
12
22.5
…
Listing 6.12 – TotalScore Class
In a loop, total all of the
exam parts together.
Display the total for each
student.
Note:
3-D array 
loop nested 3-deep.
Program output:
Contents of
memory at
breakpoint
Note: a 2dimensional array
is implemented as
an array of
arrays. Each
element of the
first dimension is
a reference to a
single
dimensional array.