[LTM] Chương 3.3 : Cấu trúc mảng

Người đăng: share-nhungdieuhay on Thứ Hai, 24 tháng 3, 2014


Cấu trúc mảng

Khai báo: 
<Type>[] <arrayname>;
<Type> <arrayname>[];
<Type>: kiểu nguyên thủy hoặc kiểu lớp
Ex: int [] IntArray, IntVariable;
Poly[] recArray, squArray, ovalArray;
Tạo lập đối tượng: dùng new
<Arrayname> = new <Type>[size];
Ex: IntArray=new Int[10]; recArray=new Poly[5];
Kết hợp:  <Type1>[] <arrayname> = new <Type2>[size];
Khởi tạo: khai báo, tạo lập, gán gtri
<Type>[] <arrayname>={value1, value2..}

Truy xuất mảng<Arrayname>.[index]
index: 0-> Arrayname.length-1
Ngoại lệ: ArrayIndexOutOfBoundsExceptin
Example

Truyền tham số và nhận giá trị trả lại
int[] myCopy(int[] a) {
int b[] = new int[a.length];
for (i=0; i<a.length; i++)
b[i] = a[i];
return b;
}
...
int a[] = {0, 1, 1, 2, 3, 5, 8};
int b[] = myCopy(a);
Ex: sx mảng

Multi Dimensional Array (mảng nhiều chiều)

Khai báo 
<Type>[] [] []… <arrayname>;
<Type> <arrayname>[][]…;
Khai báo,tạo lập:
Ex: int [][] A=new int[4][5];
Khởi tạo: khai báo, tạo lập, gán gtri
Ex: int [] [] matrix =   { {3,5,6,0}, {1,2,0,3}, {0,1,2,4}}
int c[][] = new int[2][];
c[0] = new int[5];
c[1] = new int[10];

Example – Multi Dimensional Array
import java.util.Scanner;
class vd_math {
public static int n =2;
public static int m =3;
public  static int [][] A;
public  static void nhap() {
int i,j;
Scanner nhap = new Scanner(System.in);
for ( i=0; i<=n-1; i++)
for ( j=0; j<=m-1; j++) {
 System.out.print("Nhap pt " + i + "," + j + ": ");
A[i][j]=nhap.nextInt();
}
}  
Example – Multi Dimensional Array
public static void xuat() {
int i,j;
for ( i=0; i<=n-1; i++)
{
for ( j=0; j<=m-1; j++)
System.out.print(A[i][j] + " ");
System.out.println();
}
}
public  static void main(String [] args) {
A = new int [n][m];
nhap();
xuat();
}
}
Tiện ích
„ System.arraycopy(src, s_off, des, d_off, len)
  • …src/ des : mảng nguồn, 
  • s_off/ d_off : offset của mảng nguồn/đích
  • …len: số phầntử cần copy
„Copy nội dung của dữ liệu nguyên thủy, copy tham chiếu đối với đối tượng
Cung cấp 4 phương thức static
  • …fill(): khởi tạo các phần tử của mảng vớimột giá trị như nhau
  • …sort(): xắp xếp mảng
  • …equals(): so sánh hai mảng
  • …binarySearch(): tìm kiếm nhị phân trên mảng đã sắp xếp


ex
import java.util.Arrays;
class vd_math {
public  static void main(String [] args) 
{
int a[] = { 5, 3 , 7 , 4 };
int b[] = new int[a.length];
System.arraycopy(a,0,b,0,a.length);
System.out.println(Arrays.equals(a,b)); 
Arrays.sort(a);
for (int i=0; i<= a.length-1; i++)
System.out.print(a[i] + " ");
}
}
Xử lý toán học: Math class
„ Hằng số: Math.E +Math.PI
„ Các phương thức static
  • type abs(type), sqrt(double)
  • double ceil(double), double floor(double)
  • int round(float), long round(double)
  • type max(type, type), type min(type, type)
  • double random(): sinh số ngẫu nhiên trong đoạn [0.0,1.0]
Math class
„ Lũythừa
  • double pow(double, double)
  • … double exp(double)
  • … double log(double)
  • … double sqrt(double)
„ Lượng giác
  • … double sin(double)
  • … double cos(double)
  • … double tan(double)

{ 0 nhận xét... read them below or add one }

Đăng nhận xét