Chapter 5 - Arrays
Chapter 5 - Arrays
ian@purpletrust.org
Mantra:
Coding is fun!
Chapter 5 Arrays
• Introducing Arrays
• Declaring Array Variables, Creating Arrays, and Initializing Arrays
• Passing Arrays to Methods
• Copying Arrays
• Multidimensional Arrays
• Search and Sorting Methods
Introducing Arrays
Array is a data structure that represents a collection
of the same types of data.
double[] myList = new double[10];
• datatype arrayname[];
Example:
double myList[];
Creating Arrays
arrayName = new datatype[arraySize];
Example:
myList = new double[10];
arrayVariable.length
For example,
myList.length returns 10
Initializing Arrays
• Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = i;
• Grade is F otherwise.
Passing Arrays to Methods
Java uses pass by value to pass parameters to
a method. There are important differences
between passing a value of variables of
primitive data types and passing arrays.
• For a parameter of a primitive type value, the
actual value is passed. Changing the value of
the local parameter inside the method does not
affect the value of the variable outside the
method.
• For a parameter of an array type, the value of
the parameter contains a reference to an array;
this reference is passed to the method. Any
changes to the array that occur inside the
method body will affect the original array that
was passed as the argument.
Example 5.3
Passing Arrays as Arguments
Pass by value
xi i
( x mean ) 2
mean i 1 deviation i 1
n n 1
One char [] for
Example 5.5
alphabet
Another [] to store the
random chars
Counting Occurrence of Each
Letter
Math.random()
list1 list1
Contents Contents
of list1 of list1
list2 list2
Contents Contents
of list2 of list2
Garbage
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new
int[sourceArray.length];
Example:
System.arraycopy(sourceArray, 0,
targetArray, 0, sourceArray.length);
Multidimensional Arrays
Declaring Variables of Multidimensional Arrays and Creating Multidimensional
Arrays
double[][] x;
Multidimensional Array Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
Declaring, Creating, and Initializing Using Shorthand Notations
You can also use a shorthand notation to declare, create and
initialize a two-dimensional array. For example,
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
array.length
array[0].length
array[1].length
array[2].length
Ragged Arrays
Each row in a two-dimensional array is
itself an array. So, the rows can have
different lengths. Such an array is known
as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
};
Example 5.7
Adding and Multiplying Two Matrices
cij =
ai1b1j+ai2b2j+ai3b3j+ai4b4j+ai5b5j
Example 5.8
Grading Multiple-Choice Test
• Objective: write a
Students’ Answers to the Questions:
program that grades
0 1 2 3 4 5 6 7 8 9
Student 0 A B A C C D E E A D
multiple-choice test.
Student 1 D B A B C A E E A D
Student 2 E D D A C B E E A D
Student 3 C B A E D C E E A D Key to the Questions:
Student 4 A B D C C D E E A D
Student 5 B B E C C D E E A D 0 1 2 3 4 5 6 7 8 9
Student 6 B B A C C D E E A D
Student 7 Key D B D C C D A E A D
E B E C C D E E A D
Example 5.9
Calculating Total Scores
• Objective: write a program that calculates the total score for
students in a class. Suppose the scores are stored in a three-
dimensional array named scores. The first index in scores
refers to a student, the second refers to an exam, and the
third refers to the part of the exam. Suppose there are 7
students, 5 exams, and each exam has two parts--the
multiple-choice part and the programming part. So, scores[i][j]
[0] represents the score on the multiple-choice part for the i’s
student on the j’s exam. Your program displays the total score
for each student, .
Searching Arrays
Searching is the process of looking for a specific
element in an array; for example, discovering
whether a certain score is included in a list of
scores. Searching, like sorting, is a common task
in computer programming. There are many algorithms
and data structures devoted to searching. In this
section, two commonly used approaches are
discussed, linear search and binary search.
Linear Search
The linear search approach compares the key
element, key, with each element in the array
list[]. The method continues to do so until the
key matches an element in the list or the list is
exhausted without a match being found. If a match
is made, the linear search returns the index of
the element in the array that matches the key. If
no match is found, the search returns -1.
Example 5.10
Testing Linear Search
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
key = 11 mid
Example 5.11
Testing Binary Search
2, 9, 5, 4, 8, 1, 6
Example 5.12: (Cont) Using Arrays in Sorting
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
Find the largest element in myList and swap it with the last element
in myList.
2, 9, 5, 4, 8, 1, 6 => 2, 6, 5, 4, 8, 1, 9 (size = 7)
2, 6, 5, 4, 8, 1 => 2, 6, 5, 4, 1, 8 (size = 6)
2, 6, 5, 4, 1 => 2, 1, 5, 4, 6 (size = 5)
2, 1, 5, 4 => 2, 1, 4, 5
2, 1, 4 => 2, 1, 4,
2, 1 => 1, 2
Sort it to produce 1, 2, 4, 5, 6, 8, 9
Exercise 5.5: Bubble Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
Pass 1: 2, 5, 4, 8, 1, 6, 9
Pass 2: 2, 4, 5, 1, 6, 8, 9
Pass 3: 2, 4, 1, 5, 6, 8, 9
Pass 4: 2, 1, 4, 5, 6, 8, 9
Pass 5: 1, 2, 4, 5, 6, 8, 9
Pass 6: 1, 2, 4, 5, 6, 8, 9