Chapter 9
This presentation is the property of its rightful owner.
Sponsored Links
1 / 28

Chapter 9 (part of) Single-dimensional Arrays PowerPoint PPT Presentation


  • 37 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 9 (part of) Single-dimensional Arrays. Knowledge Goals. Understand the difference between atomic and composite data types Understand the difference between unstructured and structured composite data types Know how Java implements arrays

Download Presentation

Chapter 9 (part of) Single-dimensional Arrays

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 9 part of single dimensional arrays

Chapter 9 (part of)

Single-dimensional Arrays


Knowledge goals

Knowledge Goals

  • Understand the difference between atomic and composite data types

  • Understand the difference between unstructured and structured composite data types

  • Know how Java implements arrays

  • Know how an array is passed as an argument


Skill goals

Skill Goals

  • Declare and instantiate a one-dimensional array

  • Access and manipulate the individual components in a one-dimensional array where the elements are

    • Atomic types

    • Composite types

  • Use an initializer list to instantiate a one-dimensional array


Java data types

Java Data Types


Java data types1

Java Data Types

Try expressing these definitions in words


Java data types2

Java Data Types

Composite data type

A data type that allows a collection of values to be associated with an identifier of that type

Unstructured data type

A collection of components that are not organized with respect to one another

Structured data type

An organized collection of components; the organization determines the means used to access individual components

Is a class structured?


Java data types3

Java Data Types

class Example

{

double field3;

int field2;

int field1;

}

class Example

{

int field1;

int field2;

double field3;

}

Changing the order does not change the access

Is a class structured?

Did you change your answer?


One dimensional arrays

One-Dimensional Arrays

Data structure

The implementation of a composite data type

Note the difference between a data structure (implementation of any composite type) and a structured data type (a composite type that is structured)


One dimensional arrays1

One-Dimensional Arrays

One-dimensional array

A structured collection of components, all of the same type, that is given a single name; each component is accessed by an index that indicates the component's position within the collection

Class

  • composite, unstructured

  • heterogeneous

  • access by field name

  • Array

    • composite, structured

    • homogeneous

    • access by position


One dimensional arrays2

One-Dimensional Arrays

Declare

Instantiate


One dimensional arrays3

One-Dimensional Arrays

int[] numbers = new int[4];

What

type of

values

can be

stored in

each cell

?


One dimensional arrays4

One-Dimensional Arrays

float realNumbers[] = new float[10];

How

do you

get

values

into the

cells

?


One dimensional arrays5

One-Dimensional Arrays

Array Initializers

int numbers[] = {4.93, -15.2, 0.5, 1.67};

Initializers

do the

instantiation

and

storing in

with the

declaration


One dimensional arrays6

One-Dimensional Arrays

Accessing Individual Components

Indexing expression


One dimensional arrays7

One-Dimensional Arrays

Indexing

Expression

Indexing

Expression

Place into which

a value is stored;

value is changed

Place from which

a value is extracted;

value is not changed


One dimensional arrays8

One-Dimensional Arrays

What

happens

if you

try to

access

value[1000]

?


One dimensional arrays9

One-Dimensional Arrays

Out-of-bounds array index

An index that is either less than 0 or greater than the array size minus 1, causing an ArrayIndexoutOfBoundsException to be thrown

Length

A public instance variable associated with each instantiated array, accessed by array name .length

Use length to avoid out-of-bounds indexes


One dimensional arrays10

One-Dimensional Arrays

Aggregate Array Operations

What does the following expression return?

numbers == values


One dimensional arrays11

One-Dimensional Arrays

Now, what does the following expression return?

numbers == values


One dimensional arrays12

One-Dimensional Arrays

System provides two useful array methods

first = second.clone();// duplicates second import java.util.Arrays;

Arrays.equals(first, second); // item-by-item check

System.out.println(first == second);

System.out.println(Arrays.equals(first, second);

What is printed?


More examples

More Examples

What does this code segment do?

totalOccupants = 0;

for (int aptNo = 0; aptNo < occupants.length; aptNo++)

totalOccupants = totalOccupants +occupants[aptNo];


More examples1

More Examples

What does

this code

fragment

do

?

if ((letter >= 'A' && letter <= 'Z' ||

letter >= 'a' && letter <= 'z'))

{

index = (int)Character.toUpperCase(letter) - (int)'A';

lettrCount[index] = letterCount[index] + 1;

}


Arrays of objects

Arrays of Objects

String groceryItems[] = new String[10];

for (index = 0; index < grocerItems.length; index++)

{

groceryItems[index] = inFile.nextLine();

}


Arrays of objects1

Arrays of Objects

ExpressionClass/Type

groceryItemsReference to an array

groceryItems[0]Reference to a string

groceryItems[0].charAt()A character

groceryItems[10]Error

Base address

The memory address of the first element of the array

"Reference to" is the base address


Arrays of objects2

Arrays of Objects

How do Array.equals and clone operate with arrays of objects?

Array.equals uses the == operator to compare arrays, so the addresses are compared

clone uses the = operator, so the addresses are copied

Array.equals(numbers, numbers.clone()) ?


Arrays of objects3

Arrays of Objects

Date bigEvents[] = new Date[10];

bigEvents

Expression Class/Type

bigEvents Array

bigEvents[0] Date

bigEvents[0].month String

bigEvents[0].day int

bigEvents[0].year int

bigEvents[0].month.charAt(0) char

Date

[ 0 ]

[ 1 ]

.

.

.

[ 9 ]

.

.

.

Date

.

.

.

Date

.

.

.


Arrays of objects4

Arrays of Objects

length is the number of slots assigned to the array

What if the array doesn’t have valid data in each of these slots?

Keep a counter of how many slots have valid data and use this counter when processing the array

More about this type of

processing in Chapter 11


Arrays of objects5

Arrays of Objects

public static double average(int grades[])

// Calculates and returns the average grade in an

// array of grades.

// Assumption: All array slots have valid data.

{

int total = 0;

for (int i = 0; i < grades.length; i++)

total = total + grades[i];

return (double) total / (double) grades.length;

}

What is passed as an argument?


  • Login