ngôn ngữ java mảng và xâu

66 302 0
ngôn ngữ java   mảng và xâu

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

PHẦN 1: NGÔN NGỮ JAVA BÀI 3: MẢNG VÀ XÂU Mảng Cấu trúc liệu v Trong nhiều toán, số liệu có cấu trúc tự nhiên §  Ví dụ : Texts are sequences of characters Images are matrices of pixels Classes contain sets of students v Java cung cấp số lớp tool gọi cấu trúc liệu §  hỗ trợ tổ chức liệu §  thuận lợi việc truy cập cập nhập liệu Một số cấu trúc liệu §  §  §  §  §  §  §  §  Array/Arrays (the data structure we will cover) ArrayList HashSet LinkedHashSet LinkedList TreeSet Vector HashMap Opening problem v Xét chương trình sau : How many days' temperatures? Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44.57142857142857 days were above average What makes the problem hard? v Không sử dụng biến, cần lần nhập giá trị vào §  … để tính giá trị trung bình, thơng qua tổng tích luỹ §  … để đếm số lần lớn giá trị trung bình v Sử dụng biến §  Cần khai báo biến để chứa? v Cần cách -> khai báo nhiều biến lần Mảng v array: Đối tượng chứa nhiều giá trị loại §  element: giá trị mảng §  index: số nguyên vị trí giá trị mảng index value 12 49 -2 26 17 -6 84 72 element element element Khai báo mảng v  Khai báo/khởi tạo mảng : [] = new []; v  Ví dụ : int[] numbers = new int[10]; index value 0 v  length: số nguyên nào: int x = * + 1; int[] data = new int[x % + 2]; Tự động khởi tạo mảng v Khi mảng khởi tạo, tất phần tử khởi tạo tự động tương đương giá trị §  §  §  §  int: double: boolean: object type: 0.0 false null (null means "no object")‫‏‬ Ví dụ v Mảng double index value 0.0 0.0 0.0 0.0 0.0 v Mảng booleans index value false false false false 10 Chuyển đổi kiểu liệu int x = 2640; int y = 5280; return x / y; // integer arithmetic double z = (double) x; return z / y; // double arithmetic Ép kiểu Biểu thức ép kiểu: () Chuyển expression into sang kiểu Examples: (double) 2640 (int) 32.7 (int) ‘A’ (char) 97 (String) ‘A’ (String) 27 == 2640.0 == 32 == ?? == ?? == ?? == ?? Ép kiểu Biểu thức ép kiểu: () Chuyển expression into sang kiểu Examples: (double) 2640 (int) 32.7 (int) ‘A’ (char) 97 (String) ‘A’ (String) 27 == 2640.0 == 32 == 65 == ‘a’ == TypeCastException == TypeCastException Ép kiểu với String? Ép kiểu (String) không sử dụng với kiểu liệu gốc char, int, vàdouble Có nhiều cách để ép kiểu String: Converting to strings: int x to String: x + “” or “” + x double x to String: x + “” or “” + x char, boolean, or float x to String: same thing Ép kiểu với String? Ép kiểu (String) không sử dụng với kiểu liệu gốc char, int, vàdouble Có nhiều cách để ép kiểu String: Converting from strings: String s to int: Integer.parseInt(x) String s to double: Double.parseDouble(x) … String s to char: Character.parseChar(x) Đọc vào Strings Có thể sử dụng Scanners: next() hasNext() Example: Please enter your name: Alexander Pieter Yates Name has letters Name has letters Name has letters Solution import java.util.Scanner; public class NameLength { public static void main(String [] args) { Scanner scan = new Scanner(System.in); System.out.print(“Please enter your name”); int i=1; while(scan.hasNext()) { String s = scan.next(); int length = s.length(); System.out.println(“Name “ + i + “ has “ + length + “ letters”); i++; } } } Lớp Scanner (tiếp) return method Description int nextInt() Reads the next token, converts it to an int (if possible), and returns the int value double nextDouble() Reads the next token, converts it to a double (if possible), and returns the double value String next() Reads the next token, and returns it boolean hasNext() Returns true if there are more tokens StringBuilder /StringBuffer v Một lựa chọn khác cho lớp String v StringBuilder/StringBuffer is more flexible than String You can add, insert, or append new contents into a string buffer, whereas the value of a String object is fixed once the string is created 68 StringBuilder v Lớp StringBuilder hỗ trợ cho lớp String v Mềm dẻo String §  Có thể thêm, chèn nối nội dung vào đệm string String: đối tượng cố định tạo v Thường phân tách StringBuilder thành String hoàn thành khởi tạo 69 StringBuilder Constructors 70 Modifying Strings in the Builder 71 toString, capacity, length, setLength, charAt 72 Ví dụ StringBuilder stringBuilder = new StringBuilder("Welcome to "); stringBuilder.append("Java"); stringBuilder.delete(8, 11) changes the builder to Welcome Java stringBuilder.deleteCharAt(8) changes the builder to Welcome o Java stringBuilder.reverse() changes the builder to avaJ ot emocleW stringBuilder.replace(11, 15, "HTML") changes the builder to Welcome to HTML stringBuilder.setCharAt(0, 'w') sets the builder to welcome to Java 73 StringTokenizer v boolean hasMoreTokens() v String nextToken() v String nextToken(String delim) StringTokenizer +countTokens(): int +hasMoreTokens():boolean +nextToken(): String +nextToken(delim: String): String 26/08/15 74 ... lần Mảng v array: Đối tượng chứa nhiều giá trị loại §  element: giá trị mảng §  index: số nguyên vị trí giá trị mảng index value 12 49 -2 26 17 -6 84 72 element element element Khai báo mảng. .. a is [2, 7, 8, 14, 28, 35, 44] 24 Dịch chuyển phần tử mảng 25 Chèn phần tử v Chèn giá trị vào mảng số nguyên xếp, (dữ nguyên kích thước mảng) ? index value 18 37 64 index value 18 37 26 Chèn... 5.5 7.5 quizScores[7][9] 38 Mảng chiều nhớ v? ?Mảng mảng! double [][] quizScores = new double[4][3]; quizScores quizScores[0] quizScores[1] quizScores[2] quizScores[3] Mảng chiều không double [][]

Ngày đăng: 27/09/2015, 06:30

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