Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays...

22
Chapter 8: Collections: Arrays

Transcript of Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays...

Chapter 8:

Collections: Arrays

2

Objectives

• One-Dimensional Arrays

• Array Initialization

• The Arrays Class: Searching and Sorting

• Arrays as Arguments

• The Collections Framework: ArrayLists

• Two-Dimensional Arrays

• Common Programming Errors

3

One-Dimensional Arrays

• Lists of related values with the same data type

– Stored using a single group name

• Array declaration example:

double prices[];

prices = new double[6];

4

One-Dimensional Arrays (continued)

• Using the new operator:

– Array elements are automatically initialized to:

• Zero for numerical built-in types

• False for Boolean built-in types

• Null for reference types

5

6

One-Dimensional Arrays (continued)

• Common programming practice:– Define the number of array items as a symbolic

constant

• Element– Item in an array

• Index– Position of an item in an array

– Also called a subscript

7

Accessing Elements in One-Dimensional Arrays

• grade[0]:

– Refers to the first value stored in grade array

– Read as “grade sub zero”

– Can be used anywhere that scalar variables are valid

• The subscript contained within brackets need not be an integer constant

– Any expression that evaluates to an integer may be used

8

Accessing Elements in One-Dimensional Arrays

(continued)

• An important advantage of using integer expressions as subscripts:– Allows sequencing through an array using a loop

• Example of looping through an array:sum = 0; // initialize the sum to zerofor (i = 0; i < NUMELS; i++)

sum = sum + grade[i]; // add in a grade– i is used both as a counter in the for loop and as a

subscript

9

Accessing Elements in One-Dimensional Arrays

(continued)

• When accessing an array element:

– Java checks the value of the index being used at run time

– If the index exceeds the length of the array, Java will notify you of an ArrayIndexOutOfBounds exception

10

11

One-Dimensional Array Length

• The size of an array is automatically stored in a variable named length

• Looping using for loop and array length:

sum = 0; // initialize the sum to zero

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

sum = sum + grade[i]; // add in a grade

12

Input and Output of Array Values

• Input:– Individual array elements can be assigned values

interactively using:

• readLine()

• showInputDialog()

• Output:– Array elements can be printed using:

• print()

• println()

13

Aggregate Data Types

• Any type whose:

– Individual elements are other data types

– Elements are related by some defined structure

• Also called:

– Structured type

– Data structure

• Arrays are aggregate data types

14

String Arrays

• Arrays of reference data types may also be constructed

• Declaring String array:

– String names[] = new String[4];

• Arrays of reference types are stored differently from arrays of built-in data types

15

16

17

18

Run-Time Dimensioning

• The size of an array can also be entered interactively at run time

• An entered value can be used to allocate space for an array using the new operator

19

Array Initialization

• Arrays can be initialized within declaration statements:– May continue across multiple lines

– No method of indicating repetition of initialization value

– No way to initialize later array elements without first specifying values for earlier elements

• Example:– int grade[] = {98, 87, 92, 79, 85};

20

import javax.swing.*;public class ElementInputAndDisplay{ public static void main(String[] args) { final int NUMELS= 5; String s1; int i; int grade[]; // declare the array grade = new int[NUMELS]; // allocate the array for (i = 0; i < NUMELS; i++) // enter the grades { s1 = JOptionPane.showInputDialog("Enter a grade: "); grade[i] = Integer.parseInt(s1); } for (i = 0; i < NUMELS; i++) // print the grades System.out.println("grade[" +i +"] is " + grade[i]); System.exit(0); }}

21

import javax.swing.*;public class AccumulateElements{ public static void main(String[] args) { final int NUMELS= 5; String s1; int i; int total = 0; // declare and allocate the array int grade[] = new int[NUMELS]; // allocate the array for (i = 0; i < NUMELS; i++) // enter the grades { s1 = JOptionPane.showInputDialog("Enter a grade: "); grade[i] = Integer.parseInt(s1); } System.out.print("The total of the grades"); for (i = 0; i < NUMELS; i++) // display and total the grades { System.out.print(" " + grade[i]); total = total + grade[i]; } System.out.print(" is " + total); System.exit(0); }}

22

public class StringArray{ public static void main(String[] args) { int i; String names[]; // declare the array names = new String[4]; // allocate the array // assign values to each array element names[0] = "Joe"; names[1] = "Harriet"; names[2] = "Allyn"; names[3] = "Roberta"; // display the names for (i = 0; i < names.length; i++) System.out.println("names[" + i + "] is " + names[i]); }}