Arrays - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Arrays

Description:

Explore how to declare and manipulate data into arrays ... Can specify different number of columns for each row (ragged arrays) int twoD[ ][ ] = new int[3] ... – PowerPoint PPT presentation

Number of Views:123
Avg rating:3.0/5.0
Slides: 37
Provided by: manasi48
Category:
Tags: arrays | ragged

less

Transcript and Presenter's Notes

Title: Arrays


1
Chapter 9
  • Arrays

2
Chapter Objectives
  • Learn about arrays
  • Explore how to declare and manipulate data into
    arrays
  • Understand the meaning of array index out of
    bounds
  • Become familiar with the restrictions on array
    processing

3
Chapter Objectives
  • Discover how to pass an array as a parameter to a
    method
  • Discover how to manipulate data in a
    two-dimensional array
  • Learn about multidimensional arrays

4
Array
  • Definition structured (or aggregate) data type
    with a fixed number of components
  • Every component is of the same type
  • Components are accessed using their relative
    positions in the array

5
One-Dimensional Arrays
  • is the array subscripting operator
  • Syntax to instantiate an array
  • dataType arrayName
  • arrayName new dataTypeintExp
  • dataType arrayName new dataTypeintExp
  • dataType arrayName1, arrayName2
  • dataType arrayName1 , variableName1
  • Syntax to access an array component
  • arrayNameindexExp
  • intExp number of components in array gt 0
  • 0 lt indexExp lt intExp

6
Initializing Arrays
  • int list 10, 20, 30, 40, 50
  • Creates the integer array list with length 5 and
    initializes the components to the given values

7
Array numint num new int5
8
Array list
9
Arrays
  • Not necessary to know array size at compile time
  • arrayName.length returns the number of components
    in array
  • Loops used to step through elements in array and
    perform operations

10
Arrays
  • Some operations on arrays
  • Initialize
  • Input data
  • Output stored data
  • Find largest/smallest/sum/average of elements

11
How To Specify Array Size During Program Execution
int arraySize //Line 1 System.out.print("En
ter the size of the array ") //Line
2 arraySize Integer.parseInt(keyboard.readLine(
)) //Line 3 System.out.println() //Line
4 int list new intarraySize //Line 5
12
Instance Variable length
  • Contains size of array
  • public member
  • Can be directly accessed in program using array
    name and dot operator
  • Example
  • If int list 10, 20, 30, 40, 50, 60
  • Then list.length is 6

13
Code to Initialize Array to Specific Value (10.00)
for(index 0 index lt sale.length index)
saleindex 10.00
14
Code to Read Data into Array
for(index 0 index lt sale.length index)
saleindex Integer.parseInt(keyboard.readLine()
)
15
Code to Print Array
for(index 0 index lt sale.length index)
System.out.print(saleindex " ")
16
Code to Find Sum and Average of Array
sum 0 for(index 0 index lt sale.length
index) sum sum saleindex if(sale.l
ength ! 0) average sum /
sale.length else average 0.0
17
Determining Largest Element in Array
maxIndex 0 for(index 1 index lt sale.length
index) if(salemaxIndex lt saleindex)
maxIndex index largestSale
salemaxIndex
18
Determining Largest Element in Array
19
Array Index Out of Bounds
  • Array in bounds if
  • 0 lt index lt arraySize 1
  • If index lt 0 or index gt arraySize
  • ArrayIndexOutOfBoundsException exception is
    thrown
  • Base address memory location of first component
    in array

20
The Assignment Operator, the Relational Operator,
and Arrays
21
The Assignment Operator, the Relational Operator,
and Arrays
22
Methods for Array Processing
23
Methods for Array Processing
24
Methods for Array Processing
25
Parallel Arrays
  • Arrays are parallel if corresponding components
    hold related information

26
Arrays of Objects
  • Can use arrays to manipulate objects
  • Example create array named array1 with N objects
    of type T
  • T array1 new TN
  • Can instantiate elements of array1 as follows
  • for(int j0 j ltarray1.length j)
  • array1j new T()

27
Arrays of ObjectsClock arrivalTimeEmp new
Clock 100
28
Instantiating Array Objects
29
Two-Dimensional Arrays
  • Data is sometimes in table form (difficult to
    represent using one-dimensional array). Think in
    terms of an 1-D array of 1-D arrays.
  • To declare/instantiate two-dimensional array
  • dataType arrayName new
    dataTypeintExp1intExp2
  • To access a component of a 2-dimensional array
  • arrayNameindexExp1indexExp2
  • intExp1, intExp2 gt 0
  • indexExp1 row position
  • indexExp2 column position

30
Two-Dimensional Arrays
  • Can specify different number of columns for each
    row (ragged arrays)
  • int twoD new int3
  • twoD0 new int5
  • twoD1 new int3
  • twoD2 new int7
  • twoD.length gives the number of rows
  • twoDi.length gives the number of elements in
    row I (number of columns)

31
Two-Dimensional Arrays
  • Initialization at Declaration
  • int mine 2, 3 , 15, 25, 13,
    20, 4, 7, 8
  • Three ways to process 2-D arrays
  • Entire array
  • Particular row of array (row processing)
  • Particular column of array (column processing)
  • Processing algorithms similar to processing
    algorithms of one-dimensional arrays

32
Two-Dimensional Arrays Special Cases
33
doublesales new double105
Two-Dimensional Arrays
34
Accessing Two-Dimensional Array Components
35
Multidimensional Arrays
  • Can define three-dimensional arrays or
    n-dimensional array (n can be any number)
  • Syntax to declare and instantiate array
  • dataType arrayName new
    dataTypeintExp1intExp2intExpn
  • Syntax to access component
  • arrayNameindexExp1indexExp2indexExpn
  • intExp1, intExp2, ..., intExpn positive
    integers
  • indexExp1,indexExp2, ..., indexExpn
    non-negative integers

36
Loops to Process Multidimensional Arrays
double carDealers new double 1057
for(i 0 i lt 10 i) for(j 0 j lt 5
j) for(k 0 k lt 7 k)
carDealersijk 10.00
Write a Comment
User Comments (0)
About PowerShow.com