Arrays of Primitive Values, Schemes and Mind Maps of Computer Science

Each data value stored in an array is called an element. • Each element is accessed using an integer index or subscript. • As with strings, the ...

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 03/01/2023

anamika
anamika 🇺🇸

4.7

(16)

254 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays of Primitive Values
15-110 Summer 2010
Margaret Reid-Miller
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Arrays of Primitive Values and more Schemes and Mind Maps Computer Science in PDF only on Docsity!

Arrays of Primitive Values

15-110 Summer 2010

Margaret Reid-Miller

Arrays

  • Arrays are objects that hold multiple values of the

same type.

  • Each data value stored in an array is called an

element.

  • Each element is accessed using an integer index or

subscript.

  • As with strings, the first subscript is 0.
  • Organizing data in an array allows programs to

access huge amount of data multiple times and in

any order!

Creating an Array

int[] counts = new int[23];!* counts[0] = 12;! counts[1] = 22;! counts[2] = 17;! counts[3] = 16;! counts[4] = 4;! counts[5] = 11;

[0] [1] [2] [3] [4] [5]

counts^12 22 17 16 4

bacteria colony count on

6 Petri dishes

no length length can be an int expression reference to the array

int sum = 0;! for (int i = 0; i < counts.length; i++) {! sum = sum + counts[i];! }! if (counts.length > 0){! System.out.print(“The average number of “ + !!! “bacteria colonies is “);! System.out.println(________________________);! }

Array Traversal

Constant : number of elements in the array Not a method: There is no () !!

Array Index

  • The index of an array must be a literal, variable, or

expression of type int.

E.g.,

x[0] = 3; assign the first element 3;

x[num] = num; use an int variable as index

x[j-3] = x[j]; use an int expression as index

x[x[0]] = max; use an int array element as index

Bounds Checking

[0] [1] [2] [3] [4] [5]

index > 5 is out of bounds

  • Out-of-bounds errors occur when you attempt to

access an array element that does not exist.

  • If you write x[index] the compiler cannot

determine if index is out of bounds.

  • When your run the program, however, Java checks

whether the index is out of bounds.

index < 0 is out of bounds

Initializer List

  • You can declare, create, and initialize an array with a

list of literals.

! int[] counts = {12, 22, 17, 16, 4, 11};!

  • You can use an initializer list only when the array is

first declared.

  • Each value must match the type of the array.
  • The values go into the array in the order given and

determine the length of the array.

Example

Find the minimum value stored in an array:

int min = _________ ;! for (int i = __; i < counts.length; i++) {! if (________________) {! min = counts[i];! }! }!

  • What happens if there are two or more values in the

array that are the minimum?

  • How do we modify the code to return the index of the

minimum bacteria count?!

counts[0] 1!

(counts[i] < min)!

! int[] sequence = new int[10];! ! fillAllSums(sequence);! !! }!!!!!!!! !! !!!!!!!! !! public static void fillAllSums(int[] seq) {! ! seq[0] = 0;! ! for (int i = 1; i < seq.length; i++) {! !! seq[i] = seq[i-1] + i;! ! }! }! sequence!

Arrays as Parameters

seq! 0 1 3 6 10 15 21 28 36 45 ae2f ae2f ae2f

! int[] sequence = new int[10];! ! fillAllSums(sequence);! !! }! ! public static void fillAllSums(int[] seq) {! ! seq[0] = 0;! ! for (int i = 1; i < seq.length; i++) {! !! seq[i] = seq[i-1] + i;! ! }! }!

Arrays as Parameters

0 1 3 6 10 15 21 28 36 45 Changes to an array in a method are visible outside the method! sequence! ae2f ae2f

The null reference

  • An array variable holds the special value null if no

array is created (distinct from an array with 0 elements.)

int array[] counts;!!

public static int[] copy(int[] data){! if (data == null) return null;! int[] data2 = new int[data.length];! for (int i = 0; i < data.length; i++){! data2[i] = data[i];! }! return data2;! }! counts! null! Causes nullPointerException if data is null!