? 7c
?F?_
) ?3
?F¥àQ
)D
P¨
?F
)
????°s
?E
)D???
P¨?
?F
)
?
?3
??
e
ì¥0 ?
)
3??
P¨
7??
?
)
P¨ Vector ?
Introducing Arrays
myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
?F
^M] ??¥
? ?
¨?F?
?F
^M] ??¥
? ?
¨?F?
¥B?ˉ?
? ??YV
?F
¥B?ˉ?
? ??YV
?F
?/S V[
P¨
?F?¥
? b
?/S V[
P¨
?F?¥
? b
/SV
/SV
0 7
S b
?F
^
?μI??
7
S b
?F
^
?μI??
y?訥
? 2 b
y?訥
? 2 b
double ??¥ 10?
?Fí
í :
double[] myList = new double[10]
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.
RunSelectionSort
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.
RunLinearSearch
è 7.4 ?
k=s°sE
`¥
?F
)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: ?FBF?¥
? b
?
S
??¥?? b
RunTotalArea
?F¥ˉ?
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.
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
+++++
+++++
+++++
+++++
+++++
=
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
+
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
55555454535352525151
45454444434342424141
35353434333332323131
25252424232322222121
15151412131212121111
5554535251
4544434241
3534333231
2524232221
1514131211
5554535251
4544434241
3534333231
2524232221
1514131211
bababababa
bababababa
bababababa
bababababa
bababababa
bbbbb
bbbbb
bbbbb
bbbbb
bbbbb
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa
Test RunMatrixOperation
Numeric Wrapper Classes
) Boolean
) Character
) Short
) Byte
) Long
) Float
) Double
Object
Double Float Long Integer Short Byte
Character BooleanNumber
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.
Calculator
Run
java Calculator + 2 3
Run
java Calculator - 2 3
Run
java Calculator / 2 3
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.
Run
AssignGradeUsingVector
ˉ5
)??
2
ü?7y
?F$
)
?F
^B?`?
^B?' ??¥′$
)
?F
2
üaüs¥=i
?$
I
1
H?ó
?
Fs¥=i$
)
?F/S¥ ??
^
I
1$Kl/S
^
$
)??7y Vector¥B?
L è$
?¨/
?¥}
?ísù5
?¨/
?¥}
?ísù5
String names[]={“One”, “Two”, “Three”};
String people[];
people=names;
) names [1]¥′
^
I
1$¥′
^
I
1$
) people[2]¥′
^
I
1$¥′
^
I
1$
) /
?}
?
{¥
^
I
1$/
?}
?
{¥
^
I
1$
– System.out.println(names);
) YV|′YV|′ “One”?1?1 “First” ÷?÷? names b
) Xü?M
?FXü?M
?F names
?F
?F people9?
3M9?
3M
?
?$
?
?$
) ??|??| names?¥′ˉ??B??¥M
?$?¥′ˉ??B??¥M
?$
String names[]={“One”, “Two”,“Three”};
String othernames[]=new String[3];
othernames[0] = names[0];
othernames[1] = names[1];
othernames[2] = names[2];
?
System . arrarycopy ( names, 0, othernames, 0, 3);
?D
?D
5?]¥
^I ?B?
?F7??Y
6B?
?F
?]¥
^I ?B?
?F7??Y
6B?
?F
b