1
LẬP TRINH HƢỚ NG ĐÔ
́
I TƢỢNG TRONG JAVA
phần 4
VIII.
1. Chuyn đi gia cc kiu phc hp
(Ancestors),
.
,
.
. ,
ng minh,
:
Child c = new Child();
Parent p = (Parent) c;
2. Chuyn đi kiu sơ cp thnh kiu phc hp
.
,
:
,
,
,
,
Integer :
Integer intObj = new Integer(25);
L
.
-
:
int i = intObj.intValue();
IX. M(ARRAY)
2
M .
.
,
1. To v s dng mng
Khai ba
́
o một biế n tham chiế u đế n ma
̉
ng
ArrayType[] ArrayName
Khai ArrayType ,
ArrayType : [] c
,
int[] anArrayOfInts; //
float[] anArrayOfFloats;
boolean[] anArrayOfBooleans;
Object[] anArrayOfObjects;
String[] anArrayOfStrings;
To mt mng
,
ArrayName = new ArrayType[ArraySize]
ArraySize :
: int[] M; //
M = new int[10]; // t
:
ArrayType[] ArrayName = new ArrayType[ArraySize]
:
ArrayType ArrayName[] = new ArrayType[ArraySize]
: int[] M = new int[10];
int M[] = new int[10];
3
Truy xuất tha
̀
nh phầ n cu
̉
a ma
̉
ng
ArrayVar[index]
index : ch, ,
, 0 ArraySize-1
: M[1] = 20;
Ly kch thưc mng
ArrayName.length
Khơ
̉
i tạo gia
́
tri ̣ đầ u cu
̉
a ma
̉
ng
.
.
,
.
{ v}
: boolean[] answers = { true, false, true, true, false };
int month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
1:
public class ArrayDemo {
public static void main(String[] args) {
int[] anArray;
anArray = new int[10];
for (int i = 0; i < anArray.length; i++) {
anArray[i] = i;
System.out.print(anArray[i] + " ");
}
System.out.println();
}
}
2 :
public class ArrayOfStringsDemo {
public static void main(String[] args) {
String[] anArray = { "String One", "String Two", "String Three" };
for (int i = 0; i < anArray.length; i++) {
System.out.println(anArray[i].toLowerCase());
4
}
}
}
2. Mng đa chiu (Arrays of Arrays)
.
.
: int M[][] = new int[4][5];
int[][] M = new int[4][5];
M l4x5 .
,
,
: int M[][] = new int[3][];
M[0] = new int[3];
M[1] = new int[4];
M[2] = new int[2];
1 :
public class ArrayOfArraysDemo {
public static void main(String[] args) {
String[][] cartoons = {
{ "Flintstones", "Fred", "Wilma", "Pebbles", "Dino" },
{ "Rubbles", "Barney", "Betty", "Bam Bam" },
{ "Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie",
"Astro" },
{ "Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma",
"Fred", "Daphne" }
};
for (int i = 0; i < cartoons.length; i++) {
System.out.print(cartoons[i][0] + ": ");
for (int j = 1; j < cartoons[i].length; j++) {
5
System.out.print(cartoons[i][j] + " ");
}
System.out.println();
}
}
}
.
cartoons[0], cartoons[1]
2 :
public class ArrayOfArraysDemo2 {
public static void main(String[] args) {
int[][] aMatrix = new int[4][];
for (int i = 0; i < aMatrix.length; i++) {
aMatrix[i] = new int[5];
for (int j = 0; j < aMatrix[i].length; j++) {
aMatrix[i][j] = i + j;
}
}
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
System.out.print(aMatrix[i][j] + " ");
}
System.out.println();
}
}
}
3. Sao che
́
p ma
̉
ng (Copying Arrays)
Sarraycopy
.
arraycopy 5 :
public static void arraycopy(ArrayType[] source,
int srcIndex,
ArrayType[] dest,
6
int destIndex,
int length)
.
, .
:
:
public class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
: