+ All Categories
Home > Documents > ©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005...

©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005...

Date post: 20-Dec-2015
Category:
View: 215 times
Download: 1 times
Share this document with a friend
32
©2004 Brooks/Cole Chapter 8 Arrays
Transcript

©2004 Brooks/Cole

Chapter 8

Arrays

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Sometimes we have lists of data values that all need to be treated similarly

Three Lists of Items

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Creating Arrays

• An array is an indexed collection of data values of the same type.

• Arrays are reference types (like objects)

• Three steps to creating an array– Declaration creates a reference variable

– Use new to allocate memory

– Assign values to individual elements

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Arrays are like Objects

• Declaring an array allocates memory for a reference

– The declaration does NOT allocate memory for the array data

• The result of the declaration double prices[];

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Allocating Memory for Array

• The Results of the Allocationprice = new double[6];– All elements are initialized to 0

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Example: A List of Prices

• Declarationdouble[] prices;

• Memory Allocationprices = new double[6]

• Assigning valuesprices[0] = 10.96;prices[1] = 6.43;prices[2] = 2.58;prices[3] = 0.58;prices[4] = 12.27;prices[5] = 6.39;

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

More Array Allocations

int [] grade = new int[5];

char [] code = new char[4];

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Array Allocation

• You don't have to use a literal value for the array size– You can use any integer expression

• Examplesint [] grade = new int[NUMELS];

int [] grade = new int[ size * 2];

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Array Size

• Once memory is allocated, the array has a fixed size

• Every array has a variable named length associated with it– length contains the number of elements for

which memory was allocated– get the length using

arrayName.length

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Access to Array Elements

• Use [ ] to access individual elements of an array– Index can be any integer expression– Individual elements can be used anywhere that

a value of the base type can be used– Indexes go from 0 to one less that the length of

the array

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Element Access Example

• grade[3]

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Practice

• Declare and allocate memory for the following– a list of 100 integer years– a list of 6 character codes– a list of 32 floating point velocities

• Access the 1st, 3rd and 7th elements of the arrays above

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

More Practice

• What elements are output by the followingfor (m=1; m<=5; m++)

System.out.println( a[m] + " ");

for (int k=1; k< 5; k=k+2)

System.out.println( a[k] + " ");

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Array Initializers

• You can initialize the elements of an array when you declare itint [] grade = {98, 87, 92, 79, 85};

char[] code

= {'s', 'a', 'm', 'p', 'l', 'e'};

• The size of the array is determined by the number of elements in the initializer list.– grade.length is 5 elements, code.length is 6

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

List of Prices with Initializer

• Declaration with initializerdouble[] prices = {10.96, 6.43, 2.58, 0.86, 12.27, 6.39};

• The array has 6 elements– prices.length will be 6

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

More Practice

• Write array declarations with initialization for the following– integer grades 89, 75, 82, 93, 78, 95, 81– floating point temperatures 78.2, 69.6, 68.5,

83.9, 55.4– character menu choices p, a, c, w, m, q

• What is the length of each of the arrays?

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Using Arrays

• Once you have created an array, you can access the elements in any order

• Use loops to process all elements of the array in the same way– input values for each element– modify each element in the same way– print each element out

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Arrays of Objects

• Declaration works the same as for arrays of primitive values

• Memory is allocated to store a reference to the array dataString [] names; String names[];

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Allocating Memory for an Array of Strings

• Instantiation creates an array of referencesnames = new

String[4]• Each element of the array

has to be instantiated individuallynames[0] = "Joe";

names[1] = new String("Harriet");

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The Assignment of Values Creates Actual Array Objects

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Using arrays with methods

• Arrays can be used as parameters for methods– location of the array is copied into the method

parameter variable– the contents of the array can be modified by the

method

• An array can be returned by a method– the location of the array is returned

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Arrays as Method Parameters

• Only one array exists– method gets a copy of the location

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Arrays as method argumentsThe Location of the Array is Passed

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Practice

• Write the header for a method called sort that takes an array of double values as a parameter and returns nothing (modifies the existing array)

• Write the header for a method called average that takes an array of doubles as a parameter and returns a double

• Write the header for a method called readArray that takes no parameters and returns an array of integers

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

2D Arrays

• What if we need to make a table instead of a list?– We can make arrays with two indexes

• Think of 2D array as an array whose elements are arrays

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Creating 2D Arrays

• Declaration

int [][] table • Instantiation

table = new int[3][4];• Declaration with initialization

int[][]table2

= {{8, 16, 9, 52},

{3, 15, 27, 6},

{14, 25, 2, 10}};

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Each Array Element Is Identified by Its Row and Column Position

• Access row 1 with table[1]• Access element 3 of row 2 with table[2][3]

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Practice

• Write declaration and allocation statements for– an array of integers with 6 rows and 10

columns– an array of doubles with 10 rows and 25

columns

• Write loops needed to add two 2x3 element arrays on integers and put the results in a new array.

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Representation of aThree-Dimensional Array

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Higher Dimensional arrays are also possible

• Need one index for each dimension

• For n dimensions, need n nested loops to process all elements

• Higher dimensional arrays are harder to visualize

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Deep Copy

• After the Call to System.arraycopy()newnums = System.arraycopy(nums)– This method creates a new array and copies the

values from the original

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The Result of a Shallow Copy

newnums = nums


Recommended