Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Arrays

Reference: Java Programming Fifth Edition by Joyce Farrell

Objectives

Declare and initialize an array


Use subscripts with an array
Manipulate arrays
Use two-dimensional and multidimensional arrays

Declaring and Initializing an Array


Array
Named list of data items
All have same type

Declare array variable


Same way as declaring any simple variable
Insert pair of square brackets after type

Declaring and Initializing an Array


(continued)
double[] salesFigure;
int[] idNum;
Still need to reserve memory space
salesFigure = new double[20];
double[] salesFigure = new double[20];

Subscript
Integer contained within square brackets
Indicates one of arrays variables or elements

Declaring and Initializing an Array


(continued)
Arrays elements numbered beginning with zero
Can legally use any subscript from 0 through 19
When working with array that has 20 elements

Work with any individual array element


Treat no differently than single variable of same type
Example: salesFigure[0] = 2100.00;

Declaring and Initializing an Array


(continued)

Initializing an Array
Variable with reference type
Such as array
Holds memory address where value stored

Array names
Represent computer memory addresses
Contain references

Declare array name


No computer memory address assigned
Has special value null
Unicode value \u0000
7

Initializing an Array (continued)


Use keyword new to define array
Array name acquires actual memory address value

int[] someNums = new int[10];


Each element of someNums has value of 0

char array elements


Assigned \u0000

boolean array elements


Automatically assigned value false

Initializing an Array (continued)


Assign nondefault values to array elements upon
creation
int[] tenMult = {10, 20, 30, 40, 50,
60};

Using Subscripts with an Array


Power of arrays
Use subscripts that are variables
Rather than constant subscripts
Use a loop to perform array operations
for (sub = 0; sub < 5; ++sub)
scoreArray[sub] += 3;

10

Using Subscripts with an Array


(continued)
Application contains array
Use every element of array in some task
Perform loops that vary loop control variable
Start at 0
End at one less than size of array

Convenient to declare symbolic constant equal to


size of array
final int NUMBER_OF_SCORES = 5;

11

Using Subscripts with an Array


(continued)
length field
Contains number of elements in array
for(sub = 0; sub < scoreArray.length;
sub++)
scoreArray[sub] += 3;

12

Using Subscripts with an Array


(continued)
Enhanced for loop
Cycle through array
Without specifying starting and ending points for
loop control variable
for(int val : scoreArray)
System.out.println(val);

13

Searching an Array For


an Exact Match
Determine whether variable holds one of many
valid values
Use series of if statements
Compare variable to series of valid values

14

Searching an Array For


an Exact Match (continued)
Searching array
Compare variable to list of values in array
for(int x = 0; x < validValues.length;
x++)
{
if(itemOrdered == validValues[x])
validItem = true;

15

16

Manipulating Arrays of Strings


Create array of Strings
String[] deptName = {"Accounting",
"Human Resources", "Sales"};
for(int a = 0; a < deptName.length;
a++)
System.out.println(deptName[a]);

17

Manipulating Arrays of Strings


(continued)

18

Using Two-Dimensional and


Multidimensional Arrays
One-dimensional or single-dimensional array
Picture as column of values
Elements accessed using single subscript

Two-dimensional arrays

Two or more columns of values


Rows and columns
Use two subscripts
Matrix or table
int[][] someNumbers = new int[3][4];

19

Using Two-Dimensional and


Multidimensional Arrays (continued)

20

Using Two-Dimensional and


Multidimensional Arrays (continued)
int[][] rents = { {400, 450, 510},
{500, 560, 630},
{625, 676, 740},
{1000, 1250, 1600} };

21

Using the length Field with a TwoDimensional Array


length field holds the number of rows in the array
rents.length

Each row has a length field that holds the


number of columns in the row
rents[1].length

22

Understanding Ragged Arrays


Two-dimensional array with rows of different length
Define the number of rows for a two-dimensional
array
But not defining the number of columns in the rows

Then declare the individual rows

23

Using Multidimensional Arrays


Multidimensional arrays
More than two dimensions

Create arrays of any size


Keep track of order of variables needed as
subscripts
Dont exhaust computers memory

24

You Do It
Creating and populating an array
Initializing an array
Using a for loop to access array elements

25

Dont Do It
Dont forget that the lowest array subscript is 0
Dont forget that the highest array subscript is one
less than the length
Dont forget that length is an array property and not
a method
Dont forget that array names are references
Dont forget that the first subscript used with a twodimensional array represents the row, and that the
second subscript represents the column
26

Summary
Array
Named list of data items
All have same type

Array names
Represent computer memory addresses

Shorten many array-based tasks


Use variable as subscript

length field
Contains number of elements in array

Two-dimensional arrays
Both rows and columns

27

You might also like