Lập trình hướng đối tượng trong java (Phần 4) ppt

6 333 0
Lập trình hướng đối tượng trong java (Phần 4) ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

         1 LẬP TRINH HƢỚ NG ĐÔ ́ I TƢỢNG TRONG JAVA phần 4 VIII.        1. Chuyn đi gia cc kiu phc hp                      (Ancestors),                  .          ,           .                        .   ,               ng minh,    : Child c = new Child(); Parent p = (Parent) c; 2. Chuyn đi kiu sơ cp thnh kiu phc hp    .                ,  :     ,     ,      ,                      ,              Integer : Integer intObj = new Integer(25); L     . -           : int i = intObj.intValue(); IX. M(ARRAY)          2 M .                   .                                  ,  1. To v s dng mng  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;  To mt mng ,                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;  Ly kch thưc mng 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. Mng đa chiu (Arrays of Arrays)        .                           . : int M[][] = new int[4][5]; int[][] M = new int[4][5]; M l4x5  .        ,  ,  : 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) Sarraycopy      .   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)); } }        :     .          1 LẬP TRINH HƢỚ NG ĐÔ ́ I TƢỢNG TRONG JAVA phần 4 VIII.        1. Chuyn đi gia cc kiu phc hp         

Ngày đăng: 12/07/2014, 22:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan