Professional Documents
Culture Documents
ARRAY
1
Mục tiêu bài học
2
One-dimensional Array
3
For loop
• Cú pháp
for(initialization statements; condition; increment
statements)
{
action statements;
:
:
}
4
While loop
Cú pháp
while(condition)
{
action statements;
:
:
}
• Therefore, the code in the block may never be executed. If
you want to make sure a block is executed at least once, you
will need to move the test to the bottom. You do that with the
do/while loop
5
Do-while loop
• Cú pháp
do
{
action statements;
:
:
} while(condition);
6
Break
7
Initializing
8
Using the String args[]
luckyNumbers
= java.util.Arrays.copyOf(luckyNumbers, 2 * luckyNumbers.length);
The additional elements are filled with 0 if the array contains numbers, false if
the array contains boolean values.
10
Array copying (cont)
• Prior to Java SE 6
System.arraycopy(from, fromIndex, to, toIndex, count);
Ouput: 0: 1001
1: 1002
2: 1003
3: 5
4: 7
5: 11
6: 13
11
Array order
12
Array of Object
13
Two-dimensional Array
• Declare
double[][] balances; // double balances[][]; ??
• Instaning
balances = new double[NYEARS][NRATES];
int[][] magicSquare =
{
{16, 3, 2, 13},
{4, 15, 14, 1}
};
• Initializing
for (int i = 1; i < balances.length; i++) {
for (int j = 0; j < balances[i].length; j++) {
balances[i][j] = oldBalance + interest; }}
14
“for each” loop
15
ArrayList
16
ArrayList (tiếp)
17
Understanding variable scope
18
Tổng kết
19