Chapter 8: One Dimensional Arrays



Chapter 8: One Dimensional Arrays

scalar variable is a single built-in data type value

ex. char name;

int i;

double money;

A single dimensioned array can be used for a set of data values all of the same type. It’s possibly a column of information. The list of related values are all stored in a single group name in contiguous memory. All values are automatically initialized to zero (false or null for reference types).

Prices

5.99

13.67

9.40

3.52

11.83

double prices[];

prices = new double[5];

or

double prices[] = new double[5];

or

final int MAX=5;

double prices[]=new double[MAX];

element (or component) each item in an array

index (or subscript) an element’s position, starting at index 0.

prices[0] ~ read as “prices sub zero”

Loops are used to process arrays. There is a special FOR LOOP that can be used called the for/each loop.

The for-each loop is used with both collections and arrays. It is intended to simplify the most common form of iteration, where the iterator or index is used solely for iteration, and not for any other kind of operation, such as removing or editing an item in the collection or array. When there is a choice, the for-each loop should be preferred over the for loop, since it increases legibility

// Method returns the sum of the elements of a

int sum(int[] a) {

int result = 0;

for (int i : a) //read as for each element (i) in a

result += i;

return result;

}

//typical for-each loop

//processes each item, without changing the collection or array.

String[] names = {"Ethan Hawke", "Julie Delpy"};

for( String name : names ){

System.out.println("Name : " + name);

Old loop:

sum=0;

for (i=0; i< prices.length;i++)

sum=sum + prices[i];

New loop:

sum=0;

for (int price : prices)

sum +=price;

Use loops to input /output values of an array.

Java bounds check Java checks the value of the index being used at run time. (ie. if array declared as 5 and use an index of 6 – notified of ArrayIndexOutofBounds).

Array object – aggregate type

- also referred to as a structured type and a data

structure.

- also operations must be available for retrieving

and updating individual values

i.e. array of integers, related by their position, index provides a method of accessing and modifying.

Example Programs. p 407 & 408

String Arrays

String names[]=new String[7];

or

String names[];

names= new String[7];

Arrays of reference types are stored differently than an array of built-in types.

Declaration is the same.

Allocation is different.

When a string value is assigned, storage for that string is created and that address is stored in the array. Each string value may vary in length.

When using built-in arrays, the actual values are stored directly in the array.

Nice Picture p 411

Run-time Dimensioning

s1 = JOptionPane.showInputDialog(“Enter Grade”);

num = Integer.parseInt(s1);

int grade[] = new int[num];

Array Initialization

int grade[] = {98,78,100,95};

//array is sized according to the number of values in the //initialization

Deep and Shallow Copies

System.arraycopy() method

System.arraycopy(source_array, source_start_index,

target_array, target_start_index,

number_of_elements_to_copy);

i.e.

System.arraycopy(nums,1,other,2,3);

System.arraycopy(nums,0,other,0,nums.length);

Target array must be allocated large enough or error will occur…ArrayIndexOutOfBounds

Look at p419

Deep Copy – element by element copy

Shallow copy – when an array assignment is executed

ie. other = nums;

This assignment statement copies the address stored in nums into the reference variable other. nums and other refer to the same array.(other is just an alias)

Shallow copy used in redimensioning p423 #10.

Arrays as Arguments

- individual elements (passed by value)

findMax(grades[2],price[6]);

- complete array (passed by reference)

{

double units[]=new double[500];

calcTotal(units);

…}

//in the method

void calcTotal(double array[])

{

//units and array are the same array

}

Examples p428 Pgm 8.10

P429 Pgm 8.11

Assignment: p 431 #6 (Parallel Arrays: Price, Qty, Amount)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches