Transcript Lecture 7

Chapter 7 Arrays and Vectors
Introducing Arrays
 Declaring Arrays, Creating Arrays, and
Initializing Arrays
 Array of Objects
 Copying Arrays
 Multidimensional Arrays
 Numeric Wrapper Classes
 Command-Line Parameters
 Creating Generic Classes
 The Vector Class

Introducing Arrays
Array is a data structure
that represents a
collection of the same
types of data. Java treats
these arrays as objects.
An Array of 10 Elements
of type double
double[] myList = new double[10]
myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
Declaring Arrays

datatype[] arrayname;
Example:
int[] myList;

datatype arrayname[];
Example:
int myList[];
Creating Arrays
arrayName = new datatype[arraySize];
Example:
myList = new double[10];
Declaring and Creating
in One Step

datatype[] arrayname = new
datatype[arraySize];
double[] myList = new double[10];

datatype arrayname[] = new
datatype[arraySize];
double myList[] = new double[10];
Initializing Arrays

Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = (double)i;

Declaring, creating, initializing in one
step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
Example 7.1
Assigning Grades

Objective: read student scores (int) from the
keyboard, get the best score, and then assign
grades based on the following scheme:
 Grade is A if score is >= best–10;

Grade is B if score is >= best–20;

Grade is C if score is >= best–30;

Grade is D if score is >= best–40;

Grade is F otherwise.
AssignGrade
Run
Example 7.2
Using Arrays in Sorting

Objective: Use the selectionSort method to
write a program that will sort a list of
double floating-point numbers.
SelectionSort
Run
Example 7.3
Testing Linear Search

Objective: Implement and test the linear
search method by creating an array of 10
elements of int type randomly and then
displaying this array. Prompt the user to
enter a key for testing the linear search.
LinearSearch
Run
Array of Objects

Declaring and creating:
Circle[] circleArray = new Circle[10];

Initializing:
for (int i=0; i<circleArray.length; i++)
{
circleArray[i] = new Circle();
}
Example 7.5
Summarizing the Areas of Circles

Objective: Summarize the areas of an
array of circles
TotalArea
Run
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
for (int i = 0; i < sourceArrays.length; i++)
targetArray[i] = sourceArray[i];
The arraycopy Utility
arraycopy(sourceArray, src_pos, targetArray,
tar_pos, length);
Example:
System.arraycopy(sourceArray, 0, targetArray, 0,
sourceArray.length);
Multidimensional Arrays
int[][] matrix = new int[10][10];
or
int matrix[][] = new int[10][10];
for (int i=0; i<matrix.length; i++)
for (int j=0; j<matrix[i].length; j++) {
matrix[i][j] = (int)(Math.random()*1000);
}
Example 7.7
Adding and Multiplying Two Matrices

Objective: Use two-dimensional arrays to
create two matrices, and then add and
multiple the two matrices.
 a11 a12 a13 a14 a15 
 b11 b12 b13 b14 b15 
 a11  b11 a12  b12 a12  b13 a12  b14 a15  b15 






a
21
a
22
a
23
a
24
a
25
b
21
b
22
b
23
b
24
b
25
a
21

b
21
a
22

b
22
a
23

b
23
a
24

b
24
a
25

b
25






 a 31 a 32 a 33 a 34 a 35    b31 b32 b33 b34 b35    a 31  b31 a 32  b32 a 33  b33 a 34  b34 a 35  b35 






 a 41 a 42 a 43 a 44 a 45 
 b 41 b 42 b 43 b 44 b 45 
 a 41  b 41 a 42  b 42 a 43  b 43 a 44  b 44 a 45  b 45 
 a 51 a 52 a 53 a 54 a 55 
 b51 b52 b53 b54 b55 
 a 51  b51 a 52  b52 a 53  b53 a 54  b54 a 55  b55 






TestMatrixOperation
Run
Numeric Wrapper Classes

Boolean
Long

Character
Float

Short
Double

Byte
Object
Number
Character Boolean
-
-
Double
-
Float
-
Long
-
Integer
-
Short
-
Byte
-
The Integer Class
and The Double Class

Constructors

Class Constants MAX_VALUE, MIN_VALUE

Conversion Methods
Command-Line Parameters
class TestMain
{
public static void main(String[] args)
{ ... }
}
java TestMain arg0, arg1, arg2, ..., argn
Processing
Command-Line Parameters
In the main method, get the arguments from
args[0], args[1], ..., args[n], which corresponds
to arg0, arg1, ..., argn in the command line.
Example 7.8
Using Command-Line
Parameters

Objective: Write a program that will
perform binary operations on integers.
The program receives three parameters:
an operator and two integers.
Run
java Calculator + 2 3
Run
java Calculator - 2 3
Run
java Calculator / 2 3
Calculator
Example 7.9
Designing Generic Classes

Objective: This example gives a generic class
for matrix arithmetic. This class implements
matrix addition and multiplication common for
all types of matrices.
GenericMatrix
Example 7.10
Extending Abstract Classes
GenericMatrix
IntegerMatrix
-Object[][] matrix
#GenericMatrix(Object[][] matrix)
+Object[][] getMatrix()
+void setMatrix(Object[][] matrix)
+Object[][] addMatrix(Object[][] secondMatrix)
+Object[][] multiplyMatrix(Object[][] secondMatrix)
+void printResult(
GenericMatrix m1, GenericMatrix m2, GenericMatrix m3, char op)
#GenericMatrix createGenericMatrix()
#Object add(Object o1, Object o2)
#Object multiply(Object o1, Object o2)
#Object zero()
RationalMatrix
Example 7.10
Extending Abstract Classes,
cont.

Objective: This example gives two programs
that utilize the GenericMatrix class for integer
matrix arithmetic and rational matrix
arithmetic.
IntegerMatrix
TestIntegerMatrix
Run
RationalMatrix
TestRationalMatrix
Run
The Vector Class
Java provides the Vector class in the java.util
package, which can be used to store an
unspecified number of elements. A vector can
grow or shrink dynamically as needed.
The Vector Class, cont.
To create a vector, you can use its default
constructor like this:
Vector vector = new Vector();
To add an element to the vector, use the
addElement method as follows:
vector.addElement(anObject);
The element in the vector must be an object.
The element is appended to the vector.
The Vector Class, cont.
To retrieve an element, use the elementAt
method as follows:
Object anObject = vector.elementAt(0);
To find the number of elements in the vector,
use the size method as follows:
int numOfElements = vector.size();
Example 7.10
Assigning Grades Using a
Vector

Objective: Rewrites Example 7.1 using a
vector, instead of using an array. The
program reads student scores from the
keyboard, stores the score in the vector,
finds the best score, and then assigns
grades for all the students.
AssignGradeUsingVector
Run